-
Notifications
You must be signed in to change notification settings - Fork 12
/
list_test.go
131 lines (98 loc) · 3.14 KB
/
list_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package jsh
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestList(t *testing.T) {
Convey("List Tests", t, func() {
testObject := &Object{
ID: "ID123",
Type: "testConversion",
Attributes: json.RawMessage(`{"foo":"bar"}`),
}
testList := List{testObject}
req := &http.Request{Method: "GET"}
Convey("->Validate()", func() {
err := testList.Validate(req, true)
So(err, ShouldBeNil)
})
Convey("->Send(list)", func() {
Convey("should send a properly formatted List response", func() {
writer := httptest.NewRecorder()
err := Send(writer, req, testList)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusOK)
contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
req, reqErr := testRequest(writer.Body.Bytes())
So(reqErr, ShouldBeNil)
responseList, parseErr := ParseList(req)
So(parseErr, ShouldBeNil)
So(len(responseList), ShouldEqual, 1)
})
})
Convey("->Send(empty list)", func() {
Convey("should send a properly formatted empty List response", func() {
writer := httptest.NewRecorder()
err := Send(writer, req, List([]*Object{}))
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusOK)
contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
req, reqErr := testRequest(writer.Body.Bytes())
So(reqErr, ShouldBeNil)
responseList, parseErr := ParseList(req)
So(parseErr, ShouldBeNil)
So(len(responseList), ShouldEqual, 0)
})
})
Convey("->Send(nil list)", func() {
Convey("should fail with a 500 ISE", func() {
writer := httptest.NewRecorder()
var list []*Object
err := Send(writer, req, List(list))
So(err, ShouldNotBeNil)
So(writer.Code, ShouldEqual, http.StatusInternalServerError)
})
})
Convey("->UnmarshalJSON()", func() {
Convey("should handle a data object", func() {
jObj := `{"data": {"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}}}`
l := List{}
err := l.UnmarshalJSON([]byte(jObj))
So(err, ShouldBeNil)
So(l, ShouldNotBeEmpty)
})
Convey("should handle a data list", func() {
jList := `{"data": [{"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}}]}`
l := List{}
err := l.UnmarshalJSON([]byte(jList))
So(err, ShouldBeNil)
So(l, ShouldNotBeEmpty)
})
Convey("should handle an empty array", func() {
jObj := `{"data": []}`
l := List{}
err := l.UnmarshalJSON([]byte(jObj))
So(err, ShouldBeNil)
So(l, ShouldNotBeNil)
})
})
Convey("->MarshalJSON()", func() {
Convey("should preserve an empty list", func() {
list := List{}
jData, err := json.Marshal(list)
So(err, ShouldBeNil)
So(string(jData), ShouldEqual, "[]")
})
})
})
}