-
Notifications
You must be signed in to change notification settings - Fork 8
/
request_test.go
95 lines (78 loc) · 1.98 KB
/
request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package jsonapi_test
import (
"bytes"
"errors"
"net/http/httptest"
"testing"
. "github.com/mfcochauxlaberge/jsonapi"
"github.com/stretchr/testify/assert"
)
func TestNewRequest(t *testing.T) {
assert := assert.New(t)
// Schema
schema := newMockSchema()
tests := []struct {
name string
method string
url string
schema *Schema
expectedError string
}{
{
name: "get collection (mock schema)",
method: "GET",
url: "/mocktypes1",
schema: schema,
expectedError: "",
}, {
name: "bad url",
method: "GET",
url: "/invalid",
schema: schema,
expectedError: `400 Bad Request: "invalid" is not a known type.`,
}, {
name: "bad url",
method: "GET",
url: "/mocktypes1?filter={invalid}",
schema: schema,
expectedError: `400 Bad Request: The filter parameter is not a ` +
`string or a valid JSON object.`,
},
}
for _, test := range tests {
body := bytes.NewBufferString("")
req := httptest.NewRequest(test.method, test.url, body)
doc, err := NewRequest(req, test.schema)
if test.expectedError == "" {
assert.NoError(err)
assert.Equal(test.method, doc.Method, test.name)
} else {
assert.EqualError(err, test.expectedError, test.name)
assert.Nil(doc)
}
}
}
func TestNewRequestInvalidBody(t *testing.T) {
assert := assert.New(t)
// Schema
schema := newMockSchema()
// Nil body
req := httptest.NewRequest("POST", "/mocktypes1", badReader{})
doc, err := NewRequest(req, schema)
assert.EqualError(err, "bad reader")
assert.Nil(doc)
// Invalid body
body := bytes.NewBufferString("{invalidjson}")
req = httptest.NewRequest("POST", "/mocktypes1", body)
doc, err = NewRequest(req, schema)
assert.EqualError(
err,
"invalid character 'i' looking for beginning of object key string",
)
assert.Nil(doc)
}
type badReader struct {
}
func (badReader) Read([]byte) (int, error) {
return 0, errors.New("bad reader")
}