Source file
src/net/http/request_test.go
1
2
3
4
5 package http_test
6
7 import (
8 "bufio"
9 "bytes"
10 "context"
11 "crypto/rand"
12 "encoding/base64"
13 "errors"
14 "fmt"
15 "io"
16 "math"
17 "mime/multipart"
18 "net/http"
19 . "net/http"
20 "net/http/httptest"
21 "net/url"
22 "os"
23 "reflect"
24 "regexp"
25 "strings"
26 "testing"
27 )
28
29 func TestQuery(t *testing.T) {
30 req := &Request{Method: "GET"}
31 req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar")
32 if q := req.FormValue("q"); q != "foo" {
33 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
34 }
35 }
36
37
38
39 func TestParseFormSemicolonSeparator(t *testing.T) {
40 for _, method := range []string{"POST", "PATCH", "PUT", "GET"} {
41 req, _ := NewRequest(method, "http://www.google.com/search?q=foo;q=bar&a=1",
42 strings.NewReader("q"))
43 err := req.ParseForm()
44 if err == nil {
45 t.Fatalf(`for method %s, ParseForm expected an error, got success`, method)
46 }
47 wantForm := url.Values{"a": []string{"1"}}
48 if !reflect.DeepEqual(req.Form, wantForm) {
49 t.Fatalf("for method %s, ParseForm expected req.Form = %v, want %v", method, req.Form, wantForm)
50 }
51 }
52 }
53
54 func TestParseFormQuery(t *testing.T) {
55 req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&orphan=nope&empty=not",
56 strings.NewReader("z=post&both=y&prio=2&=nokey&orphan&empty=&"))
57 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
58
59 if q := req.FormValue("q"); q != "foo" {
60 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
61 }
62 if z := req.FormValue("z"); z != "post" {
63 t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
64 }
65 if bq, found := req.PostForm["q"]; found {
66 t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
67 }
68 if bz := req.PostFormValue("z"); bz != "post" {
69 t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
70 }
71 if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
72 t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
73 }
74 if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
75 t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
76 }
77 if prio := req.FormValue("prio"); prio != "2" {
78 t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
79 }
80 if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
81 t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
82 }
83 if empty := req.Form["empty"]; !reflect.DeepEqual(empty, []string{"", "not"}) {
84 t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
85 }
86 if nokey := req.Form[""]; !reflect.DeepEqual(nokey, []string{"nokey"}) {
87 t.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey)
88 }
89 }
90
91
92 func TestParseFormQueryMethods(t *testing.T) {
93 for _, method := range []string{"POST", "PATCH", "PUT", "FOO"} {
94 req, _ := NewRequest(method, "http://www.google.com/search",
95 strings.NewReader("foo=bar"))
96 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
97 want := "bar"
98 if method == "FOO" {
99 want = ""
100 }
101 if got := req.FormValue("foo"); got != want {
102 t.Errorf(`for method %s, FormValue("foo") = %q; want %q`, method, got, want)
103 }
104 }
105 }
106
107 func TestParseFormUnknownContentType(t *testing.T) {
108 for _, test := range []struct {
109 name string
110 wantErr string
111 contentType Header
112 }{
113 {"text", "", Header{"Content-Type": {"text/plain"}}},
114
115
116 {"empty", "", Header{}},
117 {"boundary", "mime: invalid media parameter", Header{"Content-Type": {"text/plain; boundary="}}},
118 {"unknown", "", Header{"Content-Type": {"application/unknown"}}},
119 } {
120 t.Run(test.name,
121 func(t *testing.T) {
122 req := &Request{
123 Method: "POST",
124 Header: test.contentType,
125 Body: io.NopCloser(strings.NewReader("body")),
126 }
127 err := req.ParseForm()
128 switch {
129 case err == nil && test.wantErr != "":
130 t.Errorf("unexpected success; want error %q", test.wantErr)
131 case err != nil && test.wantErr == "":
132 t.Errorf("want success, got error: %v", err)
133 case test.wantErr != "" && test.wantErr != fmt.Sprint(err):
134 t.Errorf("got error %q; want %q", err, test.wantErr)
135 }
136 },
137 )
138 }
139 }
140
141 func TestParseFormInitializeOnError(t *testing.T) {
142 nilBody, _ := NewRequest("POST", "http://www.google.com/search?q=foo", nil)
143 tests := []*Request{
144 nilBody,
145 {Method: "GET", URL: nil},
146 }
147 for i, req := range tests {
148 err := req.ParseForm()
149 if req.Form == nil {
150 t.Errorf("%d. Form not initialized, error %v", i, err)
151 }
152 if req.PostForm == nil {
153 t.Errorf("%d. PostForm not initialized, error %v", i, err)
154 }
155 }
156 }
157
158 func TestMultipartReader(t *testing.T) {
159 tests := []struct {
160 shouldError bool
161 contentType string
162 }{
163 {false, `multipart/form-data; boundary="foo123"`},
164 {false, `multipart/mixed; boundary="foo123"`},
165 {true, `text/plain`},
166 }
167
168 for i, test := range tests {
169 req := &Request{
170 Method: "POST",
171 Header: Header{"Content-Type": {test.contentType}},
172 Body: io.NopCloser(new(bytes.Buffer)),
173 }
174 multipart, err := req.MultipartReader()
175 if test.shouldError {
176 if err == nil || multipart != nil {
177 t.Errorf("test %d: unexpectedly got nil-error (%v) or non-nil-multipart (%v)", i, err, multipart)
178 }
179 continue
180 }
181 if err != nil || multipart == nil {
182 t.Errorf("test %d: unexpectedly got error (%v) or nil-multipart (%v)", i, err, multipart)
183 }
184 }
185 }
186
187
188 func TestParseMultipartFormPopulatesPostForm(t *testing.T) {
189 postData :=
190 `--xxx
191 Content-Disposition: form-data; name="field1"
192
193 value1
194 --xxx
195 Content-Disposition: form-data; name="field2"
196
197 value2
198 --xxx
199 Content-Disposition: form-data; name="file"; filename="file"
200 Content-Type: application/octet-stream
201 Content-Transfer-Encoding: binary
202
203 binary data
204 --xxx--
205 `
206 req := &Request{
207 Method: "POST",
208 Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
209 Body: io.NopCloser(strings.NewReader(postData)),
210 }
211
212 initialFormItems := map[string]string{
213 "language": "Go",
214 "name": "gopher",
215 "skill": "go-ing",
216 "field2": "initial-value2",
217 }
218
219 req.Form = make(url.Values)
220 for k, v := range initialFormItems {
221 req.Form.Add(k, v)
222 }
223
224 err := req.ParseMultipartForm(10000)
225 if err != nil {
226 t.Fatalf("unexpected multipart error %v", err)
227 }
228
229 wantForm := url.Values{
230 "language": []string{"Go"},
231 "name": []string{"gopher"},
232 "skill": []string{"go-ing"},
233 "field1": []string{"value1"},
234 "field2": []string{"initial-value2", "value2"},
235 }
236 if !reflect.DeepEqual(req.Form, wantForm) {
237 t.Fatalf("req.Form = %v, want %v", req.Form, wantForm)
238 }
239
240 wantPostForm := url.Values{
241 "field1": []string{"value1"},
242 "field2": []string{"value2"},
243 }
244 if !reflect.DeepEqual(req.PostForm, wantPostForm) {
245 t.Fatalf("req.PostForm = %v, want %v", req.PostForm, wantPostForm)
246 }
247 }
248
249 func TestParseMultipartForm(t *testing.T) {
250 req := &Request{
251 Method: "POST",
252 Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
253 Body: io.NopCloser(new(bytes.Buffer)),
254 }
255 err := req.ParseMultipartForm(25)
256 if err == nil {
257 t.Error("expected multipart EOF, got nil")
258 }
259
260 req.Header = Header{"Content-Type": {"text/plain"}}
261 err = req.ParseMultipartForm(25)
262 if err != ErrNotMultipart {
263 t.Error("expected ErrNotMultipart for text/plain")
264 }
265 }
266
267
268 func TestParseMultipartFormFilename(t *testing.T) {
269 postData :=
270 `--xxx
271 Content-Disposition: form-data; name="file"; filename="../usr/foobar.txt/"
272 Content-Type: text/plain
273
274 --xxx--
275 `
276 req := &Request{
277 Method: "POST",
278 Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
279 Body: io.NopCloser(strings.NewReader(postData)),
280 }
281 _, hdr, err := req.FormFile("file")
282 if err != nil {
283 t.Fatal(err)
284 }
285 if hdr.Filename != "foobar.txt" {
286 t.Errorf("expected only the last element of the path, got %q", hdr.Filename)
287 }
288 }
289
290
291
292
293 func TestMaxInt64ForMultipartFormMaxMemoryOverflow(t *testing.T) {
294 run(t, testMaxInt64ForMultipartFormMaxMemoryOverflow)
295 }
296 func testMaxInt64ForMultipartFormMaxMemoryOverflow(t *testing.T, mode testMode) {
297 payloadSize := 1 << 10
298 cst := newClientServerTest(t, mode, HandlerFunc(func(rw ResponseWriter, req *Request) {
299
300
301
302 if err := req.ParseMultipartForm(math.MaxInt64); err != nil {
303 Error(rw, err.Error(), StatusBadRequest)
304 return
305 }
306 })).ts
307 fBuf := new(bytes.Buffer)
308 mw := multipart.NewWriter(fBuf)
309 mf, err := mw.CreateFormFile("file", "myfile.txt")
310 if err != nil {
311 t.Fatal(err)
312 }
313 if _, err := mf.Write(bytes.Repeat([]byte("abc"), payloadSize)); err != nil {
314 t.Fatal(err)
315 }
316 if err := mw.Close(); err != nil {
317 t.Fatal(err)
318 }
319 req, err := NewRequest("POST", cst.URL, fBuf)
320 if err != nil {
321 t.Fatal(err)
322 }
323 req.Header.Set("Content-Type", mw.FormDataContentType())
324 res, err := cst.Client().Do(req)
325 if err != nil {
326 t.Fatal(err)
327 }
328 res.Body.Close()
329 if g, w := res.StatusCode, StatusOK; g != w {
330 t.Fatalf("Status code mismatch: got %d, want %d", g, w)
331 }
332 }
333
334 func TestRequestRedirect(t *testing.T) { run(t, testRequestRedirect) }
335 func testRequestRedirect(t *testing.T, mode testMode) {
336 cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
337 switch r.URL.Path {
338 case "/":
339 w.Header().Set("Location", "/foo/")
340 w.WriteHeader(StatusSeeOther)
341 case "/foo/":
342 fmt.Fprintf(w, "foo")
343 default:
344 w.WriteHeader(StatusBadRequest)
345 }
346 }))
347
348 var end = regexp.MustCompile("/foo/$")
349 r, err := cst.c.Get(cst.ts.URL)
350 if err != nil {
351 t.Fatal(err)
352 }
353 r.Body.Close()
354 url := r.Request.URL.String()
355 if r.StatusCode != 200 || !end.MatchString(url) {
356 t.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r.StatusCode, url)
357 }
358 }
359
360 func TestSetBasicAuth(t *testing.T) {
361 r, _ := NewRequest("GET", "http://example.com/", nil)
362 r.SetBasicAuth("Aladdin", "open sesame")
363 if g, e := r.Header.Get("Authorization"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; g != e {
364 t.Errorf("got header %q, want %q", g, e)
365 }
366 }
367
368 func TestMultipartRequest(t *testing.T) {
369
370
371
372 req := newTestMultipartRequest(t)
373 if err := req.ParseMultipartForm(25); err != nil {
374 t.Fatal("ParseMultipartForm first call:", err)
375 }
376 defer req.MultipartForm.RemoveAll()
377 validateTestMultipartContents(t, req, false)
378 if err := req.ParseMultipartForm(25); err != nil {
379 t.Fatal("ParseMultipartForm second call:", err)
380 }
381 validateTestMultipartContents(t, req, false)
382 }
383
384
385
386 func TestParseMultipartFormSemicolonSeparator(t *testing.T) {
387 req := newTestMultipartRequest(t)
388 req.URL = &url.URL{RawQuery: "q=foo;q=bar"}
389 if err := req.ParseMultipartForm(25); err == nil {
390 t.Fatal("ParseMultipartForm expected error due to invalid semicolon, got nil")
391 }
392 defer req.MultipartForm.RemoveAll()
393 validateTestMultipartContents(t, req, false)
394 }
395
396 func TestMultipartRequestAuto(t *testing.T) {
397
398
399 req := newTestMultipartRequest(t)
400 defer func() {
401 if req.MultipartForm != nil {
402 req.MultipartForm.RemoveAll()
403 }
404 }()
405 validateTestMultipartContents(t, req, true)
406 }
407
408 func TestMissingFileMultipartRequest(t *testing.T) {
409
410
411 req := newTestMultipartRequest(t)
412 testMissingFile(t, req)
413 }
414
415
416 func TestFormValueCallsParseMultipartForm(t *testing.T) {
417 req, _ := NewRequest("POST", "http://www.google.com/", strings.NewReader("z=post"))
418 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
419 if req.Form != nil {
420 t.Fatal("Unexpected request Form, want nil")
421 }
422 req.FormValue("z")
423 if req.Form == nil {
424 t.Fatal("ParseMultipartForm not called by FormValue")
425 }
426 }
427
428
429 func TestFormFileCallsParseMultipartForm(t *testing.T) {
430 req := newTestMultipartRequest(t)
431 if req.Form != nil {
432 t.Fatal("Unexpected request Form, want nil")
433 }
434 req.FormFile("")
435 if req.Form == nil {
436 t.Fatal("ParseMultipartForm not called by FormFile")
437 }
438 }
439
440
441
442 func TestParseMultipartFormOrder(t *testing.T) {
443 req := newTestMultipartRequest(t)
444 if _, err := req.MultipartReader(); err != nil {
445 t.Fatalf("MultipartReader: %v", err)
446 }
447 if err := req.ParseMultipartForm(1024); err == nil {
448 t.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
449 }
450 }
451
452
453
454 func TestMultipartReaderOrder(t *testing.T) {
455 req := newTestMultipartRequest(t)
456 if err := req.ParseMultipartForm(25); err != nil {
457 t.Fatalf("ParseMultipartForm: %v", err)
458 }
459 defer req.MultipartForm.RemoveAll()
460 if _, err := req.MultipartReader(); err == nil {
461 t.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
462 }
463 }
464
465
466
467 func TestFormFileOrder(t *testing.T) {
468 req := newTestMultipartRequest(t)
469 if _, err := req.MultipartReader(); err != nil {
470 t.Fatalf("MultipartReader: %v", err)
471 }
472 if _, _, err := req.FormFile(""); err == nil {
473 t.Fatal("expected an error from FormFile after call to MultipartReader")
474 }
475 }
476
477 var readRequestErrorTests = []struct {
478 in string
479 err string
480
481 header Header
482 }{
483 0: {"GET / HTTP/1.1\r\nheader:foo\r\n\r\n", "", Header{"Header": {"foo"}}},
484 1: {"GET / HTTP/1.1\r\nheader:foo\r\n", io.ErrUnexpectedEOF.Error(), nil},
485 2: {"", io.EOF.Error(), nil},
486 3: {
487 in: "HEAD / HTTP/1.1\r\n\r\n",
488 header: Header{},
489 },
490
491
492
493
494 4: {
495 in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\nGopher hey\r\n",
496 err: "cannot contain multiple Content-Length headers",
497 },
498 5: {
499 in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\nGopher\r\n",
500 err: "cannot contain multiple Content-Length headers",
501 },
502 6: {
503 in: "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
504 err: "",
505 header: Header{"Content-Length": {"6"}},
506 },
507 7: {
508 in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
509 err: "cannot contain multiple Content-Length headers",
510 },
511 8: {
512 in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
513 err: "cannot contain multiple Content-Length headers",
514 },
515 9: {
516 in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
517 header: Header{"Content-Length": {"0"}},
518 },
519 10: {
520 in: "HEAD / HTTP/1.1\r\nHost: foo\r\nHost: bar\r\n\r\n\r\n\r\n",
521 err: "too many Host headers",
522 },
523 }
524
525 func TestReadRequestErrors(t *testing.T) {
526 for i, tt := range readRequestErrorTests {
527 req, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.in)))
528 if err == nil {
529 if tt.err != "" {
530 t.Errorf("#%d: got nil err; want %q", i, tt.err)
531 }
532
533 if !reflect.DeepEqual(tt.header, req.Header) {
534 t.Errorf("#%d: gotHeader: %q wantHeader: %q", i, req.Header, tt.header)
535 }
536 continue
537 }
538
539 if tt.err == "" || !strings.Contains(err.Error(), tt.err) {
540 t.Errorf("%d: got error = %v; want %v", i, err, tt.err)
541 }
542 }
543 }
544
545 var newRequestHostTests = []struct {
546 in, out string
547 }{
548 {"http://www.example.com/", "www.example.com"},
549 {"http://www.example.com:8080/", "www.example.com:8080"},
550
551 {"http://192.168.0.1/", "192.168.0.1"},
552 {"http://192.168.0.1:8080/", "192.168.0.1:8080"},
553 {"http://192.168.0.1:/", "192.168.0.1"},
554
555 {"http://[fe80::1]/", "[fe80::1]"},
556 {"http://[fe80::1]:8080/", "[fe80::1]:8080"},
557 {"http://[fe80::1%25en0]/", "[fe80::1%en0]"},
558 {"http://[fe80::1%25en0]:8080/", "[fe80::1%en0]:8080"},
559 {"http://[fe80::1%25en0]:/", "[fe80::1%en0]"},
560 }
561
562 func TestNewRequestHost(t *testing.T) {
563 for i, tt := range newRequestHostTests {
564 req, err := NewRequest("GET", tt.in, nil)
565 if err != nil {
566 t.Errorf("#%v: %v", i, err)
567 continue
568 }
569 if req.Host != tt.out {
570 t.Errorf("got %q; want %q", req.Host, tt.out)
571 }
572 }
573 }
574
575 func TestRequestInvalidMethod(t *testing.T) {
576 _, err := NewRequest("bad method", "http://foo.com/", nil)
577 if err == nil {
578 t.Error("expected error from NewRequest with invalid method")
579 }
580 req, err := NewRequest("GET", "http://foo.example/", nil)
581 if err != nil {
582 t.Fatal(err)
583 }
584 req.Method = "bad method"
585 _, err = DefaultClient.Do(req)
586 if err == nil || !strings.Contains(err.Error(), "invalid method") {
587 t.Errorf("Transport error = %v; want invalid method", err)
588 }
589
590 req, err = NewRequest("", "http://foo.com/", nil)
591 if err != nil {
592 t.Errorf("NewRequest(empty method) = %v; want nil", err)
593 } else if req.Method != "GET" {
594 t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method)
595 }
596 }
597
598 func TestNewRequestContentLength(t *testing.T) {
599 readByte := func(r io.Reader) io.Reader {
600 var b [1]byte
601 r.Read(b[:])
602 return r
603 }
604 tests := []struct {
605 r io.Reader
606 want int64
607 }{
608 {bytes.NewReader([]byte("123")), 3},
609 {bytes.NewBuffer([]byte("1234")), 4},
610 {strings.NewReader("12345"), 5},
611 {strings.NewReader(""), 0},
612 {NoBody, 0},
613
614
615
616
617 {struct{ io.Reader }{strings.NewReader("xyz")}, 0},
618 {io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
619 {readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
620 }
621 for i, tt := range tests {
622 req, err := NewRequest("POST", "http://localhost/", tt.r)
623 if err != nil {
624 t.Fatal(err)
625 }
626 if req.ContentLength != tt.want {
627 t.Errorf("test[%d]: ContentLength(%T) = %d; want %d", i, tt.r, req.ContentLength, tt.want)
628 }
629 }
630 }
631
632 var parseHTTPVersionTests = []struct {
633 vers string
634 major, minor int
635 ok bool
636 }{
637 {"HTTP/0.0", 0, 0, true},
638 {"HTTP/0.9", 0, 9, true},
639 {"HTTP/1.0", 1, 0, true},
640 {"HTTP/1.1", 1, 1, true},
641
642 {"HTTP", 0, 0, false},
643 {"HTTP/one.one", 0, 0, false},
644 {"HTTP/1.1/", 0, 0, false},
645 {"HTTP/-1,0", 0, 0, false},
646 {"HTTP/0,-1", 0, 0, false},
647 {"HTTP/", 0, 0, false},
648 {"HTTP/1,1", 0, 0, false},
649 {"HTTP/+1.1", 0, 0, false},
650 {"HTTP/1.+1", 0, 0, false},
651 {"HTTP/0000000001.1", 0, 0, false},
652 {"HTTP/1.0000000001", 0, 0, false},
653 {"HTTP/3.14", 0, 0, false},
654 {"HTTP/12.3", 0, 0, false},
655 }
656
657 func TestParseHTTPVersion(t *testing.T) {
658 for _, tt := range parseHTTPVersionTests {
659 major, minor, ok := ParseHTTPVersion(tt.vers)
660 if ok != tt.ok || major != tt.major || minor != tt.minor {
661 type version struct {
662 major, minor int
663 ok bool
664 }
665 t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
666 }
667 }
668 }
669
670 type getBasicAuthTest struct {
671 username, password string
672 ok bool
673 }
674
675 type basicAuthCredentialsTest struct {
676 username, password string
677 }
678
679 var getBasicAuthTests = []struct {
680 username, password string
681 ok bool
682 }{
683 {"Aladdin", "open sesame", true},
684 {"Aladdin", "open:sesame", true},
685 {"", "", true},
686 }
687
688 func TestGetBasicAuth(t *testing.T) {
689 for _, tt := range getBasicAuthTests {
690 r, _ := NewRequest("GET", "http://example.com/", nil)
691 r.SetBasicAuth(tt.username, tt.password)
692 username, password, ok := r.BasicAuth()
693 if ok != tt.ok || username != tt.username || password != tt.password {
694 t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
695 getBasicAuthTest{tt.username, tt.password, tt.ok})
696 }
697 }
698
699 r, _ := NewRequest("GET", "http://example.com/", nil)
700 username, password, ok := r.BasicAuth()
701 if ok {
702 t.Errorf("expected false from BasicAuth when the request is unauthenticated")
703 }
704 want := basicAuthCredentialsTest{"", ""}
705 if username != want.username || password != want.password {
706 t.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
707 want, basicAuthCredentialsTest{username, password})
708 }
709 }
710
711 var parseBasicAuthTests = []struct {
712 header, username, password string
713 ok bool
714 }{
715 {"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
716
717
718 {"BASIC " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
719 {"basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
720
721 {"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
722 {"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
723 {"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
724 {base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
725 {"Basic ", "", "", false},
726 {"Basic Aladdin:open sesame", "", "", false},
727 {`Digest username="Aladdin"`, "", "", false},
728 }
729
730 func TestParseBasicAuth(t *testing.T) {
731 for _, tt := range parseBasicAuthTests {
732 r, _ := NewRequest("GET", "http://example.com/", nil)
733 r.Header.Set("Authorization", tt.header)
734 username, password, ok := r.BasicAuth()
735 if ok != tt.ok || username != tt.username || password != tt.password {
736 t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
737 getBasicAuthTest{tt.username, tt.password, tt.ok})
738 }
739 }
740 }
741
742 type logWrites struct {
743 t *testing.T
744 dst *[]string
745 }
746
747 func (l logWrites) WriteByte(c byte) error {
748 l.t.Fatalf("unexpected WriteByte call")
749 return nil
750 }
751
752 func (l logWrites) Write(p []byte) (n int, err error) {
753 *l.dst = append(*l.dst, string(p))
754 return len(p), nil
755 }
756
757 func TestRequestWriteBufferedWriter(t *testing.T) {
758 got := []string{}
759 req, _ := NewRequest("GET", "http://foo.com/", nil)
760 req.Write(logWrites{t, &got})
761 want := []string{
762 "GET / HTTP/1.1\r\n",
763 "Host: foo.com\r\n",
764 "User-Agent: " + DefaultUserAgent + "\r\n",
765 "\r\n",
766 }
767 if !reflect.DeepEqual(got, want) {
768 t.Errorf("Writes = %q\n Want = %q", got, want)
769 }
770 }
771
772 func TestRequestBadHostHeader(t *testing.T) {
773 got := []string{}
774 req, err := NewRequest("GET", "http://foo/after", nil)
775 if err != nil {
776 t.Fatal(err)
777 }
778 req.Host = "foo.com\nnewline"
779 req.URL.Host = "foo.com\nnewline"
780 req.Write(logWrites{t, &got})
781 want := []string{
782 "GET /after HTTP/1.1\r\n",
783 "Host: \r\n",
784 "User-Agent: " + DefaultUserAgent + "\r\n",
785 "\r\n",
786 }
787 if !reflect.DeepEqual(got, want) {
788 t.Errorf("Writes = %q\n Want = %q", got, want)
789 }
790 }
791
792 func TestRequestBadUserAgent(t *testing.T) {
793 got := []string{}
794 req, err := NewRequest("GET", "http://foo/after", nil)
795 if err != nil {
796 t.Fatal(err)
797 }
798 req.Header.Set("User-Agent", "evil\r\nX-Evil: evil")
799 req.Write(logWrites{t, &got})
800 want := []string{
801 "GET /after HTTP/1.1\r\n",
802 "Host: foo\r\n",
803 "User-Agent: evil X-Evil: evil\r\n",
804 "\r\n",
805 }
806 if !reflect.DeepEqual(got, want) {
807 t.Errorf("Writes = %q\n Want = %q", got, want)
808 }
809 }
810
811 func TestStarRequest(t *testing.T) {
812 req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
813 if err != nil {
814 return
815 }
816 if req.ContentLength != 0 {
817 t.Errorf("ContentLength = %d; want 0", req.ContentLength)
818 }
819 if req.Body == nil {
820 t.Errorf("Body = nil; want non-nil")
821 }
822
823
824
825
826
827
828
829 clientReq := *req
830 clientReq.Body = nil
831
832 var out strings.Builder
833 if err := clientReq.Write(&out); err != nil {
834 t.Fatal(err)
835 }
836
837 if strings.Contains(out.String(), "chunked") {
838 t.Error("wrote chunked request; want no body")
839 }
840 back, err := ReadRequest(bufio.NewReader(strings.NewReader(out.String())))
841 if err != nil {
842 t.Fatal(err)
843 }
844
845
846 req.Header = nil
847 back.Header = nil
848 if !reflect.DeepEqual(req, back) {
849 t.Errorf("Original request doesn't match Request read back.")
850 t.Logf("Original: %#v", req)
851 t.Logf("Original.URL: %#v", req.URL)
852 t.Logf("Wrote: %s", out.String())
853 t.Logf("Read back (doesn't match Original): %#v", back)
854 }
855 }
856
857 type responseWriterJustWriter struct {
858 io.Writer
859 }
860
861 func (responseWriterJustWriter) Header() Header { panic("should not be called") }
862 func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
863
864
865
866 type delayedEOFReader struct {
867 r io.Reader
868 }
869
870 func (dr delayedEOFReader) Read(p []byte) (n int, err error) {
871 n, err = dr.r.Read(p)
872 if n > 0 && err == io.EOF {
873 err = nil
874 }
875 return
876 }
877
878 func TestIssue10884_MaxBytesEOF(t *testing.T) {
879 dst := io.Discard
880 _, err := io.Copy(dst, MaxBytesReader(
881 responseWriterJustWriter{dst},
882 io.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
883 5))
884 if err != nil {
885 t.Fatal(err)
886 }
887 }
888
889
890
891 func TestMaxBytesReaderStickyError(t *testing.T) {
892 isSticky := func(r io.Reader) error {
893 var log bytes.Buffer
894 buf := make([]byte, 1000)
895 var firstErr error
896 for {
897 n, err := r.Read(buf)
898 fmt.Fprintf(&log, "Read(%d) = %d, %v\n", len(buf), n, err)
899 if err == nil {
900 continue
901 }
902 if firstErr == nil {
903 firstErr = err
904 continue
905 }
906 if !reflect.DeepEqual(err, firstErr) {
907 return fmt.Errorf("non-sticky error. got log:\n%s", log.Bytes())
908 }
909 t.Logf("Got log: %s", log.Bytes())
910 return nil
911 }
912 }
913 tests := [...]struct {
914 readable int
915 limit int64
916 }{
917 0: {99, 100},
918 1: {100, 100},
919 2: {101, 100},
920 }
921 for i, tt := range tests {
922 rc := MaxBytesReader(nil, io.NopCloser(bytes.NewReader(make([]byte, tt.readable))), tt.limit)
923 if err := isSticky(rc); err != nil {
924 t.Errorf("%d. error: %v", i, err)
925 }
926 }
927 }
928
929
930
931 func TestMaxBytesReaderDifferentLimits(t *testing.T) {
932 const testStr = "1234"
933 tests := [...]struct {
934 limit int64
935 lenP int
936 wantN int
937 wantErr bool
938 }{
939 0: {
940 limit: -123,
941 lenP: 0,
942 wantN: 0,
943 wantErr: false,
944 },
945 1: {
946 limit: -100,
947 lenP: 32 * 1024,
948 wantN: 0,
949 wantErr: true,
950 },
951 2: {
952 limit: -2,
953 lenP: 1,
954 wantN: 0,
955 wantErr: true,
956 },
957 3: {
958 limit: -1,
959 lenP: 2,
960 wantN: 0,
961 wantErr: true,
962 },
963 4: {
964 limit: 0,
965 lenP: 3,
966 wantN: 0,
967 wantErr: true,
968 },
969 5: {
970 limit: 1,
971 lenP: 4,
972 wantN: 1,
973 wantErr: true,
974 },
975 6: {
976 limit: 2,
977 lenP: 5,
978 wantN: 2,
979 wantErr: true,
980 },
981 7: {
982 limit: 3,
983 lenP: 2,
984 wantN: 2,
985 wantErr: false,
986 },
987 8: {
988 limit: int64(len(testStr)),
989 lenP: len(testStr),
990 wantN: len(testStr),
991 wantErr: false,
992 },
993 9: {
994 limit: 100,
995 lenP: 6,
996 wantN: len(testStr),
997 wantErr: false,
998 },
999 10: {
1000 limit: int64(1<<63 - 1),
1001 lenP: len(testStr),
1002 wantN: len(testStr),
1003 wantErr: false,
1004 },
1005 }
1006 for i, tt := range tests {
1007 rc := MaxBytesReader(nil, io.NopCloser(strings.NewReader(testStr)), tt.limit)
1008
1009 n, err := rc.Read(make([]byte, tt.lenP))
1010
1011 if n != tt.wantN {
1012 t.Errorf("%d. n: %d, want n: %d", i, n, tt.wantN)
1013 }
1014
1015 if (err != nil) != tt.wantErr {
1016 t.Errorf("%d. error: %v", i, err)
1017 }
1018 }
1019 }
1020
1021 func TestWithContextNilURL(t *testing.T) {
1022 req, err := NewRequest("POST", "https://golang.org/", nil)
1023 if err != nil {
1024 t.Fatal(err)
1025 }
1026
1027
1028 req.URL = nil
1029 reqCopy := req.WithContext(context.Background())
1030 if reqCopy.URL != nil {
1031 t.Error("expected nil URL in cloned request")
1032 }
1033 }
1034
1035
1036
1037 func TestRequestCloneTransferEncoding(t *testing.T) {
1038 body := strings.NewReader("body")
1039 req, _ := NewRequest("POST", "https://example.org/", body)
1040 req.TransferEncoding = []string{
1041 "encoding1",
1042 }
1043
1044 clonedReq := req.Clone(context.Background())
1045
1046 req.TransferEncoding[0] = "encoding2"
1047
1048 if req.TransferEncoding[0] != "encoding2" {
1049 t.Error("expected req.TransferEncoding to be changed")
1050 }
1051 if clonedReq.TransferEncoding[0] != "encoding1" {
1052 t.Error("expected clonedReq.TransferEncoding to be unchanged")
1053 }
1054 }
1055
1056
1057
1058 func TestRequestClonePathValue(t *testing.T) {
1059 req, _ := http.NewRequest("GET", "https://example.org/", nil)
1060 req.SetPathValue("p1", "orig")
1061
1062 clonedReq := req.Clone(context.Background())
1063 clonedReq.SetPathValue("p2", "copy")
1064
1065
1066
1067 if g, w := req.PathValue("p2"), ""; g != w {
1068 t.Fatalf("p2 mismatch got %q, want %q", g, w)
1069 }
1070 if g, w := req.PathValue("p1"), "orig"; g != w {
1071 t.Fatalf("p1 mismatch got %q, want %q", g, w)
1072 }
1073
1074
1075 if g, w := clonedReq.PathValue("p1"), "orig"; g != w {
1076 t.Fatalf("p1 mismatch got %q, want %q", g, w)
1077 }
1078 if g, w := clonedReq.PathValue("p2"), "copy"; g != w {
1079 t.Fatalf("p2 mismatch got %q, want %q", g, w)
1080 }
1081 }
1082
1083
1084 func TestNoPanicOnRoundTripWithBasicAuth(t *testing.T) { run(t, testNoPanicWithBasicAuth) }
1085 func testNoPanicWithBasicAuth(t *testing.T, mode testMode) {
1086 cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {}))
1087
1088 u, err := url.Parse(cst.ts.URL)
1089 if err != nil {
1090 t.Fatal(err)
1091 }
1092 u.User = url.UserPassword("foo", "bar")
1093 req := &Request{
1094 URL: u,
1095 Method: "GET",
1096 }
1097 if _, err := cst.c.Do(req); err != nil {
1098 t.Fatalf("Unexpected error: %v", err)
1099 }
1100 }
1101
1102
1103 func TestNewRequestGetBody(t *testing.T) {
1104 tests := []struct {
1105 r io.Reader
1106 }{
1107 {r: strings.NewReader("hello")},
1108 {r: bytes.NewReader([]byte("hello"))},
1109 {r: bytes.NewBuffer([]byte("hello"))},
1110 }
1111 for i, tt := range tests {
1112 req, err := NewRequest("POST", "http://foo.tld/", tt.r)
1113 if err != nil {
1114 t.Errorf("test[%d]: %v", i, err)
1115 continue
1116 }
1117 if req.Body == nil {
1118 t.Errorf("test[%d]: Body = nil", i)
1119 continue
1120 }
1121 if req.GetBody == nil {
1122 t.Errorf("test[%d]: GetBody = nil", i)
1123 continue
1124 }
1125 slurp1, err := io.ReadAll(req.Body)
1126 if err != nil {
1127 t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
1128 }
1129 newBody, err := req.GetBody()
1130 if err != nil {
1131 t.Errorf("test[%d]: GetBody = %v", i, err)
1132 }
1133 slurp2, err := io.ReadAll(newBody)
1134 if err != nil {
1135 t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
1136 }
1137 if string(slurp1) != string(slurp2) {
1138 t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
1139 }
1140 }
1141 }
1142
1143 func testMissingFile(t *testing.T, req *Request) {
1144 f, fh, err := req.FormFile("missing")
1145 if f != nil {
1146 t.Errorf("FormFile file = %v, want nil", f)
1147 }
1148 if fh != nil {
1149 t.Errorf("FormFile file header = %v, want nil", fh)
1150 }
1151 if err != ErrMissingFile {
1152 t.Errorf("FormFile err = %q, want ErrMissingFile", err)
1153 }
1154 }
1155
1156 func newTestMultipartRequest(t *testing.T) *Request {
1157 b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
1158 req, err := NewRequest("POST", "/", b)
1159 if err != nil {
1160 t.Fatal("NewRequest:", err)
1161 }
1162 ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
1163 req.Header.Set("Content-type", ctype)
1164 return req
1165 }
1166
1167 func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
1168 if g, e := req.FormValue("texta"), textaValue; g != e {
1169 t.Errorf("texta value = %q, want %q", g, e)
1170 }
1171 if g, e := req.FormValue("textb"), textbValue; g != e {
1172 t.Errorf("textb value = %q, want %q", g, e)
1173 }
1174 if g := req.FormValue("missing"); g != "" {
1175 t.Errorf("missing value = %q, want empty string", g)
1176 }
1177
1178 assertMem := func(n string, fd multipart.File) {
1179 if _, ok := fd.(*os.File); ok {
1180 t.Error(n, " is *os.File, should not be")
1181 }
1182 }
1183 fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
1184 defer fda.Close()
1185 assertMem("filea", fda)
1186 fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
1187 defer fdb.Close()
1188 if allMem {
1189 assertMem("fileb", fdb)
1190 } else {
1191 if _, ok := fdb.(*os.File); !ok {
1192 t.Errorf("fileb has unexpected underlying type %T", fdb)
1193 }
1194 }
1195
1196 testMissingFile(t, req)
1197 }
1198
1199 func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
1200 f, fh, err := req.FormFile(key)
1201 if err != nil {
1202 t.Fatalf("FormFile(%q): %q", key, err)
1203 }
1204 if fh.Filename != expectFilename {
1205 t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
1206 }
1207 var b strings.Builder
1208 _, err = io.Copy(&b, f)
1209 if err != nil {
1210 t.Fatal("copying contents:", err)
1211 }
1212 if g := b.String(); g != expectContent {
1213 t.Errorf("contents = %q, want %q", g, expectContent)
1214 }
1215 return f
1216 }
1217
1218
1219
1220 func TestRequestCookie(t *testing.T) {
1221 for _, tt := range []struct {
1222 name string
1223 value string
1224 expectedErr error
1225 }{
1226 {
1227 name: "foo",
1228 value: "bar",
1229 expectedErr: nil,
1230 },
1231 {
1232 name: "",
1233 expectedErr: ErrNoCookie,
1234 },
1235 } {
1236 req, err := NewRequest("GET", "http://example.com/", nil)
1237 if err != nil {
1238 t.Fatal(err)
1239 }
1240 req.AddCookie(&Cookie{Name: tt.name, Value: tt.value})
1241 c, err := req.Cookie(tt.name)
1242 if err != tt.expectedErr {
1243 t.Errorf("got %v, want %v", err, tt.expectedErr)
1244 }
1245
1246
1247 if err != nil {
1248 continue
1249 }
1250 if c.Value != tt.value {
1251 t.Errorf("got %v, want %v", c.Value, tt.value)
1252 }
1253 if c.Name != tt.name {
1254 t.Errorf("got %s, want %v", tt.name, c.Name)
1255 }
1256 }
1257 }
1258
1259 const (
1260 fileaContents = "This is a test file."
1261 filebContents = "Another test file."
1262 textaValue = "foo"
1263 textbValue = "bar"
1264 boundary = `MyBoundary`
1265 )
1266
1267 const message = `
1268 --MyBoundary
1269 Content-Disposition: form-data; name="filea"; filename="filea.txt"
1270 Content-Type: text/plain
1271
1272 ` + fileaContents + `
1273 --MyBoundary
1274 Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
1275 Content-Type: text/plain
1276
1277 ` + filebContents + `
1278 --MyBoundary
1279 Content-Disposition: form-data; name="texta"
1280
1281 ` + textaValue + `
1282 --MyBoundary
1283 Content-Disposition: form-data; name="textb"
1284
1285 ` + textbValue + `
1286 --MyBoundary--
1287 `
1288
1289 func benchmarkReadRequest(b *testing.B, request string) {
1290 request = request + "\n"
1291 request = strings.ReplaceAll(request, "\n", "\r\n")
1292 b.SetBytes(int64(len(request)))
1293 r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
1294 b.ReportAllocs()
1295 b.ResetTimer()
1296 for i := 0; i < b.N; i++ {
1297 _, err := ReadRequest(r)
1298 if err != nil {
1299 b.Fatalf("failed to read request: %v", err)
1300 }
1301 }
1302 }
1303
1304
1305
1306 type infiniteReader struct {
1307 buf []byte
1308 offset int
1309 }
1310
1311 func (r *infiniteReader) Read(b []byte) (int, error) {
1312 n := copy(b, r.buf[r.offset:])
1313 r.offset = (r.offset + n) % len(r.buf)
1314 return n, nil
1315 }
1316
1317 func BenchmarkReadRequestChrome(b *testing.B) {
1318
1319 benchmarkReadRequest(b, `GET / HTTP/1.1
1320 Host: localhost:8080
1321 Connection: keep-alive
1322 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1323 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
1324 Accept-Encoding: gzip,deflate,sdch
1325 Accept-Language: en-US,en;q=0.8
1326 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
1327 Cookie: __utma=1.1978842379.1323102373.1323102373.1323102373.1; EPi:NumberOfVisits=1,2012-02-28T13:42:18; CrmSession=5b707226b9563e1bc69084d07a107c98; plushContainerWidth=100%25; plushNoTopMenu=0; hudson_auto_refresh=false
1328 `)
1329 }
1330
1331 func BenchmarkReadRequestCurl(b *testing.B) {
1332
1333 benchmarkReadRequest(b, `GET / HTTP/1.1
1334 User-Agent: curl/7.27.0
1335 Host: localhost:8080
1336 Accept: */*
1337 `)
1338 }
1339
1340 func BenchmarkReadRequestApachebench(b *testing.B) {
1341
1342 benchmarkReadRequest(b, `GET / HTTP/1.0
1343 Host: localhost:8080
1344 User-Agent: ApacheBench/2.3
1345 Accept: */*
1346 `)
1347 }
1348
1349 func BenchmarkReadRequestSiege(b *testing.B) {
1350
1351 benchmarkReadRequest(b, `GET / HTTP/1.1
1352 Host: localhost:8080
1353 Accept: */*
1354 Accept-Encoding: gzip
1355 User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
1356 Connection: keep-alive
1357 `)
1358 }
1359
1360 func BenchmarkReadRequestWrk(b *testing.B) {
1361
1362 benchmarkReadRequest(b, `GET / HTTP/1.1
1363 Host: localhost:8080
1364 `)
1365 }
1366
1367 func BenchmarkFileAndServer_1KB(b *testing.B) {
1368 benchmarkFileAndServer(b, 1<<10)
1369 }
1370
1371 func BenchmarkFileAndServer_16MB(b *testing.B) {
1372 benchmarkFileAndServer(b, 1<<24)
1373 }
1374
1375 func BenchmarkFileAndServer_64MB(b *testing.B) {
1376 benchmarkFileAndServer(b, 1<<26)
1377 }
1378
1379 func benchmarkFileAndServer(b *testing.B, n int64) {
1380 f, err := os.CreateTemp(os.TempDir(), "go-bench-http-file-and-server")
1381 if err != nil {
1382 b.Fatalf("Failed to create temp file: %v", err)
1383 }
1384
1385 defer func() {
1386 f.Close()
1387 os.RemoveAll(f.Name())
1388 }()
1389
1390 if _, err := io.CopyN(f, rand.Reader, n); err != nil {
1391 b.Fatalf("Failed to copy %d bytes: %v", n, err)
1392 }
1393
1394 run(b, func(b *testing.B, mode testMode) {
1395 runFileAndServerBenchmarks(b, mode, f, n)
1396 }, []testMode{http1Mode, https1Mode, http2Mode})
1397 }
1398
1399 func runFileAndServerBenchmarks(b *testing.B, mode testMode, f *os.File, n int64) {
1400 handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
1401 defer req.Body.Close()
1402 nc, err := io.Copy(io.Discard, req.Body)
1403 if err != nil {
1404 panic(err)
1405 }
1406
1407 if nc != n {
1408 panic(fmt.Errorf("Copied %d Wanted %d bytes", nc, n))
1409 }
1410 })
1411
1412 cst := newClientServerTest(b, mode, handler).ts
1413
1414 b.ResetTimer()
1415 for i := 0; i < b.N; i++ {
1416
1417 b.StopTimer()
1418 if _, err := f.Seek(0, 0); err != nil {
1419 b.Fatalf("Failed to seek back to file: %v", err)
1420 }
1421
1422 b.StartTimer()
1423 req, err := NewRequest("PUT", cst.URL, io.NopCloser(f))
1424 if err != nil {
1425 b.Fatal(err)
1426 }
1427
1428 req.ContentLength = n
1429
1430 req.Header.Set("Content-Type", "application/octet-stream")
1431 res, err := cst.Client().Do(req)
1432 if err != nil {
1433 b.Fatalf("Failed to make request to backend: %v", err)
1434 }
1435
1436 res.Body.Close()
1437 b.SetBytes(n)
1438 }
1439 }
1440
1441 func TestErrNotSupported(t *testing.T) {
1442 if !errors.Is(ErrNotSupported, errors.ErrUnsupported) {
1443 t.Error("errors.Is(ErrNotSupported, errors.ErrUnsupported) failed")
1444 }
1445 }
1446
1447 func TestPathValueNoMatch(t *testing.T) {
1448
1449 var r Request
1450 if g, w := r.PathValue("x"), ""; g != w {
1451 t.Errorf("got %q, want %q", g, w)
1452 }
1453 r.SetPathValue("x", "a")
1454 if g, w := r.PathValue("x"), "a"; g != w {
1455 t.Errorf("got %q, want %q", g, w)
1456 }
1457 }
1458
1459 func TestPathValue(t *testing.T) {
1460 for _, test := range []struct {
1461 pattern string
1462 url string
1463 want map[string]string
1464 }{
1465 {
1466 "/{a}/is/{b}/{c...}",
1467 "/now/is/the/time/for/all",
1468 map[string]string{
1469 "a": "now",
1470 "b": "the",
1471 "c": "time/for/all",
1472 "d": "",
1473 },
1474 },
1475 {
1476 "/names/{name}/{other...}",
1477 "/names/%2fjohn/address",
1478 map[string]string{
1479 "name": "/john",
1480 "other": "address",
1481 },
1482 },
1483 {
1484 "/names/{name}/{other...}",
1485 "/names/john%2Fdoe/there/is%2F/more",
1486 map[string]string{
1487 "name": "john/doe",
1488 "other": "there/is//more",
1489 },
1490 },
1491 } {
1492 mux := NewServeMux()
1493 mux.HandleFunc(test.pattern, func(w ResponseWriter, r *Request) {
1494 for name, want := range test.want {
1495 got := r.PathValue(name)
1496 if got != want {
1497 t.Errorf("%q, %q: got %q, want %q", test.pattern, name, got, want)
1498 }
1499 }
1500 })
1501 server := httptest.NewServer(mux)
1502 defer server.Close()
1503 res, err := Get(server.URL + test.url)
1504 if err != nil {
1505 t.Fatal(err)
1506 }
1507 res.Body.Close()
1508 }
1509 }
1510
1511 func TestSetPathValue(t *testing.T) {
1512 mux := NewServeMux()
1513 mux.HandleFunc("/a/{b}/c/{d...}", func(_ ResponseWriter, r *Request) {
1514 kvs := map[string]string{
1515 "b": "X",
1516 "d": "Y",
1517 "a": "Z",
1518 }
1519 for k, v := range kvs {
1520 r.SetPathValue(k, v)
1521 }
1522 for k, w := range kvs {
1523 if g := r.PathValue(k); g != w {
1524 t.Errorf("got %q, want %q", g, w)
1525 }
1526 }
1527 })
1528 server := httptest.NewServer(mux)
1529 defer server.Close()
1530 res, err := Get(server.URL + "/a/b/c/d/e")
1531 if err != nil {
1532 t.Fatal(err)
1533 }
1534 res.Body.Close()
1535 }
1536
1537 func TestStatus(t *testing.T) {
1538
1539 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
1540 mux := NewServeMux()
1541 mux.Handle("GET /g", h)
1542 mux.Handle("POST /p", h)
1543 mux.Handle("PATCH /p", h)
1544 mux.Handle("PUT /r", h)
1545 mux.Handle("GET /r/", h)
1546 server := httptest.NewServer(mux)
1547 defer server.Close()
1548
1549 for _, test := range []struct {
1550 method, path string
1551 wantStatus int
1552 wantAllow string
1553 }{
1554 {"GET", "/g", 200, ""},
1555 {"HEAD", "/g", 200, ""},
1556 {"POST", "/g", 405, "GET, HEAD"},
1557 {"GET", "/x", 404, ""},
1558 {"GET", "/p", 405, "PATCH, POST"},
1559 {"GET", "/./p", 405, "PATCH, POST"},
1560 {"GET", "/r/", 200, ""},
1561 {"GET", "/r", 200, ""},
1562 {"HEAD", "/r/", 200, ""},
1563 {"HEAD", "/r", 200, ""},
1564 {"PUT", "/r/", 405, "GET, HEAD"},
1565 {"PUT", "/r", 200, ""},
1566 } {
1567 req, err := http.NewRequest(test.method, server.URL+test.path, nil)
1568 if err != nil {
1569 t.Fatal(err)
1570 }
1571 res, err := http.DefaultClient.Do(req)
1572 if err != nil {
1573 t.Fatal(err)
1574 }
1575 res.Body.Close()
1576 if g, w := res.StatusCode, test.wantStatus; g != w {
1577 t.Errorf("%s %s: got %d, want %d", test.method, test.path, g, w)
1578 }
1579 if g, w := res.Header.Get("Allow"), test.wantAllow; g != w {
1580 t.Errorf("%s %s, Allow: got %q, want %q", test.method, test.path, g, w)
1581 }
1582 }
1583 }
1584
View as plain text