-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
199 lines (159 loc) · 4.25 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2020 Pieoneers Software Incorporated. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package client_test
import (
"io/ioutil"
. "github.com/pieoneers/jsonapi-client-go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// func ExampleRequest_NewRequest() {
// type Item struct{
// ID string `json:"-"`
// Type string `json:"-"`
// Title string `json:"title"`
// CreatedAt time.Time `json:"created_at"`
// }
//
// type Items []Item
//
// var target Items
//
// request, requestErr := client.NewRequest("GET", "/data", nil)
//
// if requestErr != nil {
// log.Println("requestErr: ",requestErr)
// return
// }
//
// request.Query.Set("filter[query]", "silver")
//
// response, responseErr := jsonapiClient.Do(request, &target)
//
// if responseErr != nil {
// log.Println("responseErr: ", responseErr)
// return
// }
// }
var _ = Describe("Request", func() {
Describe("NewRequest", func() {
Describe("Method", func() {
for _, method := range []string{"GET", "POST"} {
When(method, func() {
var request *Request
BeforeEach(func() {
request, _ = NewRequest(method, "/foo", nil)
})
It("should be equal GET", func() {
Ω(request.Method).Should(Equal(method))
})
})
}
})
Describe("URL", func() {
var req *Request
url := "/bar?include=foo"
BeforeEach(func() {
req, _ = NewRequest("GET", url, nil)
})
It("should set correct path", func() {
Ω(req.URL.Path).Should(Equal("/bar"))
})
It("should set correct query", func() {
Ω(req.URL.RawQuery).Should(Equal("include=foo"))
})
})
Describe("Header", func() {
var request *Request
When("there is no payload", func() {
BeforeEach(func() {
request, _ = NewRequest("GET", "/books", nil)
})
It("should have correct Accept header", func() {
actual := request.Header.Get("Accept")
Ω(actual).Should(Equal("application/vnd.api+json"))
})
It("should have no Content-Type header", func() {
actual := request.Header.Get("Content-Type")
Ω(actual).Should(Equal(""))
})
})
When("there is payload", func() {
BeforeEach(func() {
request, _ = NewRequest("POST", "/books", Book{
Title: "An Introduction to Programming in Go",
Year: "2012",
})
})
It("should have correct Accept header", func() {
actual := request.Header.Get("Accept")
Ω(actual).Should(Equal("application/vnd.api+json"))
})
It("should have correct Content-Type header", func() {
actual := request.Header.Get("Content-Type")
Ω(actual).Should(Equal("application/vnd.api+json"))
})
})
})
Describe("Body", func() {
var request *Request
When("there is no payload", func() {
BeforeEach(func() {
request, _ = NewRequest("GET", "/books", nil)
})
It("should have empty body", func() {
actual := request.Body
Ω(actual).Should(BeNil())
})
})
When("there is payload", func() {
payload := Book{
Title: "An Introduction to Programming in Go",
Year: "2012",
}
BeforeEach(func() {
request, _ = NewRequest("POST", "/books", payload)
})
It("should have book payload in body", func() {
actual, _ := ioutil.ReadAll(request.Body)
expected, _ := Template("book-payload", payload)
Ω(actual).Should(MatchJSON(expected))
})
})
})
})
Describe("RequestURI", func() {
var (
req *Request
url string
)
BeforeEach(func() {
req, _ = NewRequest("GET", "/foo", nil)
})
JustBeforeEach(func() {
url = req.RequestURI()
})
It("should return correct url", func() {
Ω(url).Should(Equal("/foo"))
})
When("query is passed in url", func() {
BeforeEach(func() {
req, _ = NewRequest("GET", "/foo?include=bar,baz", nil)
})
It("should return correct url", func() {
Ω(url).Should(Equal("/foo?include=bar%2Cbaz"))
})
})
When("query is set afterwards", func() {
BeforeEach(func() {
query := req.Query
query.Set("include", "bar,baz")
query.Set("filter[id]", "quux")
})
It("should return correct url", func() {
Ω(url).Should(Equal("/foo?filter%5Bid%5D=quux&include=bar%2Cbaz"))
})
})
})
})