-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_test.go
331 lines (259 loc) · 9.82 KB
/
render_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package acceptable_test
import (
"net/http"
"net/http/httptest"
"testing"
. "github.com/onsi/gomega"
"github.com/rickb777/acceptable"
"github.com/rickb777/acceptable/header"
. "github.com/rickb777/acceptable/headername"
"github.com/rickb777/acceptable/offer"
"github.com/rickb777/acceptable/templates"
)
func Test_should_return_no_content_if_no_offers_have_data(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/test")
b := offer.Of(offer.TXTProcessor(), "text/plain")
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a, b)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(204))
g.Expect(w.Header()).To(HaveLen(0))
g.Expect(w.Body.String()).To(Equal(""))
}
func Test_should_use_default_processor_if_no_accept_header(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/test").With(nil, "en")
b := offer.Of(offer.TXTProcessor(), "text/plain").With("foo", "en")
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a, b)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(204))
g.Expect(w.Header()).To(HaveLen(2))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/test;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Body.String()).To(Equal(""))
}
func Test_should_use_catch_all_if_no_matching_accept_header(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/csv").With("foo", "*")
b := offer.Of(offer.TXTProcessor(), "").With("bar", "*")
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "image/*, application/*")
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 201, "", a, b)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(201))
g.Expect(w.Header()).To(HaveLen(2))
g.Expect(w.Header().Get(ContentType)).To(Equal("application/octet-stream"))
g.Expect(w.Header().Get(Vary)).To(Equal("Accept"))
g.Expect(w.Body.String()).To(Equal("bar\n"))
}
func Test_should_return_406_if_no_matching_accept_header(t *testing.T) {
g := NewWithT(t)
cases := []string{"application/xml", "text/test"}
for _, c := range cases {
// Given ...
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "image/png")
w := httptest.NewRecorder()
a := offer.Of(offer.JSONProcessor(), c).With("foo", "en")
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(406))
}
}
func Test_should_return_204_if_there_are_no_offers(t *testing.T) {
g := NewWithT(t)
// Given ...
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "image/png")
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "")
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(204))
}
func Test_should_give_JSON_response_for_ajax_requests(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.JSONProcessor(), "application/json").With(`"foo"`, "en")
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(XRequestedWith, header.XMLHttpRequest)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Header()).To(HaveLen(2))
g.Expect(w.Header().Get(ContentType)).To(Equal("application/json;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
}
func Test_should_give_406_for_unmatched_ajax_requests(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.JSONProcessor(), "text/plain").With("foo", "en")
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(XRequestedWith, header.XMLHttpRequest)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(406))
}
func Test_should_return_204_if_there_are_no_offers_for_ajax(t *testing.T) {
g := NewWithT(t)
// Given ...
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "image/png")
req.Header.Add(XRequestedWith, header.XMLHttpRequest)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "")
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(204))
}
func Test_should_return_406_with_fallback_offer(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/foo").With(nil, "en-GB")
b := offer.Of(offer.TXTProcessor(), "text/bar").With(`bad stuff`, "en").CanHandle406As(400)
req, _ := http.NewRequest("GET", "/", nil)
// this header means "anything but text/test"
req.Header.Add(Accept, "text/foo;q=0, text/bar;q=0, */*") // excluded
req.Header.Add(AcceptLanguage, "en") // accepted
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a, b)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(400))
g.Expect(w.Header()).To(HaveLen(2))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/bar;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Body.String()).To(Equal("bad stuff\n"))
}
// RFC7231 suggests that 406 is sent when no media range matches are possible.
func Test_should_return_406_when_media_range_is_explicitly_excluded(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/foo").With(nil, "en")
b := offer.Of(offer.TXTProcessor(), "text/bar").With(nil, "en")
req, _ := http.NewRequest("GET", "/", nil)
// this header means "anything but text/test"
req.Header.Add(Accept, "text/foo;q=0, text/bar;q=0, */*") // excluded
req.Header.Add(AcceptLanguage, "en") // accepted
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a, b)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Code).To(Equal(406))
g.Expect(w.Header()).To(HaveLen(1))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/plain;charset=utf-8"))
g.Expect(w.Body.String()).To(Equal("Not Acceptable\n")) // from http.StatusText()
}
// RFC7231 recommends that, when no language matches are possible, a response should be sent anyway.
func Test_should_return_200_even_when_language_is_explicitly_excluded(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/test").With(nil, "en")
req, _ := http.NewRequest("GET", "/", nil)
// this header means "anything but text/test"
req.Header.Add(Accept, "text/test, */*")
req.Header.Add(AcceptLanguage, "en;q=0, *") // anything but "en"
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Header()).To(HaveLen(3))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/test;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Header().Get(Vary)).To(Equal("Accept, Accept-Language"))
}
func Test_should_negotiate_using_media_and_language(t *testing.T) {
g := NewWithT(t)
// Given ...
// should be skipped because of media mismatch
a := offer.Of(offer.TXTProcessor(), "text/html").With(nil, "en")
// should be skipped because of language mismatch
b := offer.Of(offer.TXTProcessor(), "text/test").With(nil, "de")
// should match
c := offer.Of(offer.TXTProcessor(), "text/test").With(nil, "en")
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "text/test, text/*")
req.Header.Add(AcceptLanguage, "en-GB, fr-FR")
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a, b, c)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Header()).To(HaveLen(3))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/test;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Header().Get(Vary)).To(Equal("Accept, Accept-Language"))
}
func Test_should_render_iso8859_html_using_templates(t *testing.T) {
g := NewWithT(t)
// Given ...
p := templates.Templates("example/templates/en", ".html", nil)
a := offer.Of(p, "text/html").With(nil, "en")
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "text/html")
req.Header.Add(AcceptLanguage, "en, fr")
req.Header.Add(AcceptCharset, "iso-8859-1")
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "home.html", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Header()).To(HaveLen(3))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/html;charset=windows-1252"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Header().Get(Vary)).To(Equal("Accept, Accept-Language, Accept-Charset"))
}
func Test_should_match_utf8_charset_when_acceptable(t *testing.T) {
g := NewWithT(t)
// Given ...
a := offer.Of(offer.TXTProcessor(), "text/html").With("foo", "en")
// all these cases contain utf-8 and another
cases := []string{
"utf-8, iso-8859-1",
"utf8, iso-8859-1",
"iso-8859-1, utf-8",
"iso-8859-1, utf8",
}
for _, cs := range cases {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(Accept, "text/html")
req.Header.Add(AcceptLanguage, "en")
req.Header.Add(AcceptCharset, cs)
w := httptest.NewRecorder()
// When ...
err := acceptable.RenderBestMatch(w, req, 0, "", a)
// Then ...
g.Expect(err).NotTo(HaveOccurred())
g.Expect(w.Header()).To(HaveLen(3))
g.Expect(w.Header().Get(ContentType)).To(Equal("text/html;charset=utf-8"))
g.Expect(w.Header().Get(ContentLanguage)).To(Equal("en"))
g.Expect(w.Header().Get(Vary)).To(Equal("Accept, Accept-Language"))
}
}