-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
381 lines (343 loc) · 9.39 KB
/
writer_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package treetop
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func mockRequest(path string, accept string) *http.Request {
req := httptest.NewRequest("GET", path, nil)
req.Header.Set("Accept", accept)
return req
}
func TestWriter(t *testing.T) {
type args struct {
w *httptest.ResponseRecorder
req *http.Request
status int
isPartial bool
pageURL string
replaceState bool
}
tests := []struct {
name string
args args
wantWriter bool
wantContentType string
wantStatus int
wantBody string
wantPageURL string
wantError string
}{
{
name: "non tt request",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", "text/html"),
isPartial: true,
},
wantWriter: false,
},
{
name: "fragment",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", TemplateContentType),
status: 201,
isPartial: false,
},
wantWriter: true,
wantContentType: TemplateContentType,
wantStatus: 201,
wantBody: `<p>this is a test</p>`,
},
{
name: "partial",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", TemplateContentType),
status: 201,
isPartial: true,
},
wantWriter: true,
wantContentType: TemplateContentType,
wantStatus: 201,
wantBody: `<p>this is a test</p>`,
wantPageURL: "/Some/path",
},
{
name: "partial replace history state",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", TemplateContentType),
status: 201,
isPartial: true,
pageURL: "/Some/other/path",
replaceState: true,
},
wantWriter: true,
wantContentType: TemplateContentType,
wantStatus: 201,
wantBody: `<p>this is a test</p>`,
wantPageURL: "/Some/other/path",
},
{
name: "Ignore invalid URL",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", TemplateContentType),
status: 201,
pageURL: "/$%^&*",
},
wantWriter: true,
wantContentType: TemplateContentType,
wantStatus: 201,
wantBody: `<p>this is a test</p>`,
wantPageURL: "/$%^&*",
},
{
name: "Escape non-ascii urls",
args: args{
w: httptest.NewRecorder(),
req: mockRequest("/Some/path", TemplateContentType),
status: 201,
pageURL: "/☆",
},
wantWriter: true,
wantContentType: TemplateContentType,
wantStatus: 201,
wantBody: `<p>this is a test</p>`,
wantPageURL: "/%E2%98%86",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
ttW Writer
ok bool
)
if tt.args.isPartial {
ttW, ok = NewPartialWriter(tt.args.w, tt.args.req)
} else {
ttW, ok = NewFragmentWriter(tt.args.w, tt.args.req)
}
if ok != tt.wantWriter {
t.Errorf("Writer() ok = %v, want %v", ok, tt.wantWriter)
}
if !ok {
return
}
if tt.args.status > 0 {
ttW.Status(tt.args.status)
}
if tt.args.pageURL != "" {
if tt.args.replaceState {
ttW.ReplacePageURL(tt.args.pageURL)
} else {
ttW.DesignatePageURL(tt.args.pageURL)
}
}
_, err := fmt.Fprint(ttW, "<p>this is a test</p>")
if err != nil {
t.Errorf("Writer got error %s", err)
}
status := tt.args.w.Code
contentType := tt.args.w.Header().Get("Content-Type")
pageURL := tt.args.w.Header().Get("X-Page-Url")
responseHistory := tt.args.w.Header().Get("X-Response-History")
var wantHistory string
if tt.args.replaceState {
wantHistory = "replace"
}
if tt.args.isPartial || tt.args.pageURL != "" {
if vary := tt.args.w.Header().Get("Vary"); vary != "Accept" {
t.Errorf("Expecting Vary header to be 'Accept', got %#v", vary)
}
}
if tt.wantStatus > 0 && status != tt.wantStatus {
t.Errorf("Writer() status writer = %v, want %v", status, tt.wantStatus)
}
if contentType != tt.wantContentType {
t.Errorf("Writer() contentType writer = %v, want %v", contentType, tt.wantContentType)
}
if pageURL != tt.wantPageURL {
t.Errorf("Writer() page url header = %v, want %v", pageURL, tt.wantPageURL)
}
if responseHistory != wantHistory {
t.Errorf("Writer() response history header = %v, want %v", responseHistory, wantHistory)
}
})
}
}
func TestNewPartialWriter_Basic(t *testing.T) {
request := mockRequest("/test", TemplateContentType)
rsp := httptest.NewRecorder()
if tW, ok := NewPartialWriter(rsp, request); ok {
tW.Header().Add("Vary", "Cookie") // test Vary header handling
tW.Write([]byte("something here!"))
} else {
t.Error("Failed to recognize template request")
return
}
if rsp.Code != 200 {
t.Errorf("Expecting status 200 got %d", rsp.Code)
}
cType := rsp.Header().Get("Content-Type")
if cType != TemplateContentType {
t.Errorf("Expecting content type %s, got %s", TemplateContentType, cType)
}
pageURL := rsp.Header().Get("X-Page-URL")
if pageURL != "/test" {
t.Errorf("Expecting page URL %s, got %s", "/test", pageURL)
}
varyHeader := strings.Join(rsp.Header().Values("Vary"), ", ")
expectHeader := "Cookie, Accept"
if varyHeader != expectHeader {
t.Errorf("Expecting vary header [%s], got [%s]", expectHeader, varyHeader)
}
body := rsp.Body.String()
if body != "something here!" {
t.Errorf("Expecting body 'something here!', got '%s'", body)
}
}
// Test that a treetop writer implements http.ResponseWriter interface and behavior
func TestNewPartialWriter_AsResponseWriter(t *testing.T) {
request := mockRequest("/test", TemplateContentType)
rsp := httptest.NewRecorder()
if tW, ok := NewPartialWriter(rsp, request); ok {
rw, ok := tW.(http.ResponseWriter)
if !ok {
t.Error("Failed to coerce partial writer to http.ResponseWriter type")
return
}
rw.WriteHeader(http.StatusTeapot)
rw.Write([]byte("I'm a teapot!"))
} else {
t.Error("Failed to recognize template request")
return
}
if rsp.Code != 418 {
t.Errorf("Expecting status 418 got %d", rsp.Code)
}
cType := rsp.Header().Get("Content-Type")
if cType != TemplateContentType {
t.Errorf("Expecting content type %s, got %s", TemplateContentType, cType)
}
pageURL := rsp.Header().Get("X-Page-URL")
if pageURL != "/test" {
t.Errorf("Expecting page URL %s, got %s", "/test", pageURL)
}
body := rsp.Body.String()
if body != "I'm a teapot!" {
t.Errorf("Expecting body 'I'm a teapot!', got '%s'", body)
}
}
func TestNewFragmentWriter_Basic(t *testing.T) {
request := mockRequest("/test", TemplateContentType)
rsp := httptest.NewRecorder()
if tW, ok := NewFragmentWriter(rsp, request); ok {
rsp.Header().Add("Vary", "Cookie")
tW.Write([]byte("something here!"))
} else {
t.Error("Failed to recognize template request")
return
}
if rsp.Code != 200 {
t.Errorf("Expecting status 200 got %d", rsp.Code)
}
cType := rsp.Header().Get("Content-Type")
if cType != TemplateContentType {
t.Errorf("Expecting content type %s, got %s", TemplateContentType, cType)
}
pageURL := rsp.Header().Get("X-Page-URL")
if pageURL != "" {
t.Errorf("Expecting page URL not be set, got %s", pageURL)
}
varyHeader := strings.Join(rsp.Header().Values("Vary"), ", ")
expectHeader := "Cookie"
if varyHeader != expectHeader {
t.Errorf("Expecting vary header [%s], got [%s]", expectHeader, varyHeader)
}
body := rsp.Body.String()
if body != "something here!" {
t.Errorf("Expecting body 'something here!', got '%s'", body)
}
}
func TestNewFragmentWriter_AddPageURL(t *testing.T) {
request := mockRequest("/test", TemplateContentType)
rsp := httptest.NewRecorder()
if tW, ok := NewFragmentWriter(rsp, request); ok {
tW.DesignatePageURL("/test-other")
tW.Header().Add("Vary", "Cookie")
tW.Write([]byte("something here!"))
} else {
t.Error("Failed to recognize template request")
return
}
if rsp.Code != 200 {
t.Errorf("Expecting status 200 got %d", rsp.Code)
}
cType := rsp.Header().Get("Content-Type")
if cType != TemplateContentType {
t.Errorf("Expecting content type %s, got %s", TemplateContentType, cType)
}
pageURL := rsp.Header().Get("X-Page-URL")
if pageURL != "/test-other" {
t.Errorf("Expecting page URL %s, got %s", "/test-other", pageURL)
}
varyHeader := strings.Join(rsp.Header().Values("Vary"), ", ")
expectHeader := "Cookie, Accept"
if varyHeader != expectHeader {
t.Errorf("Expecting vary header [%s], got [%s]", expectHeader, varyHeader)
}
body := rsp.Body.String()
if body != "something here!" {
t.Errorf("Expecting body 'something here!', got '%s'", body)
}
}
func Test_hexEscapeNonASCII(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{
name: "all ASCII",
s: "hello world!!",
want: "hello world!!",
},
{
name: "ASCII digits",
s: "abc123",
want: "abc123",
},
{
name: "Single white star, three byte range",
s: "☆",
want: "%e2%98%86",
},
{
name: "multile characters with diaeresis",
s: "äöü",
want: "%c3%a4%c3%b6%c3%bc",
},
{
name: "Latin small letter c with acute",
s: "ć",
want: "%c4%87",
},
{
name: "ASCII special characters",
s: "@*_+-./",
want: "@*_+-./",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hexEscapeNonASCII(tt.s); got != tt.want {
t.Errorf("hexEscapeNonASCII() = %v, want %v", got, tt.want)
}
})
}
}