Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage report and add tests #3

Merged
merged 13 commits into from
Mar 30, 2016
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ script:
- go test -v -race -covermode=atomic -coverprofile=coverage.out

after_success:
- goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
- goveralls -coverprofile=coverage.out -service=travis-ci
170 changes: 170 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,171 @@
package intercept

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"github.com/nbio/st"
"io/ioutil"
"net/http"
"strings"
"testing"
)

var errRead = errors.New("read error")

type errorReader struct{}

func (r *errorReader) Read(p []byte) (int, error) {
return 0, errRead
}

type user struct {
XMLName xml.Name `xml:"Person"`
Name string
}

func TestNewRequestModifier(t *testing.T) {
h := http.Header{}
h.Set("foo", "bar")
req := &http.Request{Header: h}
modifier := NewRequestModifier(req)
st.Expect(t, modifier.Request, req)
st.Expect(t, modifier.Header, h)
}

func TestReadString(t *testing.T) {
bodyStr := `{"name":"Rick"}`
strReader := strings.NewReader(bodyStr)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
str, err := modifier.ReadString()
st.Expect(t, err, nil)
st.Expect(t, str, bodyStr)
}

func TestReadStringError(t *testing.T) {
body := ioutil.NopCloser(&errorReader{})
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
str, err := modifier.ReadString()
st.Expect(t, err, errRead)
st.Expect(t, str, "")
}

func TestReadBytes(t *testing.T) {
bodyBytes := []byte(`{"name":"Rick"}`)
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
str, err := modifier.ReadBytes()
st.Expect(t, err, nil)
st.Expect(t, str, bodyBytes)
}

func TestReadBytesError(t *testing.T) {
body := ioutil.NopCloser(&errorReader{})
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
buf, err := modifier.ReadBytes()
st.Expect(t, err, errRead)
st.Expect(t, len(buf), 0)
}

func TestDecodeJSON(t *testing.T) {
bodyBytes := []byte(`{"name":"Rick"}`)
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeJSON(&u)
st.Expect(t, err, nil)
st.Expect(t, u.Name, "Rick")
}

func TestDecodeJSONErrorFromReadBytes(t *testing.T) {
body := ioutil.NopCloser(&errorReader{})
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeJSON(&u)
st.Expect(t, err, errRead)
st.Expect(t, u.Name, "")
}

func TestDecodeJSONEOF(t *testing.T) {
bodyBytes := []byte("")
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeJSON(&u)
st.Expect(t, err, nil)
st.Expect(t, u.Name, "")
}

func TestDecodeJSONErrorFromDecode(t *testing.T) {
bodyBytes := []byte(`/`)
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeJSON(&u)
_, ok := (err).(*json.SyntaxError)
st.Expect(t, ok, true)
st.Expect(t, err.Error(), "invalid character '/' looking for beginning of value")
st.Expect(t, u.Name, "")
}

func TestDecodeXML(t *testing.T) {
bodyBytes := []byte(`<Person><Name>Rick</Name></Person>`)
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeXML(&u, nil)
st.Expect(t, err, nil)
st.Expect(t, u.Name, "Rick")
}

func TestDecodeXMLErrorFromReadBytes(t *testing.T) {
body := ioutil.NopCloser(&errorReader{})
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeXML(&u, nil)
st.Expect(t, err, errRead)
st.Expect(t, u.Name, "")
}

func TestDecodeXMLErrorFromDecode(t *testing.T) {
bodyBytes := []byte(`]]>`)
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeXML(&u, nil)
_, ok := (err).(*xml.SyntaxError)
st.Expect(t, ok, true)
st.Expect(t, err.Error(), "XML syntax error on line 1: unescaped ]]> not in CDATA section")
st.Expect(t, u.Name, "")
}

func TestDecodeXMLEOF(t *testing.T) {
bodyBytes := []byte("")
strReader := bytes.NewBuffer(bodyBytes)
body := ioutil.NopCloser(strReader)
req := &http.Request{Header: http.Header{}, Body: body}
modifier := NewRequestModifier(req)
u := user{}
err := modifier.DecodeXML(&u, nil)
st.Expect(t, err, nil)
st.Expect(t, u.Name, "")
}