1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
27 func TestQuery(t
*testing
.T
) {
28 req
:= &Request
{Method
: "GET"}
29 req
.URL
, _
= url
.Parse("http://www.google.com/search?q=foo&q=bar")
30 if q
:= req
.FormValue("q"); q
!= "foo" {
31 t
.Errorf(`req.FormValue("q") = %q, want "foo"`, q
)
35 // Issue #25192: Test that ParseForm fails but still parses the form when an URL
36 // containing a semicolon is provided.
37 func TestParseFormSemicolonSeparator(t
*testing
.T
) {
38 for _
, method
:= range []string{"POST", "PATCH", "PUT", "GET"} {
39 req
, _
:= NewRequest(method
, "http://www.google.com/search?q=foo;q=bar&a=1",
40 strings
.NewReader("q"))
41 err
:= req
.ParseForm()
43 t
.Fatalf(`for method %s, ParseForm expected an error, got success`, method
)
45 wantForm
:= url
.Values
{"a": []string{"1"}}
46 if !reflect
.DeepEqual(req
.Form
, wantForm
) {
47 t
.Fatalf("for method %s, ParseForm expected req.Form = %v, want %v", method
, req
.Form
, wantForm
)
52 func TestParseFormQuery(t
*testing
.T
) {
53 req
, _
:= NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&orphan=nope&empty=not",
54 strings
.NewReader("z=post&both=y&prio=2&=nokey&orphan&empty=&"))
55 req
.Header
.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
57 if q
:= req
.FormValue("q"); q
!= "foo" {
58 t
.Errorf(`req.FormValue("q") = %q, want "foo"`, q
)
60 if z
:= req
.FormValue("z"); z
!= "post" {
61 t
.Errorf(`req.FormValue("z") = %q, want "post"`, z
)
63 if bq
, found
:= req
.PostForm
["q"]; found
{
64 t
.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq
)
66 if bz
:= req
.PostFormValue("z"); bz
!= "post" {
67 t
.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz
)
69 if qs
:= req
.Form
["q"]; !reflect
.DeepEqual(qs
, []string{"foo", "bar"}) {
70 t
.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs
)
72 if both
:= req
.Form
["both"]; !reflect
.DeepEqual(both
, []string{"y", "x"}) {
73 t
.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both
)
75 if prio
:= req
.FormValue("prio"); prio
!= "2" {
76 t
.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio
)
78 if orphan
:= req
.Form
["orphan"]; !reflect
.DeepEqual(orphan
, []string{"", "nope"}) {
79 t
.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan
)
81 if empty
:= req
.Form
["empty"]; !reflect
.DeepEqual(empty
, []string{"", "not"}) {
82 t
.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty
)
84 if nokey
:= req
.Form
[""]; !reflect
.DeepEqual(nokey
, []string{"nokey"}) {
85 t
.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey
)
89 // Tests that we only parse the form automatically for certain methods.
90 func TestParseFormQueryMethods(t
*testing
.T
) {
91 for _
, method
:= range []string{"POST", "PATCH", "PUT", "FOO"} {
92 req
, _
:= NewRequest(method
, "http://www.google.com/search",
93 strings
.NewReader("foo=bar"))
94 req
.Header
.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
99 if got
:= req
.FormValue("foo"); got
!= want
{
100 t
.Errorf(`for method %s, FormValue("foo") = %q; want %q`, method
, got
, want
)
105 func TestParseFormUnknownContentType(t
*testing
.T
) {
106 for _
, test
:= range []struct {
111 {"text", "", Header
{"Content-Type": {"text/plain"}}},
112 // Empty content type is legal - may be treated as
113 // application/octet-stream (RFC 7231, section 3.1.1.5)
114 {"empty", "", Header
{}},
115 {"boundary", "mime: invalid media parameter", Header
{"Content-Type": {"text/plain; boundary="}}},
116 {"unknown", "", Header
{"Content-Type": {"application/unknown"}}},
122 Header
: test
.contentType
,
123 Body
: io
.NopCloser(strings
.NewReader("body")),
125 err
:= req
.ParseForm()
127 case err
== nil && test
.wantErr
!= "":
128 t
.Errorf("unexpected success; want error %q", test
.wantErr
)
129 case err
!= nil && test
.wantErr
== "":
130 t
.Errorf("want success, got error: %v", err
)
131 case test
.wantErr
!= "" && test
.wantErr
!= fmt
.Sprint(err
):
132 t
.Errorf("got error %q; want %q", err
, test
.wantErr
)
139 func TestParseFormInitializeOnError(t
*testing
.T
) {
140 nilBody
, _
:= NewRequest("POST", "http://www.google.com/search?q=foo", nil)
143 {Method
: "GET", URL
: nil},
145 for i
, req
:= range tests
{
146 err
:= req
.ParseForm()
148 t
.Errorf("%d. Form not initialized, error %v", i
, err
)
150 if req
.PostForm
== nil {
151 t
.Errorf("%d. PostForm not initialized, error %v", i
, err
)
156 func TestMultipartReader(t
*testing
.T
) {
161 {false, `multipart/form-data; boundary="foo123"`},
162 {false, `multipart/mixed; boundary="foo123"`},
163 {true, `text/plain`},
166 for i
, test
:= range tests
{
169 Header
: Header
{"Content-Type": {test
.contentType
}},
170 Body
: io
.NopCloser(new(bytes
.Buffer
)),
172 multipart
, err
:= req
.MultipartReader()
173 if test
.shouldError
{
174 if err
== nil || multipart
!= nil {
175 t
.Errorf("test %d: unexpectedly got nil-error (%v) or non-nil-multipart (%v)", i
, err
, multipart
)
179 if err
!= nil || multipart
== nil {
180 t
.Errorf("test %d: unexpectedly got error (%v) or nil-multipart (%v)", i
, err
, multipart
)
185 // Issue 9305: ParseMultipartForm should populate PostForm too
186 func TestParseMultipartFormPopulatesPostForm(t
*testing
.T
) {
189 Content-Disposition: form-data; name="field1"
193 Content-Disposition: form-data; name="field2"
197 Content-Disposition: form-data; name="file"; filename="file"
198 Content-Type: application/octet-stream
199 Content-Transfer-Encoding: binary
206 Header
: Header
{"Content-Type": {`multipart/form-data; boundary=xxx`}},
207 Body
: io
.NopCloser(strings
.NewReader(postData
)),
210 initialFormItems
:= map[string]string{
214 "field2": "initial-value2",
217 req
.Form
= make(url
.Values
)
218 for k
, v
:= range initialFormItems
{
222 err
:= req
.ParseMultipartForm(10000)
224 t
.Fatalf("unexpected multipart error %v", err
)
227 wantForm
:= url
.Values
{
228 "language": []string{"Go"},
229 "name": []string{"gopher"},
230 "skill": []string{"go-ing"},
231 "field1": []string{"value1"},
232 "field2": []string{"initial-value2", "value2"},
234 if !reflect
.DeepEqual(req
.Form
, wantForm
) {
235 t
.Fatalf("req.Form = %v, want %v", req
.Form
, wantForm
)
238 wantPostForm
:= url
.Values
{
239 "field1": []string{"value1"},
240 "field2": []string{"value2"},
242 if !reflect
.DeepEqual(req
.PostForm
, wantPostForm
) {
243 t
.Fatalf("req.PostForm = %v, want %v", req
.PostForm
, wantPostForm
)
247 func TestParseMultipartForm(t
*testing
.T
) {
250 Header
: Header
{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
251 Body
: io
.NopCloser(new(bytes
.Buffer
)),
253 err
:= req
.ParseMultipartForm(25)
255 t
.Error("expected multipart EOF, got nil")
258 req
.Header
= Header
{"Content-Type": {"text/plain"}}
259 err
= req
.ParseMultipartForm(25)
260 if err
!= ErrNotMultipart
{
261 t
.Error("expected ErrNotMultipart for text/plain")
265 // Issue 45789: multipart form should not include directory path in filename
266 func TestParseMultipartFormFilename(t
*testing
.T
) {
269 Content-Disposition: form-data; name="file"; filename="../usr/foobar.txt/"
270 Content-Type: text/plain
276 Header
: Header
{"Content-Type": {`multipart/form-data; boundary=xxx`}},
277 Body
: io
.NopCloser(strings
.NewReader(postData
)),
279 _
, hdr
, err
:= req
.FormFile("file")
283 if hdr
.Filename
!= "foobar.txt" {
284 t
.Errorf("expected only the last element of the path, got %q", hdr
.Filename
)
288 // Issue #40430: Test that if maxMemory for ParseMultipartForm when combined with
289 // the payload size and the internal leeway buffer size of 10MiB overflows, that we
290 // correctly return an error.
291 func TestMaxInt64ForMultipartFormMaxMemoryOverflow(t
*testing
.T
) {
294 payloadSize
:= 1 << 10
295 cst
:= httptest
.NewServer(HandlerFunc(func(rw ResponseWriter
, req
*Request
) {
296 // The combination of:
297 // MaxInt64 + payloadSize + (internal spare of 10MiB)
298 // triggers the overflow. See issue https://golang.org/issue/40430/
299 if err
:= req
.ParseMultipartForm(math
.MaxInt64
); err
!= nil {
300 Error(rw
, err
.Error(), StatusBadRequest
)
305 fBuf
:= new(bytes
.Buffer
)
306 mw
:= multipart
.NewWriter(fBuf
)
307 mf
, err
:= mw
.CreateFormFile("file", "myfile.txt")
311 if _
, err
:= mf
.Write(bytes
.Repeat([]byte("abc"), payloadSize
)); err
!= nil {
314 if err
:= mw
.Close(); err
!= nil {
317 req
, err
:= NewRequest("POST", cst
.URL
, fBuf
)
321 req
.Header
.Set("Content-Type", mw
.FormDataContentType())
322 res
, err
:= cst
.Client().Do(req
)
327 if g
, w
:= res
.StatusCode
, StatusOK
; g
!= w
{
328 t
.Fatalf("Status code mismatch: got %d, want %d", g
, w
)
332 func TestRedirect_h1(t
*testing
.T
) { testRedirect(t
, h1Mode
) }
333 func TestRedirect_h2(t
*testing
.T
) { testRedirect(t
, h2Mode
) }
334 func testRedirect(t
*testing
.T
, h2
bool) {
336 cst
:= newClientServerTest(t
, h2
, HandlerFunc(func(w ResponseWriter
, r
*Request
) {
339 w
.Header().Set("Location", "/foo/")
340 w
.WriteHeader(StatusSeeOther
)
342 fmt
.Fprintf(w
, "foo")
344 w
.WriteHeader(StatusBadRequest
)
349 var end
= regexp
.MustCompile("/foo/$")
350 r
, err
:= cst
.c
.Get(cst
.ts
.URL
)
355 url
:= r
.Request
.URL
.String()
356 if r
.StatusCode
!= 200 ||
!end
.MatchString(url
) {
357 t
.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r
.StatusCode
, url
)
361 func TestSetBasicAuth(t
*testing
.T
) {
362 r
, _
:= NewRequest("GET", "http://example.com/", nil)
363 r
.SetBasicAuth("Aladdin", "open sesame")
364 if g
, e
:= r
.Header
.Get("Authorization"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; g
!= e
{
365 t
.Errorf("got header %q, want %q", g
, e
)
369 func TestMultipartRequest(t
*testing
.T
) {
370 // Test that we can read the values and files of a
371 // multipart request with FormValue and FormFile,
372 // and that ParseMultipartForm can be called multiple times.
373 req
:= newTestMultipartRequest(t
)
374 if err
:= req
.ParseMultipartForm(25); err
!= nil {
375 t
.Fatal("ParseMultipartForm first call:", err
)
377 defer req
.MultipartForm
.RemoveAll()
378 validateTestMultipartContents(t
, req
, false)
379 if err
:= req
.ParseMultipartForm(25); err
!= nil {
380 t
.Fatal("ParseMultipartForm second call:", err
)
382 validateTestMultipartContents(t
, req
, false)
385 // Issue #25192: Test that ParseMultipartForm fails but still parses the
386 // multi-part form when an URL containing a semicolon is provided.
387 func TestParseMultipartFormSemicolonSeparator(t
*testing
.T
) {
388 req
:= newTestMultipartRequest(t
)
389 req
.URL
= &url
.URL
{RawQuery
: "q=foo;q=bar"}
390 if err
:= req
.ParseMultipartForm(25); err
== nil {
391 t
.Fatal("ParseMultipartForm expected error due to invalid semicolon, got nil")
393 defer req
.MultipartForm
.RemoveAll()
394 validateTestMultipartContents(t
, req
, false)
397 func TestMultipartRequestAuto(t
*testing
.T
) {
398 // Test that FormValue and FormFile automatically invoke
399 // ParseMultipartForm and return the right values.
400 req
:= newTestMultipartRequest(t
)
402 if req
.MultipartForm
!= nil {
403 req
.MultipartForm
.RemoveAll()
406 validateTestMultipartContents(t
, req
, true)
409 func TestMissingFileMultipartRequest(t
*testing
.T
) {
410 // Test that FormFile returns an error if
411 // the named file is missing.
412 req
:= newTestMultipartRequest(t
)
413 testMissingFile(t
, req
)
416 // Test that FormValue invokes ParseMultipartForm.
417 func TestFormValueCallsParseMultipartForm(t
*testing
.T
) {
418 req
, _
:= NewRequest("POST", "http://www.google.com/", strings
.NewReader("z=post"))
419 req
.Header
.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
421 t
.Fatal("Unexpected request Form, want nil")
425 t
.Fatal("ParseMultipartForm not called by FormValue")
429 // Test that FormFile invokes ParseMultipartForm.
430 func TestFormFileCallsParseMultipartForm(t
*testing
.T
) {
431 req
:= newTestMultipartRequest(t
)
433 t
.Fatal("Unexpected request Form, want nil")
437 t
.Fatal("ParseMultipartForm not called by FormFile")
441 // Test that ParseMultipartForm errors if called
442 // after MultipartReader on the same request.
443 func TestParseMultipartFormOrder(t
*testing
.T
) {
444 req
:= newTestMultipartRequest(t
)
445 if _
, err
:= req
.MultipartReader(); err
!= nil {
446 t
.Fatalf("MultipartReader: %v", err
)
448 if err
:= req
.ParseMultipartForm(1024); err
== nil {
449 t
.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
453 // Test that MultipartReader errors if called
454 // after ParseMultipartForm on the same request.
455 func TestMultipartReaderOrder(t
*testing
.T
) {
456 req
:= newTestMultipartRequest(t
)
457 if err
:= req
.ParseMultipartForm(25); err
!= nil {
458 t
.Fatalf("ParseMultipartForm: %v", err
)
460 defer req
.MultipartForm
.RemoveAll()
461 if _
, err
:= req
.MultipartReader(); err
== nil {
462 t
.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
466 // Test that FormFile errors if called after
467 // MultipartReader on the same request.
468 func TestFormFileOrder(t
*testing
.T
) {
469 req
:= newTestMultipartRequest(t
)
470 if _
, err
:= req
.MultipartReader(); err
!= nil {
471 t
.Fatalf("MultipartReader: %v", err
)
473 if _
, _
, err
:= req
.FormFile(""); err
== nil {
474 t
.Fatal("expected an error from FormFile after call to MultipartReader")
478 var readRequestErrorTests
= []struct {
484 0: {"GET / HTTP/1.1\r\nheader:foo\r\n\r\n", "", Header
{"Header": {"foo"}}},
485 1: {"GET / HTTP/1.1\r\nheader:foo\r\n", io
.ErrUnexpectedEOF
.Error(), nil},
486 2: {"", io
.EOF
.Error(), nil},
488 in
: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
489 err
: "http: method cannot contain a Content-Length",
492 in
: "HEAD / HTTP/1.1\r\n\r\n",
496 // Multiple Content-Length values should either be
497 // deduplicated if same or reject otherwise
500 in
: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\nGopher hey\r\n",
501 err
: "cannot contain multiple Content-Length headers",
504 in
: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\nGopher\r\n",
505 err
: "cannot contain multiple Content-Length headers",
508 in
: "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
510 header
: Header
{"Content-Length": {"6"}},
513 in
: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
514 err
: "cannot contain multiple Content-Length headers",
517 in
: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
518 err
: "cannot contain multiple Content-Length headers",
521 in
: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
522 header
: Header
{"Content-Length": {"0"}},
525 in
: "HEAD / HTTP/1.1\r\nHost: foo\r\nHost: bar\r\n\r\n\r\n\r\n",
526 err
: "too many Host headers",
530 func TestReadRequestErrors(t
*testing
.T
) {
531 for i
, tt
:= range readRequestErrorTests
{
532 req
, err
:= ReadRequest(bufio
.NewReader(strings
.NewReader(tt
.in
)))
535 t
.Errorf("#%d: got nil err; want %q", i
, tt
.err
)
538 if !reflect
.DeepEqual(tt
.header
, req
.Header
) {
539 t
.Errorf("#%d: gotHeader: %q wantHeader: %q", i
, req
.Header
, tt
.header
)
544 if tt
.err
== "" ||
!strings
.Contains(err
.Error(), tt
.err
) {
545 t
.Errorf("%d: got error = %v; want %v", i
, err
, tt
.err
)
550 var newRequestHostTests
= []struct {
553 {"http://www.example.com/", "www.example.com"},
554 {"http://www.example.com:8080/", "www.example.com:8080"},
556 {"http://192.168.0.1/", "192.168.0.1"},
557 {"http://192.168.0.1:8080/", "192.168.0.1:8080"},
558 {"http://192.168.0.1:/", "192.168.0.1"},
560 {"http://[fe80::1]/", "[fe80::1]"},
561 {"http://[fe80::1]:8080/", "[fe80::1]:8080"},
562 {"http://[fe80::1%25en0]/", "[fe80::1%en0]"},
563 {"http://[fe80::1%25en0]:8080/", "[fe80::1%en0]:8080"},
564 {"http://[fe80::1%25en0]:/", "[fe80::1%en0]"},
567 func TestNewRequestHost(t
*testing
.T
) {
568 for i
, tt
:= range newRequestHostTests
{
569 req
, err
:= NewRequest("GET", tt
.in
, nil)
571 t
.Errorf("#%v: %v", i
, err
)
574 if req
.Host
!= tt
.out
{
575 t
.Errorf("got %q; want %q", req
.Host
, tt
.out
)
580 func TestRequestInvalidMethod(t
*testing
.T
) {
581 _
, err
:= NewRequest("bad method", "http://foo.com/", nil)
583 t
.Error("expected error from NewRequest with invalid method")
585 req
, err
:= NewRequest("GET", "http://foo.example/", nil)
589 req
.Method
= "bad method"
590 _
, err
= DefaultClient
.Do(req
)
591 if err
== nil ||
!strings
.Contains(err
.Error(), "invalid method") {
592 t
.Errorf("Transport error = %v; want invalid method", err
)
595 req
, err
= NewRequest("", "http://foo.com/", nil)
597 t
.Errorf("NewRequest(empty method) = %v; want nil", err
)
598 } else if req
.Method
!= "GET" {
599 t
.Errorf("NewRequest(empty method) has method %q; want GET", req
.Method
)
603 func TestNewRequestContentLength(t
*testing
.T
) {
604 readByte
:= func(r io
.Reader
) io
.Reader
{
613 {bytes
.NewReader([]byte("123")), 3},
614 {bytes
.NewBuffer([]byte("1234")), 4},
615 {strings
.NewReader("12345"), 5},
616 {strings
.NewReader(""), 0},
619 // Not detected. During Go 1.8 we tried to make these set to -1, but
620 // due to Issue 18117, we keep these returning 0, even though they're
622 {struct{ io
.Reader
}{strings
.NewReader("xyz")}, 0},
623 {io
.NewSectionReader(strings
.NewReader("x"), 0, 6), 0},
624 {readByte(io
.NewSectionReader(strings
.NewReader("xy"), 0, 6)), 0},
626 for i
, tt
:= range tests
{
627 req
, err
:= NewRequest("POST", "http://localhost/", tt
.r
)
631 if req
.ContentLength
!= tt
.want
{
632 t
.Errorf("test[%d]: ContentLength(%T) = %d; want %d", i
, tt
.r
, req
.ContentLength
, tt
.want
)
637 var parseHTTPVersionTests
= []struct {
642 {"HTTP/0.0", 0, 0, true},
643 {"HTTP/0.9", 0, 9, true},
644 {"HTTP/1.0", 1, 0, true},
645 {"HTTP/1.1", 1, 1, true},
647 {"HTTP", 0, 0, false},
648 {"HTTP/one.one", 0, 0, false},
649 {"HTTP/1.1/", 0, 0, false},
650 {"HTTP/-1,0", 0, 0, false},
651 {"HTTP/0,-1", 0, 0, false},
652 {"HTTP/", 0, 0, false},
653 {"HTTP/1,1", 0, 0, false},
654 {"HTTP/+1.1", 0, 0, false},
655 {"HTTP/1.+1", 0, 0, false},
656 {"HTTP/0000000001.1", 0, 0, false},
657 {"HTTP/1.0000000001", 0, 0, false},
658 {"HTTP/3.14", 0, 0, false},
659 {"HTTP/12.3", 0, 0, false},
662 func TestParseHTTPVersion(t
*testing
.T
) {
663 for _
, tt
:= range parseHTTPVersionTests
{
664 major
, minor
, ok
:= ParseHTTPVersion(tt
.vers
)
665 if ok
!= tt
.ok || major
!= tt
.major || minor
!= tt
.minor
{
666 type version
struct {
670 t
.Errorf("failed to parse %q, expected: %#v, got %#v", tt
.vers
, version
{tt
.major
, tt
.minor
, tt
.ok
}, version
{major
, minor
, ok
})
675 type getBasicAuthTest
struct {
676 username
, password
string
680 type basicAuthCredentialsTest
struct {
681 username
, password
string
684 var getBasicAuthTests
= []struct {
685 username
, password
string
688 {"Aladdin", "open sesame", true},
689 {"Aladdin", "open:sesame", true},
693 func TestGetBasicAuth(t
*testing
.T
) {
694 for _
, tt
:= range getBasicAuthTests
{
695 r
, _
:= NewRequest("GET", "http://example.com/", nil)
696 r
.SetBasicAuth(tt
.username
, tt
.password
)
697 username
, password
, ok
:= r
.BasicAuth()
698 if ok
!= tt
.ok || username
!= tt
.username || password
!= tt
.password
{
699 t
.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest
{username
, password
, ok
},
700 getBasicAuthTest
{tt
.username
, tt
.password
, tt
.ok
})
703 // Unauthenticated request.
704 r
, _
:= NewRequest("GET", "http://example.com/", nil)
705 username
, password
, ok
:= r
.BasicAuth()
707 t
.Errorf("expected false from BasicAuth when the request is unauthenticated")
709 want
:= basicAuthCredentialsTest
{"", ""}
710 if username
!= want
.username || password
!= want
.password
{
711 t
.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
712 want
, basicAuthCredentialsTest
{username
, password
})
716 var parseBasicAuthTests
= []struct {
717 header
, username
, password
string
720 {"Basic " + base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
722 // Case doesn't matter:
723 {"BASIC " + base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
724 {"basic " + base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
726 {"Basic " + base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
727 {"Basic " + base64
.StdEncoding
.EncodeToString([]byte(":")), "", "", true},
728 {"Basic" + base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
729 {base64
.StdEncoding
.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
730 {"Basic ", "", "", false},
731 {"Basic Aladdin:open sesame", "", "", false},
732 {`Digest username="Aladdin"`, "", "", false},
735 func TestParseBasicAuth(t
*testing
.T
) {
736 for _
, tt
:= range parseBasicAuthTests
{
737 r
, _
:= NewRequest("GET", "http://example.com/", nil)
738 r
.Header
.Set("Authorization", tt
.header
)
739 username
, password
, ok
:= r
.BasicAuth()
740 if ok
!= tt
.ok || username
!= tt
.username || password
!= tt
.password
{
741 t
.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest
{username
, password
, ok
},
742 getBasicAuthTest
{tt
.username
, tt
.password
, tt
.ok
})
747 type logWrites
struct {
752 func (l logWrites
) WriteByte(c
byte) error
{
753 l
.t
.Fatalf("unexpected WriteByte call")
757 func (l logWrites
) Write(p
[]byte) (n
int, err error
) {
758 *l
.dst
= append(*l
.dst
, string(p
))
762 func TestRequestWriteBufferedWriter(t
*testing
.T
) {
764 req
, _
:= NewRequest("GET", "http://foo.com/", nil)
765 req
.Write(logWrites
{t
, &got
})
767 "GET / HTTP/1.1\r\n",
769 "User-Agent: " + DefaultUserAgent
+ "\r\n",
772 if !reflect
.DeepEqual(got
, want
) {
773 t
.Errorf("Writes = %q\n Want = %q", got
, want
)
777 func TestRequestBadHost(t
*testing
.T
) {
779 req
, err
:= NewRequest("GET", "http://foo/after", nil)
783 req
.Host
= "foo.com with spaces"
784 req
.URL
.Host
= "foo.com with spaces"
785 req
.Write(logWrites
{t
, &got
})
787 "GET /after HTTP/1.1\r\n",
789 "User-Agent: " + DefaultUserAgent
+ "\r\n",
792 if !reflect
.DeepEqual(got
, want
) {
793 t
.Errorf("Writes = %q\n Want = %q", got
, want
)
797 func TestStarRequest(t
*testing
.T
) {
798 req
, err
:= ReadRequest(bufio
.NewReader(strings
.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
802 if req
.ContentLength
!= 0 {
803 t
.Errorf("ContentLength = %d; want 0", req
.ContentLength
)
806 t
.Errorf("Body = nil; want non-nil")
809 // Request.Write has Client semantics for Body/ContentLength,
810 // where ContentLength 0 means unknown if Body is non-nil, and
811 // thus chunking will happen unless we change semantics and
812 // signal that we want to serialize it as exactly zero. The
813 // only way to do that for outbound requests is with a nil
819 if err
:= clientReq
.Write(&out
); err
!= nil {
823 if strings
.Contains(out
.String(), "chunked") {
824 t
.Error("wrote chunked request; want no body")
826 back
, err
:= ReadRequest(bufio
.NewReader(bytes
.NewReader(out
.Bytes())))
830 // Ignore the Headers (the User-Agent breaks the deep equal,
831 // but we don't care about it)
834 if !reflect
.DeepEqual(req
, back
) {
835 t
.Errorf("Original request doesn't match Request read back.")
836 t
.Logf("Original: %#v", req
)
837 t
.Logf("Original.URL: %#v", req
.URL
)
838 t
.Logf("Wrote: %s", out
.Bytes())
839 t
.Logf("Read back (doesn't match Original): %#v", back
)
843 type responseWriterJustWriter
struct {
847 func (responseWriterJustWriter
) Header() Header
{ panic("should not be called") }
848 func (responseWriterJustWriter
) WriteHeader(int) { panic("should not be called") }
850 // delayedEOFReader never returns (n > 0, io.EOF), instead putting
851 // off the io.EOF until a subsequent Read call.
852 type delayedEOFReader
struct {
856 func (dr delayedEOFReader
) Read(p
[]byte) (n
int, err error
) {
857 n
, err
= dr
.r
.Read(p
)
858 if n
> 0 && err
== io
.EOF
{
864 func TestIssue10884_MaxBytesEOF(t
*testing
.T
) {
866 _
, err
:= io
.Copy(dst
, MaxBytesReader(
867 responseWriterJustWriter
{dst
},
868 io
.NopCloser(delayedEOFReader
{strings
.NewReader("12345")}),
875 // Issue 14981: MaxBytesReader's return error wasn't sticky. It
876 // doesn't technically need to be, but people expected it to be.
877 func TestMaxBytesReaderStickyError(t
*testing
.T
) {
878 isSticky
:= func(r io
.Reader
) error
{
880 buf
:= make([]byte, 1000)
883 n
, err
:= r
.Read(buf
)
884 fmt
.Fprintf(&log
, "Read(%d) = %d, %v\n", len(buf
), n
, err
)
892 if !reflect
.DeepEqual(err
, firstErr
) {
893 return fmt
.Errorf("non-sticky error. got log:\n%s", log
.Bytes())
895 t
.Logf("Got log: %s", log
.Bytes())
899 tests
:= [...]struct {
907 for i
, tt
:= range tests
{
908 rc
:= MaxBytesReader(nil, io
.NopCloser(bytes
.NewReader(make([]byte, tt
.readable
))), tt
.limit
)
909 if err
:= isSticky(rc
); err
!= nil {
910 t
.Errorf("%d. error: %v", i
, err
)
915 // Issue 45101: maxBytesReader's Read panicked when n < -1. This test
916 // also ensures that Read treats negative limits as equivalent to 0.
917 func TestMaxBytesReaderDifferentLimits(t
*testing
.T
) {
918 const testStr
= "1234"
919 tests
:= [...]struct {
929 wantErr
: false, // Ensure we won't return an error when the limit is negative, but we don't need to read.
974 limit
: int64(len(testStr
)),
986 for i
, tt
:= range tests
{
987 rc
:= MaxBytesReader(nil, io
.NopCloser(strings
.NewReader(testStr
)), tt
.limit
)
989 n
, err
:= rc
.Read(make([]byte, tt
.lenP
))
992 t
.Errorf("%d. n: %d, want n: %d", i
, n
, tt
.wantN
)
995 if (err
!= nil) != tt
.wantErr
{
996 t
.Errorf("%d. error: %v", i
, err
)
1001 func TestWithContextDeepCopiesURL(t
*testing
.T
) {
1002 req
, err
:= NewRequest("POST", "https://golang.org/", nil)
1007 reqCopy
:= req
.WithContext(context
.Background())
1008 reqCopy
.URL
.Scheme
= "http"
1010 firstURL
, secondURL
:= req
.URL
.String(), reqCopy
.URL
.String()
1011 if firstURL
== secondURL
{
1012 t
.Errorf("unexpected change to original request's URL")
1015 // And also check we don't crash on nil (Issue 20601)
1017 reqCopy
= req
.WithContext(context
.Background())
1018 if reqCopy
.URL
!= nil {
1019 t
.Error("expected nil URL in cloned request")
1023 // Ensure that Request.Clone creates a deep copy of TransferEncoding.
1025 func TestRequestCloneTransferEncoding(t
*testing
.T
) {
1026 body
:= strings
.NewReader("body")
1027 req
, _
:= NewRequest("POST", "https://example.org/", body
)
1028 req
.TransferEncoding
= []string{
1032 clonedReq
:= req
.Clone(context
.Background())
1033 // modify original after deep copy
1034 req
.TransferEncoding
[0] = "encoding2"
1036 if req
.TransferEncoding
[0] != "encoding2" {
1037 t
.Error("expected req.TransferEncoding to be changed")
1039 if clonedReq
.TransferEncoding
[0] != "encoding1" {
1040 t
.Error("expected clonedReq.TransferEncoding to be unchanged")
1044 func TestNoPanicOnRoundTripWithBasicAuth_h1(t
*testing
.T
) {
1045 testNoPanicWithBasicAuth(t
, h1Mode
)
1048 func TestNoPanicOnRoundTripWithBasicAuth_h2(t
*testing
.T
) {
1049 testNoPanicWithBasicAuth(t
, h2Mode
)
1052 // Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
1053 func testNoPanicWithBasicAuth(t
*testing
.T
, h2
bool) {
1055 cst
:= newClientServerTest(t
, h2
, HandlerFunc(func(w ResponseWriter
, r
*Request
) {}))
1058 u
, err
:= url
.Parse(cst
.ts
.URL
)
1062 u
.User
= url
.UserPassword("foo", "bar")
1067 if _
, err
:= cst
.c
.Do(req
); err
!= nil {
1068 t
.Fatalf("Unexpected error: %v", err
)
1072 // verify that NewRequest sets Request.GetBody and that it works
1073 func TestNewRequestGetBody(t
*testing
.T
) {
1077 {r
: strings
.NewReader("hello")},
1078 {r
: bytes
.NewReader([]byte("hello"))},
1079 {r
: bytes
.NewBuffer([]byte("hello"))},
1081 for i
, tt
:= range tests
{
1082 req
, err
:= NewRequest("POST", "http://foo.tld/", tt
.r
)
1084 t
.Errorf("test[%d]: %v", i
, err
)
1087 if req
.Body
== nil {
1088 t
.Errorf("test[%d]: Body = nil", i
)
1091 if req
.GetBody
== nil {
1092 t
.Errorf("test[%d]: GetBody = nil", i
)
1095 slurp1
, err
:= io
.ReadAll(req
.Body
)
1097 t
.Errorf("test[%d]: ReadAll(Body) = %v", i
, err
)
1099 newBody
, err
:= req
.GetBody()
1101 t
.Errorf("test[%d]: GetBody = %v", i
, err
)
1103 slurp2
, err
:= io
.ReadAll(newBody
)
1105 t
.Errorf("test[%d]: ReadAll(GetBody()) = %v", i
, err
)
1107 if string(slurp1
) != string(slurp2
) {
1108 t
.Errorf("test[%d]: Body %q != GetBody %q", i
, slurp1
, slurp2
)
1113 func testMissingFile(t
*testing
.T
, req
*Request
) {
1114 f
, fh
, err
:= req
.FormFile("missing")
1116 t
.Errorf("FormFile file = %v, want nil", f
)
1119 t
.Errorf("FormFile file header = %q, want nil", fh
)
1121 if err
!= ErrMissingFile
{
1122 t
.Errorf("FormFile err = %q, want ErrMissingFile", err
)
1126 func newTestMultipartRequest(t
*testing
.T
) *Request
{
1127 b
:= strings
.NewReader(strings
.ReplaceAll(message
, "\n", "\r\n"))
1128 req
, err
:= NewRequest("POST", "/", b
)
1130 t
.Fatal("NewRequest:", err
)
1132 ctype
:= fmt
.Sprintf(`multipart/form-data; boundary="%s"`, boundary
)
1133 req
.Header
.Set("Content-type", ctype
)
1137 func validateTestMultipartContents(t
*testing
.T
, req
*Request
, allMem
bool) {
1138 if g
, e
:= req
.FormValue("texta"), textaValue
; g
!= e
{
1139 t
.Errorf("texta value = %q, want %q", g
, e
)
1141 if g
, e
:= req
.FormValue("textb"), textbValue
; g
!= e
{
1142 t
.Errorf("textb value = %q, want %q", g
, e
)
1144 if g
:= req
.FormValue("missing"); g
!= "" {
1145 t
.Errorf("missing value = %q, want empty string", g
)
1148 assertMem
:= func(n
string, fd multipart
.File
) {
1149 if _
, ok
:= fd
.(*os
.File
); ok
{
1150 t
.Error(n
, " is *os.File, should not be")
1153 fda
:= testMultipartFile(t
, req
, "filea", "filea.txt", fileaContents
)
1155 assertMem("filea", fda
)
1156 fdb
:= testMultipartFile(t
, req
, "fileb", "fileb.txt", filebContents
)
1159 assertMem("fileb", fdb
)
1161 if _
, ok
:= fdb
.(*os
.File
); !ok
{
1162 t
.Errorf("fileb has unexpected underlying type %T", fdb
)
1166 testMissingFile(t
, req
)
1169 func testMultipartFile(t
*testing
.T
, req
*Request
, key
, expectFilename
, expectContent
string) multipart
.File
{
1170 f
, fh
, err
:= req
.FormFile(key
)
1172 t
.Fatalf("FormFile(%q): %q", key
, err
)
1174 if fh
.Filename
!= expectFilename
{
1175 t
.Errorf("filename = %q, want %q", fh
.Filename
, expectFilename
)
1178 _
, err
= io
.Copy(&b
, f
)
1180 t
.Fatal("copying contents:", err
)
1182 if g
:= b
.String(); g
!= expectContent
{
1183 t
.Errorf("contents = %q, want %q", g
, expectContent
)
1189 fileaContents
= "This is a test file."
1190 filebContents
= "Another test file."
1193 boundary
= `MyBoundary`
1198 Content-Disposition: form-data; name="filea"; filename="filea.txt"
1199 Content-Type: text/plain
1201 ` + fileaContents
+ `
1203 Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
1204 Content-Type: text/plain
1206 ` + filebContents
+ `
1208 Content-Disposition: form-data; name="texta"
1212 Content-Disposition: form-data; name="textb"
1218 func benchmarkReadRequest(b
*testing
.B
, request
string) {
1219 request
= request
+ "\n" // final \n
1220 request
= strings
.ReplaceAll(request
, "\n", "\r\n") // expand \n to \r\n
1221 b
.SetBytes(int64(len(request
)))
1222 r
:= bufio
.NewReader(&infiniteReader
{buf
: []byte(request
)})
1225 for i
:= 0; i
< b
.N
; i
++ {
1226 _
, err
:= ReadRequest(r
)
1228 b
.Fatalf("failed to read request: %v", err
)
1233 // infiniteReader satisfies Read requests as if the contents of buf
1234 // loop indefinitely.
1235 type infiniteReader
struct {
1240 func (r
*infiniteReader
) Read(b
[]byte) (int, error
) {
1241 n
:= copy(b
, r
.buf
[r
.offset
:])
1242 r
.offset
= (r
.offset
+ n
) % len(r
.buf
)
1246 func BenchmarkReadRequestChrome(b
*testing
.B
) {
1247 // https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
1248 benchmarkReadRequest(b
, `GET / HTTP/1.1
1249 Host: localhost:8080
1250 Connection: keep-alive
1251 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1252 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
1253 Accept-Encoding: gzip,deflate,sdch
1254 Accept-Language: en-US,en;q=0.8
1255 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
1256 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
1260 func BenchmarkReadRequestCurl(b
*testing
.B
) {
1261 // curl http://localhost:8080/
1262 benchmarkReadRequest(b
, `GET / HTTP/1.1
1263 User-Agent: curl/7.27.0
1264 Host: localhost:8080
1269 func BenchmarkReadRequestApachebench(b
*testing
.B
) {
1270 // ab -n 1 -c 1 http://localhost:8080/
1271 benchmarkReadRequest(b
, `GET / HTTP/1.0
1272 Host: localhost:8080
1273 User-Agent: ApacheBench/2.3
1278 func BenchmarkReadRequestSiege(b
*testing
.B
) {
1279 // siege -r 1 -c 1 http://localhost:8080/
1280 benchmarkReadRequest(b
, `GET / HTTP/1.1
1281 Host: localhost:8080
1283 Accept-Encoding: gzip
1284 User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
1285 Connection: keep-alive
1289 func BenchmarkReadRequestWrk(b
*testing
.B
) {
1290 // wrk -t 1 -r 1 -c 1 http://localhost:8080/
1291 benchmarkReadRequest(b
, `GET / HTTP/1.1
1292 Host: localhost:8080
1301 func BenchmarkFileAndServer_1KB(b
*testing
.B
) {
1302 benchmarkFileAndServer(b
, 1<<10)
1305 func BenchmarkFileAndServer_16MB(b
*testing
.B
) {
1306 benchmarkFileAndServer(b
, 1<<24)
1309 func BenchmarkFileAndServer_64MB(b
*testing
.B
) {
1310 benchmarkFileAndServer(b
, 1<<26)
1313 func benchmarkFileAndServer(b
*testing
.B
, n
int64) {
1314 f
, err
:= os
.CreateTemp(os
.TempDir(), "go-bench-http-file-and-server")
1316 b
.Fatalf("Failed to create temp file: %v", err
)
1321 os
.RemoveAll(f
.Name())
1324 if _
, err
:= io
.CopyN(f
, rand
.Reader
, n
); err
!= nil {
1325 b
.Fatalf("Failed to copy %d bytes: %v", n
, err
)
1328 b
.Run("NoTLS", func(b
*testing
.B
) {
1329 runFileAndServerBenchmarks(b
, noTLS
, f
, n
)
1332 b
.Run("TLS", func(b
*testing
.B
) {
1333 runFileAndServerBenchmarks(b
, withTLS
, f
, n
)
1337 func runFileAndServerBenchmarks(b
*testing
.B
, tlsOption
bool, f
*os
.File
, n
int64) {
1338 handler
:= HandlerFunc(func(rw ResponseWriter
, req
*Request
) {
1339 defer req
.Body
.Close()
1340 nc
, err
:= io
.Copy(io
.Discard
, req
.Body
)
1346 panic(fmt
.Errorf("Copied %d Wanted %d bytes", nc
, n
))
1350 var cst
*httptest
.Server
1351 if tlsOption
== withTLS
{
1352 cst
= httptest
.NewTLSServer(handler
)
1354 cst
= httptest
.NewServer(handler
)
1359 for i
:= 0; i
< b
.N
; i
++ {
1360 // Perform some setup.
1362 if _
, err
:= f
.Seek(0, 0); err
!= nil {
1363 b
.Fatalf("Failed to seek back to file: %v", err
)
1367 req
, err
:= NewRequest("PUT", cst
.URL
, io
.NopCloser(f
))
1372 req
.ContentLength
= n
1373 // Prevent mime sniffing by setting the Content-Type.
1374 req
.Header
.Set("Content-Type", "application/octet-stream")
1375 res
, err
:= cst
.Client().Do(req
)
1377 b
.Fatalf("Failed to make request to backend: %v", err
)