forked from hoisie/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_test.go
684 lines (562 loc) · 20.4 KB
/
web_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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package web
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"testing"
)
func init() {
runtime.GOMAXPROCS(4)
}
//this implements io.ReadWriteCloser, which means it can be passed around as a tcp connection
type tcpBuffer struct {
input *bytes.Buffer
output *bytes.Buffer
}
func (buf *tcpBuffer) Write(p []uint8) (n int, err error) {
return buf.output.Write(p)
}
func (buf *tcpBuffer) Read(p []byte) (n int, err error) {
return buf.input.Read(p)
}
func (buf *tcpBuffer) Close() error { return nil }
type testResponse struct {
statusCode int
status string
body string
headers map[string][]string
cookies map[string]string
}
func buildTestResponse(buf *bytes.Buffer) *testResponse {
response := testResponse{headers: make(map[string][]string), cookies: make(map[string]string)}
s := buf.String()
contents := strings.SplitN(s, "\r\n\r\n", 2)
header := contents[0]
if len(contents) > 1 {
response.body = contents[1]
}
headers := strings.Split(header, "\r\n")
statusParts := strings.SplitN(headers[0], " ", 3)
response.statusCode, _ = strconv.Atoi(statusParts[1])
for _, h := range headers[1:] {
split := strings.SplitN(h, ":", 2)
name := strings.TrimSpace(split[0])
value := strings.TrimSpace(split[1])
if _, ok := response.headers[name]; !ok {
response.headers[name] = []string{}
}
newheaders := make([]string, len(response.headers[name])+1)
copy(newheaders, response.headers[name])
newheaders[len(newheaders)-1] = value
response.headers[name] = newheaders
//if the header is a cookie, set it
if name == "Set-Cookie" {
i := strings.Index(value, ";")
cookie := value[0:i]
cookieParts := strings.SplitN(cookie, "=", 2)
response.cookies[strings.TrimSpace(cookieParts[0])] = strings.TrimSpace(cookieParts[1])
}
}
return &response
}
func getTestResponse(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *testResponse {
req := buildTestRequest(method, path, body, headers, cookies)
var buf bytes.Buffer
tcpb := tcpBuffer{nil, &buf}
c := scgiConn{wroteHeaders: false, req: req, headers: make(map[string][]string), fd: &tcpb}
mainServer.routeHandler(req, &c)
return buildTestResponse(&buf)
}
type Test struct {
method string
path string
headers map[string][]string
body string
expectedStatus int
expectedBody string
}
type StructHandler struct {
a string
}
func (s *StructHandler) method() string {
return s.a
}
func (s *StructHandler) method2(ctx *Context) string {
return s.a + ctx.Params["b"]
}
func (s *StructHandler) method3(ctx *Context, b string) string {
return s.a + b
}
//initialize the routes
func init() {
f, _ := os.OpenFile(os.DevNull, os.O_RDWR, 0644)
mainServer.SetLogger(log.New(f, "", 0))
Get("/", func() string { return "index" })
Get("/panic", func() { panic(0) })
Get("/echo/(.*)", func(s string) string { return s })
Get("/multiecho/(.*)/(.*)/(.*)/(.*)", func(a, b, c, d string) string { return a + b + c + d })
Post("/post/echo/(.*)", func(s string) string { return s })
Post("/post/echoparam/(.*)", func(ctx *Context, name string) string { return ctx.Params[name] })
Get("/error/code/(.*)", func(ctx *Context, code string) string {
n, _ := strconv.Atoi(code)
message := statusText[n]
ctx.Abort(n, message)
return ""
})
Get("/error/notfound/(.*)", func(ctx *Context, message string) { ctx.NotFound(message) })
Post("/posterror/code/(.*)/(.*)", func(ctx *Context, code string, message string) string {
n, _ := strconv.Atoi(code)
ctx.Abort(n, message)
return ""
})
Get("/writetest", func(ctx *Context) { ctx.WriteString("hello") })
Post("/securecookie/set/(.+)/(.+)", func(ctx *Context, name string, val string) string {
ctx.SetSecureCookie(name, val, 60)
return ""
})
Get("/securecookie/get/(.+)", func(ctx *Context, name string) string {
val, ok := ctx.GetSecureCookie(name)
if !ok {
return ""
}
return val
})
Get("/getparam", func(ctx *Context) string { return ctx.Params["a"] })
Get("/fullparams", func(ctx *Context) string {
return strings.Join(ctx.Request.Form["a"], ",")
})
Get("/json", func(ctx *Context) string {
ctx.ContentType("json")
data, _ := json.Marshal(ctx.Params)
return string(data)
})
Get("/jsonbytes", func(ctx *Context) []byte {
ctx.ContentType("json")
data, _ := json.Marshal(ctx.Params)
return data
})
Post("/parsejson", func(ctx *Context) string {
var tmp = struct {
A string
B string
}{}
json.NewDecoder(ctx.Request.Body).Decode(&tmp)
return tmp.A + " " + tmp.B
})
//s := &StructHandler{"a"}
//Get("/methodhandler", MethodHandler(s, "method"))
//Get("/methodhandler2", MethodHandler(s, "method2"))
//Get("/methodhandler3/(.*)", MethodHandler(s, "method3"))
}
var tests = []Test{
{"GET", "/", nil, "", 200, "index"},
{"GET", "/echo/hello", nil, "", 200, "hello"},
{"GET", "/echo/hello", nil, "", 200, "hello"},
{"GET", "/multiecho/a/b/c/d", nil, "", 200, "abcd"},
{"POST", "/post/echo/hello", nil, "", 200, "hello"},
{"POST", "/post/echo/hello", nil, "", 200, "hello"},
{"POST", "/post/echoparam/a", map[string][]string{"Content-Type": {"application/x-www-form-urlencoded"}}, "a=hello", 200, "hello"},
{"POST", "/post/echoparam/c?c=hello", nil, "", 200, "hello"},
{"POST", "/post/echoparam/a", map[string][]string{"Content-Type": {"application/x-www-form-urlencoded"}}, "a=hello\x00", 200, "hello\x00"},
//long url
{"GET", "/echo/" + strings.Repeat("0123456789", 100), nil, "", 200, strings.Repeat("0123456789", 100)},
{"GET", "/writetest", nil, "", 200, "hello"},
{"GET", "/error/notfound/notfound", nil, "", 404, "notfound"},
{"GET", "/doesnotexist", nil, "", 404, "Page not found"},
{"POST", "/doesnotexist", nil, "", 404, "Page not found"},
{"GET", "/error/code/500", nil, "", 500, statusText[500]},
{"POST", "/posterror/code/410/failedrequest", nil, "", 410, "failedrequest"},
{"GET", "/getparam?a=abcd", nil, "", 200, "abcd"},
{"GET", "/getparam?b=abcd", nil, "", 200, ""},
{"GET", "/fullparams?a=1&a=2&a=3", nil, "", 200, "1,2,3"},
{"GET", "/panic", nil, "", 500, "Server Error"},
{"GET", "/json?a=1&b=2", nil, "", 200, `{"a":"1","b":"2"}`},
{"GET", "/jsonbytes?a=1&b=2", nil, "", 200, `{"a":"1","b":"2"}`},
{"POST", "/parsejson", map[string][]string{"Content-Type": {"application/json"}}, `{"a":"hello", "b":"world"}`, 200, "hello world"},
//{"GET", "/testenv", "", 200, "hello world"},
}
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
host := "127.0.0.1"
port := "80"
rawurl := "http://" + host + ":" + port + path
url_, _ := url.Parse(rawurl)
proto := "HTTP/1.1"
if headers == nil {
headers = map[string][]string{}
}
headers["User-Agent"] = []string{"web.go test"}
if method == "POST" {
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
if headers["Content-Type"] == nil {
headers["Content-Type"] = []string{"text/plain"}
}
}
req := http.Request{Method: method,
URL: url_,
Proto: proto,
Host: host,
Header: http.Header(headers),
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
for _, cookie := range cookies {
req.AddCookie(cookie)
}
return &req
}
func TestRouting(t *testing.T) {
for _, test := range tests {
resp := getTestResponse(test.method, test.path, test.body, test.headers, nil)
if resp.statusCode != test.expectedStatus {
t.Fatalf("expected status %d got %d", test.expectedStatus, resp.statusCode)
}
if resp.body != test.expectedBody {
t.Fatalf("expected %q got %q", test.expectedBody, resp.body)
}
if cl, ok := resp.headers["Content-Length"]; ok {
clExp, _ := strconv.Atoi(cl[0])
clAct := len(resp.body)
if clExp != clAct {
t.Fatalf("Content-length doesn't match. expected %d got %d", clExp, clAct)
}
}
}
}
func TestHead(t *testing.T) {
for _, test := range tests {
if test.method != "GET" {
continue
}
getresp := getTestResponse("GET", test.path, test.body, test.headers, nil)
headresp := getTestResponse("HEAD", test.path, test.body, test.headers, nil)
if getresp.statusCode != headresp.statusCode {
t.Fatalf("head and get status differ. expected %d got %d", getresp.statusCode, headresp.statusCode)
}
if len(headresp.body) != 0 {
t.Fatalf("head request arrived with a body")
}
var cl []string
var getcl, headcl int
var hascl1, hascl2 bool
if cl, hascl1 = getresp.headers["Content-Length"]; hascl1 {
getcl, _ = strconv.Atoi(cl[0])
}
if cl, hascl2 = headresp.headers["Content-Length"]; hascl2 {
headcl, _ = strconv.Atoi(cl[0])
}
if hascl1 != hascl2 {
t.Fatalf("head and get: one has content-length, one doesn't")
}
if hascl1 == true && getcl != headcl {
t.Fatalf("head and get content-length differ")
}
}
}
func buildScgiFields(fields map[string]string, buf *bytes.Buffer) []byte {
for k, v := range fields {
buf.WriteString(k)
buf.WriteByte(0)
buf.WriteString(v)
buf.WriteByte(0)
}
return buf.Bytes()
}
func buildTestScgiRequest(method string, path string, body string, headers map[string][]string) *bytes.Buffer {
var hbuf bytes.Buffer
scgiHeaders := make(map[string]string)
hbuf.WriteString("CONTENT_LENGTH")
hbuf.WriteByte(0)
hbuf.WriteString(fmt.Sprintf("%d", len(body)))
hbuf.WriteByte(0)
scgiHeaders["REQUEST_METHOD"] = method
scgiHeaders["HTTP_HOST"] = "127.0.0.1"
scgiHeaders["REQUEST_URI"] = path
scgiHeaders["SERVER_PORT"] = "80"
scgiHeaders["SERVER_PROTOCOL"] = "HTTP/1.1"
scgiHeaders["USER_AGENT"] = "web.go test framework"
for k, v := range headers {
//Skip content-length
if k == "Content-Length" {
continue
}
key := "HTTP_" + strings.ToUpper(strings.Replace(k, "-", "_", -1))
scgiHeaders[key] = v[0]
}
buildScgiFields(scgiHeaders, &hbuf)
fielddata := hbuf.Bytes()
var buf bytes.Buffer
//extra 1 is for the comma at the end
dlen := len(fielddata) + len(body) + 1
fmt.Fprintf(&buf, "%d:", dlen)
buf.Write(fielddata)
buf.WriteByte(',')
buf.WriteString(body)
return &buf
}
func TestScgi(t *testing.T) {
for _, test := range tests {
req := buildTestScgiRequest(test.method, test.path, test.body, test.headers)
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleScgiRequest(&nb)
resp := buildTestResponse(&output)
if resp.statusCode != test.expectedStatus {
t.Fatalf("expected status %d got %d", test.expectedStatus, resp.statusCode)
}
if resp.body != test.expectedBody {
t.Fatalf("Scgi expected %q got %q", test.expectedBody, resp.body)
}
}
}
func TestScgiHead(t *testing.T) {
for _, test := range tests {
if test.method != "GET" {
continue
}
req := buildTestScgiRequest("GET", test.path, test.body, make(map[string][]string))
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleScgiRequest(&nb)
getresp := buildTestResponse(&output)
req = buildTestScgiRequest("HEAD", test.path, test.body, make(map[string][]string))
var output2 bytes.Buffer
nb = tcpBuffer{input: req, output: &output2}
mainServer.handleScgiRequest(&nb)
headresp := buildTestResponse(&output2)
if getresp.statusCode != headresp.statusCode {
t.Fatalf("head and get status differ. expected %d got %d", getresp.statusCode, headresp.statusCode)
}
if len(headresp.body) != 0 {
t.Fatalf("head request arrived with a body")
}
var cl []string
var getcl, headcl int
var hascl1, hascl2 bool
if cl, hascl1 = getresp.headers["Content-Length"]; hascl1 {
getcl, _ = strconv.Atoi(cl[0])
}
if cl, hascl2 = headresp.headers["Content-Length"]; hascl2 {
headcl, _ = strconv.Atoi(cl[0])
}
if hascl1 != hascl2 {
t.Fatalf("head and get: one has content-length, one doesn't")
}
if hascl1 == true && getcl != headcl {
t.Fatalf("head and get content-length differ")
}
}
}
func buildFcgiKeyValue(key string, val string) []byte {
var buf bytes.Buffer
if len(key) <= 127 && len(val) <= 127 {
data := struct {
A uint8
B uint8
}{uint8(len(key)), uint8(len(val))}
binary.Write(&buf, binary.BigEndian, data)
} else if len(key) <= 127 && len(val) > 127 {
data := struct {
A uint8
B uint32
}{uint8(len(key)), uint32(len(val)) | 1<<31}
binary.Write(&buf, binary.BigEndian, data)
}
buf.WriteString(key)
buf.WriteString(val)
return buf.Bytes()
}
func buildTestFcgiRequest(method string, path string, bodychunks []string, headers map[string][]string) *bytes.Buffer {
var req bytes.Buffer
fcgiHeaders := make(map[string]string)
bodylength := 0
for _, s := range bodychunks {
bodylength += len(s)
}
fcgiHeaders["CONTENT_LENGTH"] = fmt.Sprintf("%d", bodylength)
fcgiHeaders["REQUEST_METHOD"] = method
fcgiHeaders["HTTP_HOST"] = "127.0.0.1"
fcgiHeaders["REQUEST_URI"] = path
fcgiHeaders["SERVER_PORT"] = "80"
fcgiHeaders["SERVER_PROTOCOL"] = "HTTP/1.1"
fcgiHeaders["USER_AGENT"] = "web.go test framework"
for k, v := range headers {
//Skip content-length
if k == "Content-Length" {
continue
}
key := "HTTP_" + strings.ToUpper(strings.Replace(k, "-", "_", -1))
fcgiHeaders[key] = v[0]
}
// add the begin request
req.Write(newFcgiRecord(fcgiBeginRequest, 0, make([]byte, 8)))
var buf bytes.Buffer
for k, v := range fcgiHeaders {
kv := buildFcgiKeyValue(k, v)
buf.Write(kv)
}
//add the params record
req.Write(newFcgiRecord(fcgiParams, 0, buf.Bytes()))
//add the end-of-params record
req.Write(newFcgiRecord(fcgiParams, 0, []byte{}))
//send the body
for _, s := range bodychunks {
if len(s) > 0 {
req.Write(newFcgiRecord(fcgiStdin, 0, []byte(s)))
}
}
//add the end-of-stdin record
req.Write(newFcgiRecord(fcgiStdin, 0, []byte{}))
return &req
}
func getFcgiOutput(br *bytes.Buffer) *bytes.Buffer {
var output bytes.Buffer
for {
var h fcgiHeader
err := binary.Read(br, binary.BigEndian, &h)
if err == io.EOF {
break
}
content := make([]byte, h.ContentLength)
br.Read(content)
//read padding
if h.PaddingLength > 0 {
padding := make([]byte, h.PaddingLength)
br.Read(padding)
}
if h.Type == fcgiStdout {
output.Write(content)
}
}
return &output
}
func TestFcgi(t *testing.T) {
for _, test := range tests {
req := buildTestFcgiRequest(test.method, test.path, []string{test.body}, test.headers)
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleFcgiConnection(&nb)
contents := getFcgiOutput(&output)
resp := buildTestResponse(contents)
if resp.statusCode != test.expectedStatus {
t.Fatalf("expected status %d got %d", test.expectedStatus, resp.statusCode)
}
if resp.body != test.expectedBody {
t.Fatalf("Fcgi exected %q got %q", test.expectedBody, resp.body)
}
}
}
func TestFcgiHead(t *testing.T) {
for _, test := range tests {
if test.method != "GET" {
continue
}
req := buildTestFcgiRequest("GET", test.path, []string{test.body}, make(map[string][]string))
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleFcgiConnection(&nb)
contents := getFcgiOutput(&output)
getresp := buildTestResponse(contents)
req = buildTestFcgiRequest("HEAD", test.path, []string{test.body}, make(map[string][]string))
var output2 bytes.Buffer
nb = tcpBuffer{input: req, output: &output2}
mainServer.handleFcgiConnection(&nb)
contents = getFcgiOutput(&output2)
headresp := buildTestResponse(contents)
if getresp.statusCode != headresp.statusCode {
t.Fatalf("head and get status differ. expected %d got %d", getresp.statusCode, headresp.statusCode)
}
if len(headresp.body) != 0 {
t.Fatalf("head request arrived with a body")
}
var cl []string
var getcl, headcl int
var hascl1, hascl2 bool
if cl, hascl1 = getresp.headers["Content-Length"]; hascl1 {
getcl, _ = strconv.Atoi(cl[0])
}
if cl, hascl2 = headresp.headers["Content-Length"]; hascl2 {
headcl, _ = strconv.Atoi(cl[0])
}
if hascl1 != hascl2 {
t.Fatalf("head and get: one has content-length, one doesn't")
}
if hascl1 == true && getcl != headcl {
t.Fatalf("head and get content-length differ")
}
}
}
func TestFcgiChunks(t *testing.T) {
//split up an fcgi request
bodychunks := []string{`a=12&b=`, strings.Repeat("1234567890", 200)}
req := buildTestFcgiRequest("POST", "/post/echoparam/b", bodychunks, map[string][]string{"Content-Type": {"application/x-www-form-urlencoded"}})
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleFcgiConnection(&nb)
contents := getFcgiOutput(&output)
resp := buildTestResponse(contents)
if resp.body != strings.Repeat("1234567890", 200) {
t.Fatalf("Fcgi chunks test failed")
}
}
func makeCookie(vals map[string]string) []*http.Cookie {
var cookies []*http.Cookie
for k, v := range vals {
c := &http.Cookie{
Name: k,
Value: v,
}
cookies = append(cookies, c)
}
return cookies
}
func TestSecureCookie(t *testing.T) {
mainServer.Config.CookieSecret = "7C19QRmwf3mHZ9CPAaPQ0hsWeufKd"
resp1 := getTestResponse("POST", "/securecookie/set/a/1", "", nil, nil)
sval, ok := resp1.cookies["a"]
if !ok {
t.Fatalf("Failed to get cookie ")
}
cookies := makeCookie(map[string]string{"a": sval})
resp2 := getTestResponse("GET", "/securecookie/get/a", "", nil, cookies)
if resp2.body != "1" {
t.Fatalf("SecureCookie test failed")
}
}
func TestSecureCookieFcgi(t *testing.T) {
mainServer.Config.CookieSecret = "7C19QRmwf3mHZ9CPAaPQ0hsWeufKd"
//set the cookie
req := buildTestFcgiRequest("POST", "/securecookie/set/a/1", []string{}, make(map[string][]string))
var output bytes.Buffer
nb := tcpBuffer{input: req, output: &output}
mainServer.handleFcgiConnection(&nb)
contents := getFcgiOutput(&output)
resp := buildTestResponse(contents)
sval, ok := resp.cookies["a"]
if !ok {
t.Fatalf("SecureCookieFcgi test failed")
}
//send the cookie
cookie := fmt.Sprintf("a=%s", sval)
req = buildTestFcgiRequest("GET", "/securecookie/get/a", []string{}, map[string][]string{"Cookie": {cookie}})
var output2 bytes.Buffer
nb = tcpBuffer{input: req, output: &output2}
mainServer.handleFcgiConnection(&nb)
contents = getFcgiOutput(&output2)
resp = buildTestResponse(contents)
if resp.body != "1" {
t.Fatalf("SecureCookie test failed body")
}
}
func TestEarlyClose(t *testing.T) {
var server1 Server
server1.Close()
}