-
Notifications
You must be signed in to change notification settings - Fork 12
/
parser_test.go
131 lines (106 loc) · 3.53 KB
/
parser_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"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParsing(t *testing.T) {
Convey("Parse Tests", t, func() {
Convey("->validateHeaders()", func() {
req, reqErr := http.NewRequest("GET", "", nil)
So(reqErr, ShouldBeNil)
req.Header.Set("Content-Type", "jpeg")
err := validateHeaders(req.Header)
So(err, ShouldNotBeNil)
So(err.Status, ShouldEqual, http.StatusNotAcceptable)
})
Convey("->ParseObject()", func() {
Convey("should parse a valid object", func() {
objectJSON := `{
"data": {
"type": "user",
"id": "sweetID123",
"attributes": {"ID":"123"},
"relationships": {
"company": {
"data": { "type": "company", "id": "companyID123" }
},
"comments": {
"data": [
{ "type": "comments", "id": "commentID123" },
{ "type": "comments", "id": "commentID456" }
]
}
}
}
}`
req, reqErr := testRequest([]byte(objectJSON))
So(reqErr, ShouldBeNil)
object, err := ParseObject(req)
So(err, ShouldBeNil)
So(object, ShouldNotBeEmpty)
So(object.Type, ShouldEqual, "user")
So(object.ID, ShouldEqual, "sweetID123")
So(object.Attributes, ShouldResemble, json.RawMessage(`{"ID":"123"}`))
So(object.Relationships["company"], ShouldResemble, &Relationship{Data: ResourceLinkage{&ResourceIdentifier{Type: "company", ID: "companyID123"}}})
So(object.Relationships["comments"], ShouldResemble, &Relationship{Data: ResourceLinkage{{Type: "comments", ID: "commentID123"}, {Type: "comments", ID: "commentID456"}}})
})
Convey("should reject an object with missing attributes", func() {
objectJSON := `{"data": {"id": "sweetID123", "attributes": {"ID":"123"}}}`
req, reqErr := testRequest([]byte(objectJSON))
So(reqErr, ShouldBeNil)
_, err := ParseObject(req)
So(err, ShouldNotBeNil)
So(err.Status, ShouldEqual, 422)
So(err.Source.Pointer, ShouldEqual, "/data/attributes/type")
})
Convey("should accept empty ID only for POST", func() {
objectJSON := `{"data": {"id": "", "type":"test", "attributes": {"ID":"123"}}}`
req, reqErr := testRequest([]byte(objectJSON))
So(reqErr, ShouldBeNil)
Convey("POST test", func() {
req.Method = "POST"
_, err := ParseObject(req)
So(err, ShouldBeNil)
})
Convey("PATCH test", func() {
req.Method = "PATCH"
_, err := ParseObject(req)
So(err, ShouldNotBeNil)
})
})
})
Convey("->ParseList()", func() {
Convey("should parse a valid list", func() {
listJSON :=
`{"data": [
{"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}},
{"type": "user", "id": "sweetID456", "attributes": {"ID":"456"}}
]}`
req, reqErr := testRequest([]byte(listJSON))
So(reqErr, ShouldBeNil)
list, err := ParseList(req)
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 2)
object := list[1]
So(object.Type, ShouldEqual, "user")
So(object.ID, ShouldEqual, "sweetID456")
So(object.Attributes, ShouldResemble, json.RawMessage(`{"ID":"456"}`))
})
Convey("should error for an invalid list", func() {
listJSON :=
`{"data": [
{"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}},
{"type": "user", "attributes": {"ID":"456"}}
]}`
req, reqErr := testRequest([]byte(listJSON))
So(reqErr, ShouldBeNil)
_, err := ParseList(req)
So(err, ShouldNotBeNil)
So(err.Status, ShouldEqual, 422)
So(err.Source.Pointer, ShouldEqual, "/data/attributes/id")
})
})
})
}