Source file src/net/http/request_test.go

     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.
     4  
     5  package http_test
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"context"
    11  	"crypto/rand"
    12  	"encoding/base64"
    13  	"fmt"
    14  	"io"
    15  	"math"
    16  	"mime/multipart"
    17  	. "net/http"
    18  	"net/http/httptest"
    19  	"net/url"
    20  	"os"
    21  	"reflect"
    22  	"regexp"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    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)
    32  	}
    33  }
    34  
    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()
    42  		if err == nil {
    43  			t.Fatalf(`for method %s, ParseForm expected an error, got success`, method)
    44  		}
    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)
    48  		}
    49  	}
    50  }
    51  
    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")
    56  
    57  	if q := req.FormValue("q"); q != "foo" {
    58  		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
    59  	}
    60  	if z := req.FormValue("z"); z != "post" {
    61  		t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
    62  	}
    63  	if bq, found := req.PostForm["q"]; found {
    64  		t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
    65  	}
    66  	if bz := req.PostFormValue("z"); bz != "post" {
    67  		t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
    68  	}
    69  	if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
    70  		t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
    71  	}
    72  	if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
    73  		t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
    74  	}
    75  	if prio := req.FormValue("prio"); prio != "2" {
    76  		t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
    77  	}
    78  	if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
    79  		t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
    80  	}
    81  	if empty := req.Form["empty"]; !reflect.DeepEqual(empty, []string{"", "not"}) {
    82  		t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
    83  	}
    84  	if nokey := req.Form[""]; !reflect.DeepEqual(nokey, []string{"nokey"}) {
    85  		t.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey)
    86  	}
    87  }
    88  
    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")
    95  		want := "bar"
    96  		if method == "FOO" {
    97  			want = ""
    98  		}
    99  		if got := req.FormValue("foo"); got != want {
   100  			t.Errorf(`for method %s, FormValue("foo") = %q; want %q`, method, got, want)
   101  		}
   102  	}
   103  }
   104  
   105  func TestParseFormUnknownContentType(t *testing.T) {
   106  	for _, test := range []struct {
   107  		name        string
   108  		wantErr     string
   109  		contentType Header
   110  	}{
   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"}}},
   117  	} {
   118  		t.Run(test.name,
   119  			func(t *testing.T) {
   120  				req := &Request{
   121  					Method: "POST",
   122  					Header: test.contentType,
   123  					Body:   io.NopCloser(strings.NewReader("body")),
   124  				}
   125  				err := req.ParseForm()
   126  				switch {
   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)
   133  				}
   134  			},
   135  		)
   136  	}
   137  }
   138  
   139  func TestParseFormInitializeOnError(t *testing.T) {
   140  	nilBody, _ := NewRequest("POST", "http://www.google.com/search?q=foo", nil)
   141  	tests := []*Request{
   142  		nilBody,
   143  		{Method: "GET", URL: nil},
   144  	}
   145  	for i, req := range tests {
   146  		err := req.ParseForm()
   147  		if req.Form == nil {
   148  			t.Errorf("%d. Form not initialized, error %v", i, err)
   149  		}
   150  		if req.PostForm == nil {
   151  			t.Errorf("%d. PostForm not initialized, error %v", i, err)
   152  		}
   153  	}
   154  }
   155  
   156  func TestMultipartReader(t *testing.T) {
   157  	tests := []struct {
   158  		shouldError bool
   159  		contentType string
   160  	}{
   161  		{false, `multipart/form-data; boundary="foo123"`},
   162  		{false, `multipart/mixed; boundary="foo123"`},
   163  		{true, `text/plain`},
   164  	}
   165  
   166  	for i, test := range tests {
   167  		req := &Request{
   168  			Method: "POST",
   169  			Header: Header{"Content-Type": {test.contentType}},
   170  			Body:   io.NopCloser(new(bytes.Buffer)),
   171  		}
   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)
   176  			}
   177  			continue
   178  		}
   179  		if err != nil || multipart == nil {
   180  			t.Errorf("test %d: unexpectedly got error (%v) or nil-multipart (%v)", i, err, multipart)
   181  		}
   182  	}
   183  }
   184  
   185  // Issue 9305: ParseMultipartForm should populate PostForm too
   186  func TestParseMultipartFormPopulatesPostForm(t *testing.T) {
   187  	postData :=
   188  		`--xxx
   189  Content-Disposition: form-data; name="field1"
   190  
   191  value1
   192  --xxx
   193  Content-Disposition: form-data; name="field2"
   194  
   195  value2
   196  --xxx
   197  Content-Disposition: form-data; name="file"; filename="file"
   198  Content-Type: application/octet-stream
   199  Content-Transfer-Encoding: binary
   200  
   201  binary data
   202  --xxx--
   203  `
   204  	req := &Request{
   205  		Method: "POST",
   206  		Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
   207  		Body:   io.NopCloser(strings.NewReader(postData)),
   208  	}
   209  
   210  	initialFormItems := map[string]string{
   211  		"language": "Go",
   212  		"name":     "gopher",
   213  		"skill":    "go-ing",
   214  		"field2":   "initial-value2",
   215  	}
   216  
   217  	req.Form = make(url.Values)
   218  	for k, v := range initialFormItems {
   219  		req.Form.Add(k, v)
   220  	}
   221  
   222  	err := req.ParseMultipartForm(10000)
   223  	if err != nil {
   224  		t.Fatalf("unexpected multipart error %v", err)
   225  	}
   226  
   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"},
   233  	}
   234  	if !reflect.DeepEqual(req.Form, wantForm) {
   235  		t.Fatalf("req.Form = %v, want %v", req.Form, wantForm)
   236  	}
   237  
   238  	wantPostForm := url.Values{
   239  		"field1": []string{"value1"},
   240  		"field2": []string{"value2"},
   241  	}
   242  	if !reflect.DeepEqual(req.PostForm, wantPostForm) {
   243  		t.Fatalf("req.PostForm = %v, want %v", req.PostForm, wantPostForm)
   244  	}
   245  }
   246  
   247  func TestParseMultipartForm(t *testing.T) {
   248  	req := &Request{
   249  		Method: "POST",
   250  		Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
   251  		Body:   io.NopCloser(new(bytes.Buffer)),
   252  	}
   253  	err := req.ParseMultipartForm(25)
   254  	if err == nil {
   255  		t.Error("expected multipart EOF, got nil")
   256  	}
   257  
   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")
   262  	}
   263  }
   264  
   265  // Issue 45789: multipart form should not include directory path in filename
   266  func TestParseMultipartFormFilename(t *testing.T) {
   267  	postData :=
   268  		`--xxx
   269  Content-Disposition: form-data; name="file"; filename="../usr/foobar.txt/"
   270  Content-Type: text/plain
   271  
   272  --xxx--
   273  `
   274  	req := &Request{
   275  		Method: "POST",
   276  		Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
   277  		Body:   io.NopCloser(strings.NewReader(postData)),
   278  	}
   279  	_, hdr, err := req.FormFile("file")
   280  	if err != nil {
   281  		t.Fatal(err)
   282  	}
   283  	if hdr.Filename != "foobar.txt" {
   284  		t.Errorf("expected only the last element of the path, got %q", hdr.Filename)
   285  	}
   286  }
   287  
   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) {
   292  	defer afterTest(t)
   293  
   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)
   301  			return
   302  		}
   303  	}))
   304  	defer cst.Close()
   305  	fBuf := new(bytes.Buffer)
   306  	mw := multipart.NewWriter(fBuf)
   307  	mf, err := mw.CreateFormFile("file", "myfile.txt")
   308  	if err != nil {
   309  		t.Fatal(err)
   310  	}
   311  	if _, err := mf.Write(bytes.Repeat([]byte("abc"), payloadSize)); err != nil {
   312  		t.Fatal(err)
   313  	}
   314  	if err := mw.Close(); err != nil {
   315  		t.Fatal(err)
   316  	}
   317  	req, err := NewRequest("POST", cst.URL, fBuf)
   318  	if err != nil {
   319  		t.Fatal(err)
   320  	}
   321  	req.Header.Set("Content-Type", mw.FormDataContentType())
   322  	res, err := cst.Client().Do(req)
   323  	if err != nil {
   324  		t.Fatal(err)
   325  	}
   326  	res.Body.Close()
   327  	if g, w := res.StatusCode, StatusOK; g != w {
   328  		t.Fatalf("Status code mismatch: got %d, want %d", g, w)
   329  	}
   330  }
   331  
   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) {
   335  	defer afterTest(t)
   336  	cst := newClientServerTest(t, h2, 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  	defer cst.close()
   348  
   349  	var end = regexp.MustCompile("/foo/$")
   350  	r, err := cst.c.Get(cst.ts.URL)
   351  	if err != nil {
   352  		t.Fatal(err)
   353  	}
   354  	r.Body.Close()
   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)
   358  	}
   359  }
   360  
   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)
   366  	}
   367  }
   368  
   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)
   376  	}
   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)
   381  	}
   382  	validateTestMultipartContents(t, req, false)
   383  }
   384  
   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")
   392  	}
   393  	defer req.MultipartForm.RemoveAll()
   394  	validateTestMultipartContents(t, req, false)
   395  }
   396  
   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)
   401  	defer func() {
   402  		if req.MultipartForm != nil {
   403  			req.MultipartForm.RemoveAll()
   404  		}
   405  	}()
   406  	validateTestMultipartContents(t, req, true)
   407  }
   408  
   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)
   414  }
   415  
   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")
   420  	if req.Form != nil {
   421  		t.Fatal("Unexpected request Form, want nil")
   422  	}
   423  	req.FormValue("z")
   424  	if req.Form == nil {
   425  		t.Fatal("ParseMultipartForm not called by FormValue")
   426  	}
   427  }
   428  
   429  // Test that FormFile invokes ParseMultipartForm.
   430  func TestFormFileCallsParseMultipartForm(t *testing.T) {
   431  	req := newTestMultipartRequest(t)
   432  	if req.Form != nil {
   433  		t.Fatal("Unexpected request Form, want nil")
   434  	}
   435  	req.FormFile("")
   436  	if req.Form == nil {
   437  		t.Fatal("ParseMultipartForm not called by FormFile")
   438  	}
   439  }
   440  
   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)
   447  	}
   448  	if err := req.ParseMultipartForm(1024); err == nil {
   449  		t.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
   450  	}
   451  }
   452  
   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)
   459  	}
   460  	defer req.MultipartForm.RemoveAll()
   461  	if _, err := req.MultipartReader(); err == nil {
   462  		t.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
   463  	}
   464  }
   465  
   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)
   472  	}
   473  	if _, _, err := req.FormFile(""); err == nil {
   474  		t.Fatal("expected an error from FormFile after call to MultipartReader")
   475  	}
   476  }
   477  
   478  var readRequestErrorTests = []struct {
   479  	in  string
   480  	err string
   481  
   482  	header Header
   483  }{
   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},
   487  	3: {
   488  		in:  "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
   489  		err: "http: method cannot contain a Content-Length",
   490  	},
   491  	4: {
   492  		in:     "HEAD / HTTP/1.1\r\n\r\n",
   493  		header: Header{},
   494  	},
   495  
   496  	// Multiple Content-Length values should either be
   497  	// deduplicated if same or reject otherwise
   498  	// See Issue 16490.
   499  	5: {
   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",
   502  	},
   503  	6: {
   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",
   506  	},
   507  	7: {
   508  		in:     "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
   509  		err:    "",
   510  		header: Header{"Content-Length": {"6"}},
   511  	},
   512  	8: {
   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",
   515  	},
   516  	9: {
   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",
   519  	},
   520  	10: {
   521  		in:     "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
   522  		header: Header{"Content-Length": {"0"}},
   523  	},
   524  	11: {
   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",
   527  	},
   528  }
   529  
   530  func TestReadRequestErrors(t *testing.T) {
   531  	for i, tt := range readRequestErrorTests {
   532  		req, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.in)))
   533  		if err == nil {
   534  			if tt.err != "" {
   535  				t.Errorf("#%d: got nil err; want %q", i, tt.err)
   536  			}
   537  
   538  			if !reflect.DeepEqual(tt.header, req.Header) {
   539  				t.Errorf("#%d: gotHeader: %q wantHeader: %q", i, req.Header, tt.header)
   540  			}
   541  			continue
   542  		}
   543  
   544  		if tt.err == "" || !strings.Contains(err.Error(), tt.err) {
   545  			t.Errorf("%d: got error = %v; want %v", i, err, tt.err)
   546  		}
   547  	}
   548  }
   549  
   550  var newRequestHostTests = []struct {
   551  	in, out string
   552  }{
   553  	{"http://www.example.com/", "www.example.com"},
   554  	{"http://www.example.com:8080/", "www.example.com:8080"},
   555  
   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"},
   559  
   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]"},
   565  }
   566  
   567  func TestNewRequestHost(t *testing.T) {
   568  	for i, tt := range newRequestHostTests {
   569  		req, err := NewRequest("GET", tt.in, nil)
   570  		if err != nil {
   571  			t.Errorf("#%v: %v", i, err)
   572  			continue
   573  		}
   574  		if req.Host != tt.out {
   575  			t.Errorf("got %q; want %q", req.Host, tt.out)
   576  		}
   577  	}
   578  }
   579  
   580  func TestRequestInvalidMethod(t *testing.T) {
   581  	_, err := NewRequest("bad method", "http://foo.com/", nil)
   582  	if err == nil {
   583  		t.Error("expected error from NewRequest with invalid method")
   584  	}
   585  	req, err := NewRequest("GET", "http://foo.example/", nil)
   586  	if err != nil {
   587  		t.Fatal(err)
   588  	}
   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)
   593  	}
   594  
   595  	req, err = NewRequest("", "http://foo.com/", nil)
   596  	if err != 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)
   600  	}
   601  }
   602  
   603  func TestNewRequestContentLength(t *testing.T) {
   604  	readByte := func(r io.Reader) io.Reader {
   605  		var b [1]byte
   606  		r.Read(b[:])
   607  		return r
   608  	}
   609  	tests := []struct {
   610  		r    io.Reader
   611  		want int64
   612  	}{
   613  		{bytes.NewReader([]byte("123")), 3},
   614  		{bytes.NewBuffer([]byte("1234")), 4},
   615  		{strings.NewReader("12345"), 5},
   616  		{strings.NewReader(""), 0},
   617  		{NoBody, 0},
   618  
   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
   621  		// unknown.
   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},
   625  	}
   626  	for i, tt := range tests {
   627  		req, err := NewRequest("POST", "http://localhost/", tt.r)
   628  		if err != nil {
   629  			t.Fatal(err)
   630  		}
   631  		if req.ContentLength != tt.want {
   632  			t.Errorf("test[%d]: ContentLength(%T) = %d; want %d", i, tt.r, req.ContentLength, tt.want)
   633  		}
   634  	}
   635  }
   636  
   637  var parseHTTPVersionTests = []struct {
   638  	vers         string
   639  	major, minor int
   640  	ok           bool
   641  }{
   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},
   646  
   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},
   660  }
   661  
   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 {
   667  				major, minor int
   668  				ok           bool
   669  			}
   670  			t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
   671  		}
   672  	}
   673  }
   674  
   675  type getBasicAuthTest struct {
   676  	username, password string
   677  	ok                 bool
   678  }
   679  
   680  type basicAuthCredentialsTest struct {
   681  	username, password string
   682  }
   683  
   684  var getBasicAuthTests = []struct {
   685  	username, password string
   686  	ok                 bool
   687  }{
   688  	{"Aladdin", "open sesame", true},
   689  	{"Aladdin", "open:sesame", true},
   690  	{"", "", true},
   691  }
   692  
   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})
   701  		}
   702  	}
   703  	// Unauthenticated request.
   704  	r, _ := NewRequest("GET", "http://example.com/", nil)
   705  	username, password, ok := r.BasicAuth()
   706  	if ok {
   707  		t.Errorf("expected false from BasicAuth when the request is unauthenticated")
   708  	}
   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})
   713  	}
   714  }
   715  
   716  var parseBasicAuthTests = []struct {
   717  	header, username, password string
   718  	ok                         bool
   719  }{
   720  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   721  
   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},
   725  
   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},
   733  }
   734  
   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})
   743  		}
   744  	}
   745  }
   746  
   747  type logWrites struct {
   748  	t   *testing.T
   749  	dst *[]string
   750  }
   751  
   752  func (l logWrites) WriteByte(c byte) error {
   753  	l.t.Fatalf("unexpected WriteByte call")
   754  	return nil
   755  }
   756  
   757  func (l logWrites) Write(p []byte) (n int, err error) {
   758  	*l.dst = append(*l.dst, string(p))
   759  	return len(p), nil
   760  }
   761  
   762  func TestRequestWriteBufferedWriter(t *testing.T) {
   763  	got := []string{}
   764  	req, _ := NewRequest("GET", "http://foo.com/", nil)
   765  	req.Write(logWrites{t, &got})
   766  	want := []string{
   767  		"GET / HTTP/1.1\r\n",
   768  		"Host: foo.com\r\n",
   769  		"User-Agent: " + DefaultUserAgent + "\r\n",
   770  		"\r\n",
   771  	}
   772  	if !reflect.DeepEqual(got, want) {
   773  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   774  	}
   775  }
   776  
   777  func TestRequestBadHost(t *testing.T) {
   778  	got := []string{}
   779  	req, err := NewRequest("GET", "http://foo/after", nil)
   780  	if err != nil {
   781  		t.Fatal(err)
   782  	}
   783  	req.Host = "foo.com with spaces"
   784  	req.URL.Host = "foo.com with spaces"
   785  	req.Write(logWrites{t, &got})
   786  	want := []string{
   787  		"GET /after HTTP/1.1\r\n",
   788  		"Host: foo.com\r\n",
   789  		"User-Agent: " + DefaultUserAgent + "\r\n",
   790  		"\r\n",
   791  	}
   792  	if !reflect.DeepEqual(got, want) {
   793  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   794  	}
   795  }
   796  
   797  func TestStarRequest(t *testing.T) {
   798  	req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
   799  	if err != nil {
   800  		return
   801  	}
   802  	if req.ContentLength != 0 {
   803  		t.Errorf("ContentLength = %d; want 0", req.ContentLength)
   804  	}
   805  	if req.Body == nil {
   806  		t.Errorf("Body = nil; want non-nil")
   807  	}
   808  
   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
   814  	// Body:
   815  	clientReq := *req
   816  	clientReq.Body = nil
   817  
   818  	var out bytes.Buffer
   819  	if err := clientReq.Write(&out); err != nil {
   820  		t.Fatal(err)
   821  	}
   822  
   823  	if strings.Contains(out.String(), "chunked") {
   824  		t.Error("wrote chunked request; want no body")
   825  	}
   826  	back, err := ReadRequest(bufio.NewReader(bytes.NewReader(out.Bytes())))
   827  	if err != nil {
   828  		t.Fatal(err)
   829  	}
   830  	// Ignore the Headers (the User-Agent breaks the deep equal,
   831  	// but we don't care about it)
   832  	req.Header = nil
   833  	back.Header = nil
   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)
   840  	}
   841  }
   842  
   843  type responseWriterJustWriter struct {
   844  	io.Writer
   845  }
   846  
   847  func (responseWriterJustWriter) Header() Header  { panic("should not be called") }
   848  func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
   849  
   850  // delayedEOFReader never returns (n > 0, io.EOF), instead putting
   851  // off the io.EOF until a subsequent Read call.
   852  type delayedEOFReader struct {
   853  	r io.Reader
   854  }
   855  
   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 {
   859  		err = nil
   860  	}
   861  	return
   862  }
   863  
   864  func TestIssue10884_MaxBytesEOF(t *testing.T) {
   865  	dst := io.Discard
   866  	_, err := io.Copy(dst, MaxBytesReader(
   867  		responseWriterJustWriter{dst},
   868  		io.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
   869  		5))
   870  	if err != nil {
   871  		t.Fatal(err)
   872  	}
   873  }
   874  
   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 {
   879  		var log bytes.Buffer
   880  		buf := make([]byte, 1000)
   881  		var firstErr error
   882  		for {
   883  			n, err := r.Read(buf)
   884  			fmt.Fprintf(&log, "Read(%d) = %d, %v\n", len(buf), n, err)
   885  			if err == nil {
   886  				continue
   887  			}
   888  			if firstErr == nil {
   889  				firstErr = err
   890  				continue
   891  			}
   892  			if !reflect.DeepEqual(err, firstErr) {
   893  				return fmt.Errorf("non-sticky error. got log:\n%s", log.Bytes())
   894  			}
   895  			t.Logf("Got log: %s", log.Bytes())
   896  			return nil
   897  		}
   898  	}
   899  	tests := [...]struct {
   900  		readable int
   901  		limit    int64
   902  	}{
   903  		0: {99, 100},
   904  		1: {100, 100},
   905  		2: {101, 100},
   906  	}
   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)
   911  		}
   912  	}
   913  }
   914  
   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 {
   920  		limit   int64
   921  		lenP    int
   922  		wantN   int
   923  		wantErr bool
   924  	}{
   925  		0: {
   926  			limit:   -123,
   927  			lenP:    0,
   928  			wantN:   0,
   929  			wantErr: false, // Ensure we won't return an error when the limit is negative, but we don't need to read.
   930  		},
   931  		1: {
   932  			limit:   -100,
   933  			lenP:    32 * 1024,
   934  			wantN:   0,
   935  			wantErr: true,
   936  		},
   937  		2: {
   938  			limit:   -2,
   939  			lenP:    1,
   940  			wantN:   0,
   941  			wantErr: true,
   942  		},
   943  		3: {
   944  			limit:   -1,
   945  			lenP:    2,
   946  			wantN:   0,
   947  			wantErr: true,
   948  		},
   949  		4: {
   950  			limit:   0,
   951  			lenP:    3,
   952  			wantN:   0,
   953  			wantErr: true,
   954  		},
   955  		5: {
   956  			limit:   1,
   957  			lenP:    4,
   958  			wantN:   1,
   959  			wantErr: true,
   960  		},
   961  		6: {
   962  			limit:   2,
   963  			lenP:    5,
   964  			wantN:   2,
   965  			wantErr: true,
   966  		},
   967  		7: {
   968  			limit:   3,
   969  			lenP:    2,
   970  			wantN:   2,
   971  			wantErr: false,
   972  		},
   973  		8: {
   974  			limit:   int64(len(testStr)),
   975  			lenP:    len(testStr),
   976  			wantN:   len(testStr),
   977  			wantErr: false,
   978  		},
   979  		9: {
   980  			limit:   100,
   981  			lenP:    6,
   982  			wantN:   len(testStr),
   983  			wantErr: false,
   984  		},
   985  	}
   986  	for i, tt := range tests {
   987  		rc := MaxBytesReader(nil, io.NopCloser(strings.NewReader(testStr)), tt.limit)
   988  
   989  		n, err := rc.Read(make([]byte, tt.lenP))
   990  
   991  		if n != tt.wantN {
   992  			t.Errorf("%d. n: %d, want n: %d", i, n, tt.wantN)
   993  		}
   994  
   995  		if (err != nil) != tt.wantErr {
   996  			t.Errorf("%d. error: %v", i, err)
   997  		}
   998  	}
   999  }
  1000  
  1001  func TestWithContextDeepCopiesURL(t *testing.T) {
  1002  	req, err := NewRequest("POST", "https://golang.org/", nil)
  1003  	if err != nil {
  1004  		t.Fatal(err)
  1005  	}
  1006  
  1007  	reqCopy := req.WithContext(context.Background())
  1008  	reqCopy.URL.Scheme = "http"
  1009  
  1010  	firstURL, secondURL := req.URL.String(), reqCopy.URL.String()
  1011  	if firstURL == secondURL {
  1012  		t.Errorf("unexpected change to original request's URL")
  1013  	}
  1014  
  1015  	// And also check we don't crash on nil (Issue 20601)
  1016  	req.URL = nil
  1017  	reqCopy = req.WithContext(context.Background())
  1018  	if reqCopy.URL != nil {
  1019  		t.Error("expected nil URL in cloned request")
  1020  	}
  1021  }
  1022  
  1023  // Ensure that Request.Clone creates a deep copy of TransferEncoding.
  1024  // See issue 41907.
  1025  func TestRequestCloneTransferEncoding(t *testing.T) {
  1026  	body := strings.NewReader("body")
  1027  	req, _ := NewRequest("POST", "https://example.org/", body)
  1028  	req.TransferEncoding = []string{
  1029  		"encoding1",
  1030  	}
  1031  
  1032  	clonedReq := req.Clone(context.Background())
  1033  	// modify original after deep copy
  1034  	req.TransferEncoding[0] = "encoding2"
  1035  
  1036  	if req.TransferEncoding[0] != "encoding2" {
  1037  		t.Error("expected req.TransferEncoding to be changed")
  1038  	}
  1039  	if clonedReq.TransferEncoding[0] != "encoding1" {
  1040  		t.Error("expected clonedReq.TransferEncoding to be unchanged")
  1041  	}
  1042  }
  1043  
  1044  func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
  1045  	testNoPanicWithBasicAuth(t, h1Mode)
  1046  }
  1047  
  1048  func TestNoPanicOnRoundTripWithBasicAuth_h2(t *testing.T) {
  1049  	testNoPanicWithBasicAuth(t, h2Mode)
  1050  }
  1051  
  1052  // Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
  1053  func testNoPanicWithBasicAuth(t *testing.T, h2 bool) {
  1054  	defer afterTest(t)
  1055  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {}))
  1056  	defer cst.close()
  1057  
  1058  	u, err := url.Parse(cst.ts.URL)
  1059  	if err != nil {
  1060  		t.Fatal(err)
  1061  	}
  1062  	u.User = url.UserPassword("foo", "bar")
  1063  	req := &Request{
  1064  		URL:    u,
  1065  		Method: "GET",
  1066  	}
  1067  	if _, err := cst.c.Do(req); err != nil {
  1068  		t.Fatalf("Unexpected error: %v", err)
  1069  	}
  1070  }
  1071  
  1072  // verify that NewRequest sets Request.GetBody and that it works
  1073  func TestNewRequestGetBody(t *testing.T) {
  1074  	tests := []struct {
  1075  		r io.Reader
  1076  	}{
  1077  		{r: strings.NewReader("hello")},
  1078  		{r: bytes.NewReader([]byte("hello"))},
  1079  		{r: bytes.NewBuffer([]byte("hello"))},
  1080  	}
  1081  	for i, tt := range tests {
  1082  		req, err := NewRequest("POST", "http://foo.tld/", tt.r)
  1083  		if err != nil {
  1084  			t.Errorf("test[%d]: %v", i, err)
  1085  			continue
  1086  		}
  1087  		if req.Body == nil {
  1088  			t.Errorf("test[%d]: Body = nil", i)
  1089  			continue
  1090  		}
  1091  		if req.GetBody == nil {
  1092  			t.Errorf("test[%d]: GetBody = nil", i)
  1093  			continue
  1094  		}
  1095  		slurp1, err := io.ReadAll(req.Body)
  1096  		if err != nil {
  1097  			t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
  1098  		}
  1099  		newBody, err := req.GetBody()
  1100  		if err != nil {
  1101  			t.Errorf("test[%d]: GetBody = %v", i, err)
  1102  		}
  1103  		slurp2, err := io.ReadAll(newBody)
  1104  		if err != nil {
  1105  			t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
  1106  		}
  1107  		if string(slurp1) != string(slurp2) {
  1108  			t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
  1109  		}
  1110  	}
  1111  }
  1112  
  1113  func testMissingFile(t *testing.T, req *Request) {
  1114  	f, fh, err := req.FormFile("missing")
  1115  	if f != nil {
  1116  		t.Errorf("FormFile file = %v, want nil", f)
  1117  	}
  1118  	if fh != nil {
  1119  		t.Errorf("FormFile file header = %q, want nil", fh)
  1120  	}
  1121  	if err != ErrMissingFile {
  1122  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
  1123  	}
  1124  }
  1125  
  1126  func newTestMultipartRequest(t *testing.T) *Request {
  1127  	b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
  1128  	req, err := NewRequest("POST", "/", b)
  1129  	if err != nil {
  1130  		t.Fatal("NewRequest:", err)
  1131  	}
  1132  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
  1133  	req.Header.Set("Content-type", ctype)
  1134  	return req
  1135  }
  1136  
  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)
  1140  	}
  1141  	if g, e := req.FormValue("textb"), textbValue; g != e {
  1142  		t.Errorf("textb value = %q, want %q", g, e)
  1143  	}
  1144  	if g := req.FormValue("missing"); g != "" {
  1145  		t.Errorf("missing value = %q, want empty string", g)
  1146  	}
  1147  
  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")
  1151  		}
  1152  	}
  1153  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
  1154  	defer fda.Close()
  1155  	assertMem("filea", fda)
  1156  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
  1157  	defer fdb.Close()
  1158  	if allMem {
  1159  		assertMem("fileb", fdb)
  1160  	} else {
  1161  		if _, ok := fdb.(*os.File); !ok {
  1162  			t.Errorf("fileb has unexpected underlying type %T", fdb)
  1163  		}
  1164  	}
  1165  
  1166  	testMissingFile(t, req)
  1167  }
  1168  
  1169  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
  1170  	f, fh, err := req.FormFile(key)
  1171  	if err != nil {
  1172  		t.Fatalf("FormFile(%q): %q", key, err)
  1173  	}
  1174  	if fh.Filename != expectFilename {
  1175  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
  1176  	}
  1177  	var b bytes.Buffer
  1178  	_, err = io.Copy(&b, f)
  1179  	if err != nil {
  1180  		t.Fatal("copying contents:", err)
  1181  	}
  1182  	if g := b.String(); g != expectContent {
  1183  		t.Errorf("contents = %q, want %q", g, expectContent)
  1184  	}
  1185  	return f
  1186  }
  1187  
  1188  const (
  1189  	fileaContents = "This is a test file."
  1190  	filebContents = "Another test file."
  1191  	textaValue    = "foo"
  1192  	textbValue    = "bar"
  1193  	boundary      = `MyBoundary`
  1194  )
  1195  
  1196  const message = `
  1197  --MyBoundary
  1198  Content-Disposition: form-data; name="filea"; filename="filea.txt"
  1199  Content-Type: text/plain
  1200  
  1201  ` + fileaContents + `
  1202  --MyBoundary
  1203  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
  1204  Content-Type: text/plain
  1205  
  1206  ` + filebContents + `
  1207  --MyBoundary
  1208  Content-Disposition: form-data; name="texta"
  1209  
  1210  ` + textaValue + `
  1211  --MyBoundary
  1212  Content-Disposition: form-data; name="textb"
  1213  
  1214  ` + textbValue + `
  1215  --MyBoundary--
  1216  `
  1217  
  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)})
  1223  	b.ReportAllocs()
  1224  	b.ResetTimer()
  1225  	for i := 0; i < b.N; i++ {
  1226  		_, err := ReadRequest(r)
  1227  		if err != nil {
  1228  			b.Fatalf("failed to read request: %v", err)
  1229  		}
  1230  	}
  1231  }
  1232  
  1233  // infiniteReader satisfies Read requests as if the contents of buf
  1234  // loop indefinitely.
  1235  type infiniteReader struct {
  1236  	buf    []byte
  1237  	offset int
  1238  }
  1239  
  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)
  1243  	return n, nil
  1244  }
  1245  
  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
  1257  `)
  1258  }
  1259  
  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
  1265  Accept: */*
  1266  `)
  1267  }
  1268  
  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
  1274  Accept: */*
  1275  `)
  1276  }
  1277  
  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
  1282  Accept: */*
  1283  Accept-Encoding: gzip
  1284  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
  1285  Connection: keep-alive
  1286  `)
  1287  }
  1288  
  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
  1293  `)
  1294  }
  1295  
  1296  const (
  1297  	withTLS = true
  1298  	noTLS   = false
  1299  )
  1300  
  1301  func BenchmarkFileAndServer_1KB(b *testing.B) {
  1302  	benchmarkFileAndServer(b, 1<<10)
  1303  }
  1304  
  1305  func BenchmarkFileAndServer_16MB(b *testing.B) {
  1306  	benchmarkFileAndServer(b, 1<<24)
  1307  }
  1308  
  1309  func BenchmarkFileAndServer_64MB(b *testing.B) {
  1310  	benchmarkFileAndServer(b, 1<<26)
  1311  }
  1312  
  1313  func benchmarkFileAndServer(b *testing.B, n int64) {
  1314  	f, err := os.CreateTemp(os.TempDir(), "go-bench-http-file-and-server")
  1315  	if err != nil {
  1316  		b.Fatalf("Failed to create temp file: %v", err)
  1317  	}
  1318  
  1319  	defer func() {
  1320  		f.Close()
  1321  		os.RemoveAll(f.Name())
  1322  	}()
  1323  
  1324  	if _, err := io.CopyN(f, rand.Reader, n); err != nil {
  1325  		b.Fatalf("Failed to copy %d bytes: %v", n, err)
  1326  	}
  1327  
  1328  	b.Run("NoTLS", func(b *testing.B) {
  1329  		runFileAndServerBenchmarks(b, noTLS, f, n)
  1330  	})
  1331  
  1332  	b.Run("TLS", func(b *testing.B) {
  1333  		runFileAndServerBenchmarks(b, withTLS, f, n)
  1334  	})
  1335  }
  1336  
  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)
  1341  		if err != nil {
  1342  			panic(err)
  1343  		}
  1344  
  1345  		if nc != n {
  1346  			panic(fmt.Errorf("Copied %d Wanted %d bytes", nc, n))
  1347  		}
  1348  	})
  1349  
  1350  	var cst *httptest.Server
  1351  	if tlsOption == withTLS {
  1352  		cst = httptest.NewTLSServer(handler)
  1353  	} else {
  1354  		cst = httptest.NewServer(handler)
  1355  	}
  1356  
  1357  	defer cst.Close()
  1358  	b.ResetTimer()
  1359  	for i := 0; i < b.N; i++ {
  1360  		// Perform some setup.
  1361  		b.StopTimer()
  1362  		if _, err := f.Seek(0, 0); err != nil {
  1363  			b.Fatalf("Failed to seek back to file: %v", err)
  1364  		}
  1365  
  1366  		b.StartTimer()
  1367  		req, err := NewRequest("PUT", cst.URL, io.NopCloser(f))
  1368  		if err != nil {
  1369  			b.Fatal(err)
  1370  		}
  1371  
  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)
  1376  		if err != nil {
  1377  			b.Fatalf("Failed to make request to backend: %v", err)
  1378  		}
  1379  
  1380  		res.Body.Close()
  1381  		b.SetBytes(n)
  1382  	}
  1383  }
  1384  

View as plain text