Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/http/request_test.go

Documentation: net/http

     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.9", 0, 9, true},
   643  	{"HTTP/1.0", 1, 0, true},
   644  	{"HTTP/1.1", 1, 1, true},
   645  	{"HTTP/3.14", 3, 14, 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  }
   655  
   656  func TestParseHTTPVersion(t *testing.T) {
   657  	for _, tt := range parseHTTPVersionTests {
   658  		major, minor, ok := ParseHTTPVersion(tt.vers)
   659  		if ok != tt.ok || major != tt.major || minor != tt.minor {
   660  			type version struct {
   661  				major, minor int
   662  				ok           bool
   663  			}
   664  			t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
   665  		}
   666  	}
   667  }
   668  
   669  type getBasicAuthTest struct {
   670  	username, password string
   671  	ok                 bool
   672  }
   673  
   674  type basicAuthCredentialsTest struct {
   675  	username, password string
   676  }
   677  
   678  var getBasicAuthTests = []struct {
   679  	username, password string
   680  	ok                 bool
   681  }{
   682  	{"Aladdin", "open sesame", true},
   683  	{"Aladdin", "open:sesame", true},
   684  	{"", "", true},
   685  }
   686  
   687  func TestGetBasicAuth(t *testing.T) {
   688  	for _, tt := range getBasicAuthTests {
   689  		r, _ := NewRequest("GET", "http://example.com/", nil)
   690  		r.SetBasicAuth(tt.username, tt.password)
   691  		username, password, ok := r.BasicAuth()
   692  		if ok != tt.ok || username != tt.username || password != tt.password {
   693  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   694  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   695  		}
   696  	}
   697  	// Unauthenticated request.
   698  	r, _ := NewRequest("GET", "http://example.com/", nil)
   699  	username, password, ok := r.BasicAuth()
   700  	if ok {
   701  		t.Errorf("expected false from BasicAuth when the request is unauthenticated")
   702  	}
   703  	want := basicAuthCredentialsTest{"", ""}
   704  	if username != want.username || password != want.password {
   705  		t.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
   706  			want, basicAuthCredentialsTest{username, password})
   707  	}
   708  }
   709  
   710  var parseBasicAuthTests = []struct {
   711  	header, username, password string
   712  	ok                         bool
   713  }{
   714  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   715  
   716  	// Case doesn't matter:
   717  	{"BASIC " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   718  	{"basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   719  
   720  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
   721  	{"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
   722  	{"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   723  	{base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   724  	{"Basic ", "", "", false},
   725  	{"Basic Aladdin:open sesame", "", "", false},
   726  	{`Digest username="Aladdin"`, "", "", false},
   727  }
   728  
   729  func TestParseBasicAuth(t *testing.T) {
   730  	for _, tt := range parseBasicAuthTests {
   731  		r, _ := NewRequest("GET", "http://example.com/", nil)
   732  		r.Header.Set("Authorization", tt.header)
   733  		username, password, ok := r.BasicAuth()
   734  		if ok != tt.ok || username != tt.username || password != tt.password {
   735  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   736  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   737  		}
   738  	}
   739  }
   740  
   741  type logWrites struct {
   742  	t   *testing.T
   743  	dst *[]string
   744  }
   745  
   746  func (l logWrites) WriteByte(c byte) error {
   747  	l.t.Fatalf("unexpected WriteByte call")
   748  	return nil
   749  }
   750  
   751  func (l logWrites) Write(p []byte) (n int, err error) {
   752  	*l.dst = append(*l.dst, string(p))
   753  	return len(p), nil
   754  }
   755  
   756  func TestRequestWriteBufferedWriter(t *testing.T) {
   757  	got := []string{}
   758  	req, _ := NewRequest("GET", "http://foo.com/", nil)
   759  	req.Write(logWrites{t, &got})
   760  	want := []string{
   761  		"GET / HTTP/1.1\r\n",
   762  		"Host: foo.com\r\n",
   763  		"User-Agent: " + DefaultUserAgent + "\r\n",
   764  		"\r\n",
   765  	}
   766  	if !reflect.DeepEqual(got, want) {
   767  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   768  	}
   769  }
   770  
   771  func TestRequestBadHost(t *testing.T) {
   772  	got := []string{}
   773  	req, err := NewRequest("GET", "http://foo/after", nil)
   774  	if err != nil {
   775  		t.Fatal(err)
   776  	}
   777  	req.Host = "foo.com with spaces"
   778  	req.URL.Host = "foo.com with spaces"
   779  	req.Write(logWrites{t, &got})
   780  	want := []string{
   781  		"GET /after HTTP/1.1\r\n",
   782  		"Host: foo.com\r\n",
   783  		"User-Agent: " + DefaultUserAgent + "\r\n",
   784  		"\r\n",
   785  	}
   786  	if !reflect.DeepEqual(got, want) {
   787  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   788  	}
   789  }
   790  
   791  func TestStarRequest(t *testing.T) {
   792  	req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
   793  	if err != nil {
   794  		return
   795  	}
   796  	if req.ContentLength != 0 {
   797  		t.Errorf("ContentLength = %d; want 0", req.ContentLength)
   798  	}
   799  	if req.Body == nil {
   800  		t.Errorf("Body = nil; want non-nil")
   801  	}
   802  
   803  	// Request.Write has Client semantics for Body/ContentLength,
   804  	// where ContentLength 0 means unknown if Body is non-nil, and
   805  	// thus chunking will happen unless we change semantics and
   806  	// signal that we want to serialize it as exactly zero.  The
   807  	// only way to do that for outbound requests is with a nil
   808  	// Body:
   809  	clientReq := *req
   810  	clientReq.Body = nil
   811  
   812  	var out bytes.Buffer
   813  	if err := clientReq.Write(&out); err != nil {
   814  		t.Fatal(err)
   815  	}
   816  
   817  	if strings.Contains(out.String(), "chunked") {
   818  		t.Error("wrote chunked request; want no body")
   819  	}
   820  	back, err := ReadRequest(bufio.NewReader(bytes.NewReader(out.Bytes())))
   821  	if err != nil {
   822  		t.Fatal(err)
   823  	}
   824  	// Ignore the Headers (the User-Agent breaks the deep equal,
   825  	// but we don't care about it)
   826  	req.Header = nil
   827  	back.Header = nil
   828  	if !reflect.DeepEqual(req, back) {
   829  		t.Errorf("Original request doesn't match Request read back.")
   830  		t.Logf("Original: %#v", req)
   831  		t.Logf("Original.URL: %#v", req.URL)
   832  		t.Logf("Wrote: %s", out.Bytes())
   833  		t.Logf("Read back (doesn't match Original): %#v", back)
   834  	}
   835  }
   836  
   837  type responseWriterJustWriter struct {
   838  	io.Writer
   839  }
   840  
   841  func (responseWriterJustWriter) Header() Header  { panic("should not be called") }
   842  func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
   843  
   844  // delayedEOFReader never returns (n > 0, io.EOF), instead putting
   845  // off the io.EOF until a subsequent Read call.
   846  type delayedEOFReader struct {
   847  	r io.Reader
   848  }
   849  
   850  func (dr delayedEOFReader) Read(p []byte) (n int, err error) {
   851  	n, err = dr.r.Read(p)
   852  	if n > 0 && err == io.EOF {
   853  		err = nil
   854  	}
   855  	return
   856  }
   857  
   858  func TestIssue10884_MaxBytesEOF(t *testing.T) {
   859  	dst := io.Discard
   860  	_, err := io.Copy(dst, MaxBytesReader(
   861  		responseWriterJustWriter{dst},
   862  		io.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
   863  		5))
   864  	if err != nil {
   865  		t.Fatal(err)
   866  	}
   867  }
   868  
   869  // Issue 14981: MaxBytesReader's return error wasn't sticky. It
   870  // doesn't technically need to be, but people expected it to be.
   871  func TestMaxBytesReaderStickyError(t *testing.T) {
   872  	isSticky := func(r io.Reader) error {
   873  		var log bytes.Buffer
   874  		buf := make([]byte, 1000)
   875  		var firstErr error
   876  		for {
   877  			n, err := r.Read(buf)
   878  			fmt.Fprintf(&log, "Read(%d) = %d, %v\n", len(buf), n, err)
   879  			if err == nil {
   880  				continue
   881  			}
   882  			if firstErr == nil {
   883  				firstErr = err
   884  				continue
   885  			}
   886  			if !reflect.DeepEqual(err, firstErr) {
   887  				return fmt.Errorf("non-sticky error. got log:\n%s", log.Bytes())
   888  			}
   889  			t.Logf("Got log: %s", log.Bytes())
   890  			return nil
   891  		}
   892  	}
   893  	tests := [...]struct {
   894  		readable int
   895  		limit    int64
   896  	}{
   897  		0: {99, 100},
   898  		1: {100, 100},
   899  		2: {101, 100},
   900  	}
   901  	for i, tt := range tests {
   902  		rc := MaxBytesReader(nil, io.NopCloser(bytes.NewReader(make([]byte, tt.readable))), tt.limit)
   903  		if err := isSticky(rc); err != nil {
   904  			t.Errorf("%d. error: %v", i, err)
   905  		}
   906  	}
   907  }
   908  
   909  // Issue 45101: maxBytesReader's Read panicked when n < -1. This test
   910  // also ensures that Read treats negative limits as equivalent to 0.
   911  func TestMaxBytesReaderDifferentLimits(t *testing.T) {
   912  	const testStr = "1234"
   913  	tests := [...]struct {
   914  		limit   int64
   915  		lenP    int
   916  		wantN   int
   917  		wantErr bool
   918  	}{
   919  		0: {
   920  			limit:   -123,
   921  			lenP:    0,
   922  			wantN:   0,
   923  			wantErr: false, // Ensure we won't return an error when the limit is negative, but we don't need to read.
   924  		},
   925  		1: {
   926  			limit:   -100,
   927  			lenP:    32 * 1024,
   928  			wantN:   0,
   929  			wantErr: true,
   930  		},
   931  		2: {
   932  			limit:   -2,
   933  			lenP:    1,
   934  			wantN:   0,
   935  			wantErr: true,
   936  		},
   937  		3: {
   938  			limit:   -1,
   939  			lenP:    2,
   940  			wantN:   0,
   941  			wantErr: true,
   942  		},
   943  		4: {
   944  			limit:   0,
   945  			lenP:    3,
   946  			wantN:   0,
   947  			wantErr: true,
   948  		},
   949  		5: {
   950  			limit:   1,
   951  			lenP:    4,
   952  			wantN:   1,
   953  			wantErr: true,
   954  		},
   955  		6: {
   956  			limit:   2,
   957  			lenP:    5,
   958  			wantN:   2,
   959  			wantErr: true,
   960  		},
   961  		7: {
   962  			limit:   3,
   963  			lenP:    2,
   964  			wantN:   2,
   965  			wantErr: false,
   966  		},
   967  		8: {
   968  			limit:   int64(len(testStr)),
   969  			lenP:    len(testStr),
   970  			wantN:   len(testStr),
   971  			wantErr: false,
   972  		},
   973  		9: {
   974  			limit:   100,
   975  			lenP:    6,
   976  			wantN:   len(testStr),
   977  			wantErr: false,
   978  		},
   979  	}
   980  	for i, tt := range tests {
   981  		rc := MaxBytesReader(nil, io.NopCloser(strings.NewReader(testStr)), tt.limit)
   982  
   983  		n, err := rc.Read(make([]byte, tt.lenP))
   984  
   985  		if n != tt.wantN {
   986  			t.Errorf("%d. n: %d, want n: %d", i, n, tt.wantN)
   987  		}
   988  
   989  		if (err != nil) != tt.wantErr {
   990  			t.Errorf("%d. error: %v", i, err)
   991  		}
   992  	}
   993  }
   994  
   995  func TestWithContextDeepCopiesURL(t *testing.T) {
   996  	req, err := NewRequest("POST", "https://golang.org/", nil)
   997  	if err != nil {
   998  		t.Fatal(err)
   999  	}
  1000  
  1001  	reqCopy := req.WithContext(context.Background())
  1002  	reqCopy.URL.Scheme = "http"
  1003  
  1004  	firstURL, secondURL := req.URL.String(), reqCopy.URL.String()
  1005  	if firstURL == secondURL {
  1006  		t.Errorf("unexpected change to original request's URL")
  1007  	}
  1008  
  1009  	// And also check we don't crash on nil (Issue 20601)
  1010  	req.URL = nil
  1011  	reqCopy = req.WithContext(context.Background())
  1012  	if reqCopy.URL != nil {
  1013  		t.Error("expected nil URL in cloned request")
  1014  	}
  1015  }
  1016  
  1017  // Ensure that Request.Clone creates a deep copy of TransferEncoding.
  1018  // See issue 41907.
  1019  func TestRequestCloneTransferEncoding(t *testing.T) {
  1020  	body := strings.NewReader("body")
  1021  	req, _ := NewRequest("POST", "https://example.org/", body)
  1022  	req.TransferEncoding = []string{
  1023  		"encoding1",
  1024  	}
  1025  
  1026  	clonedReq := req.Clone(context.Background())
  1027  	// modify original after deep copy
  1028  	req.TransferEncoding[0] = "encoding2"
  1029  
  1030  	if req.TransferEncoding[0] != "encoding2" {
  1031  		t.Error("expected req.TransferEncoding to be changed")
  1032  	}
  1033  	if clonedReq.TransferEncoding[0] != "encoding1" {
  1034  		t.Error("expected clonedReq.TransferEncoding to be unchanged")
  1035  	}
  1036  }
  1037  
  1038  func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
  1039  	testNoPanicWithBasicAuth(t, h1Mode)
  1040  }
  1041  
  1042  func TestNoPanicOnRoundTripWithBasicAuth_h2(t *testing.T) {
  1043  	testNoPanicWithBasicAuth(t, h2Mode)
  1044  }
  1045  
  1046  // Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
  1047  func testNoPanicWithBasicAuth(t *testing.T, h2 bool) {
  1048  	defer afterTest(t)
  1049  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {}))
  1050  	defer cst.close()
  1051  
  1052  	u, err := url.Parse(cst.ts.URL)
  1053  	if err != nil {
  1054  		t.Fatal(err)
  1055  	}
  1056  	u.User = url.UserPassword("foo", "bar")
  1057  	req := &Request{
  1058  		URL:    u,
  1059  		Method: "GET",
  1060  	}
  1061  	if _, err := cst.c.Do(req); err != nil {
  1062  		t.Fatalf("Unexpected error: %v", err)
  1063  	}
  1064  }
  1065  
  1066  // verify that NewRequest sets Request.GetBody and that it works
  1067  func TestNewRequestGetBody(t *testing.T) {
  1068  	tests := []struct {
  1069  		r io.Reader
  1070  	}{
  1071  		{r: strings.NewReader("hello")},
  1072  		{r: bytes.NewReader([]byte("hello"))},
  1073  		{r: bytes.NewBuffer([]byte("hello"))},
  1074  	}
  1075  	for i, tt := range tests {
  1076  		req, err := NewRequest("POST", "http://foo.tld/", tt.r)
  1077  		if err != nil {
  1078  			t.Errorf("test[%d]: %v", i, err)
  1079  			continue
  1080  		}
  1081  		if req.Body == nil {
  1082  			t.Errorf("test[%d]: Body = nil", i)
  1083  			continue
  1084  		}
  1085  		if req.GetBody == nil {
  1086  			t.Errorf("test[%d]: GetBody = nil", i)
  1087  			continue
  1088  		}
  1089  		slurp1, err := io.ReadAll(req.Body)
  1090  		if err != nil {
  1091  			t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
  1092  		}
  1093  		newBody, err := req.GetBody()
  1094  		if err != nil {
  1095  			t.Errorf("test[%d]: GetBody = %v", i, err)
  1096  		}
  1097  		slurp2, err := io.ReadAll(newBody)
  1098  		if err != nil {
  1099  			t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
  1100  		}
  1101  		if string(slurp1) != string(slurp2) {
  1102  			t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
  1103  		}
  1104  	}
  1105  }
  1106  
  1107  func testMissingFile(t *testing.T, req *Request) {
  1108  	f, fh, err := req.FormFile("missing")
  1109  	if f != nil {
  1110  		t.Errorf("FormFile file = %v, want nil", f)
  1111  	}
  1112  	if fh != nil {
  1113  		t.Errorf("FormFile file header = %q, want nil", fh)
  1114  	}
  1115  	if err != ErrMissingFile {
  1116  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
  1117  	}
  1118  }
  1119  
  1120  func newTestMultipartRequest(t *testing.T) *Request {
  1121  	b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
  1122  	req, err := NewRequest("POST", "/", b)
  1123  	if err != nil {
  1124  		t.Fatal("NewRequest:", err)
  1125  	}
  1126  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
  1127  	req.Header.Set("Content-type", ctype)
  1128  	return req
  1129  }
  1130  
  1131  func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
  1132  	if g, e := req.FormValue("texta"), textaValue; g != e {
  1133  		t.Errorf("texta value = %q, want %q", g, e)
  1134  	}
  1135  	if g, e := req.FormValue("textb"), textbValue; g != e {
  1136  		t.Errorf("textb value = %q, want %q", g, e)
  1137  	}
  1138  	if g := req.FormValue("missing"); g != "" {
  1139  		t.Errorf("missing value = %q, want empty string", g)
  1140  	}
  1141  
  1142  	assertMem := func(n string, fd multipart.File) {
  1143  		if _, ok := fd.(*os.File); ok {
  1144  			t.Error(n, " is *os.File, should not be")
  1145  		}
  1146  	}
  1147  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
  1148  	defer fda.Close()
  1149  	assertMem("filea", fda)
  1150  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
  1151  	defer fdb.Close()
  1152  	if allMem {
  1153  		assertMem("fileb", fdb)
  1154  	} else {
  1155  		if _, ok := fdb.(*os.File); !ok {
  1156  			t.Errorf("fileb has unexpected underlying type %T", fdb)
  1157  		}
  1158  	}
  1159  
  1160  	testMissingFile(t, req)
  1161  }
  1162  
  1163  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
  1164  	f, fh, err := req.FormFile(key)
  1165  	if err != nil {
  1166  		t.Fatalf("FormFile(%q): %q", key, err)
  1167  	}
  1168  	if fh.Filename != expectFilename {
  1169  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
  1170  	}
  1171  	var b bytes.Buffer
  1172  	_, err = io.Copy(&b, f)
  1173  	if err != nil {
  1174  		t.Fatal("copying contents:", err)
  1175  	}
  1176  	if g := b.String(); g != expectContent {
  1177  		t.Errorf("contents = %q, want %q", g, expectContent)
  1178  	}
  1179  	return f
  1180  }
  1181  
  1182  const (
  1183  	fileaContents = "This is a test file."
  1184  	filebContents = "Another test file."
  1185  	textaValue    = "foo"
  1186  	textbValue    = "bar"
  1187  	boundary      = `MyBoundary`
  1188  )
  1189  
  1190  const message = `
  1191  --MyBoundary
  1192  Content-Disposition: form-data; name="filea"; filename="filea.txt"
  1193  Content-Type: text/plain
  1194  
  1195  ` + fileaContents + `
  1196  --MyBoundary
  1197  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
  1198  Content-Type: text/plain
  1199  
  1200  ` + filebContents + `
  1201  --MyBoundary
  1202  Content-Disposition: form-data; name="texta"
  1203  
  1204  ` + textaValue + `
  1205  --MyBoundary
  1206  Content-Disposition: form-data; name="textb"
  1207  
  1208  ` + textbValue + `
  1209  --MyBoundary--
  1210  `
  1211  
  1212  func benchmarkReadRequest(b *testing.B, request string) {
  1213  	request = request + "\n"                            // final \n
  1214  	request = strings.ReplaceAll(request, "\n", "\r\n") // expand \n to \r\n
  1215  	b.SetBytes(int64(len(request)))
  1216  	r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
  1217  	b.ReportAllocs()
  1218  	b.ResetTimer()
  1219  	for i := 0; i < b.N; i++ {
  1220  		_, err := ReadRequest(r)
  1221  		if err != nil {
  1222  			b.Fatalf("failed to read request: %v", err)
  1223  		}
  1224  	}
  1225  }
  1226  
  1227  // infiniteReader satisfies Read requests as if the contents of buf
  1228  // loop indefinitely.
  1229  type infiniteReader struct {
  1230  	buf    []byte
  1231  	offset int
  1232  }
  1233  
  1234  func (r *infiniteReader) Read(b []byte) (int, error) {
  1235  	n := copy(b, r.buf[r.offset:])
  1236  	r.offset = (r.offset + n) % len(r.buf)
  1237  	return n, nil
  1238  }
  1239  
  1240  func BenchmarkReadRequestChrome(b *testing.B) {
  1241  	// https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
  1242  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1243  Host: localhost:8080
  1244  Connection: keep-alive
  1245  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  1246  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
  1247  Accept-Encoding: gzip,deflate,sdch
  1248  Accept-Language: en-US,en;q=0.8
  1249  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  1250  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
  1251  `)
  1252  }
  1253  
  1254  func BenchmarkReadRequestCurl(b *testing.B) {
  1255  	// curl http://localhost:8080/
  1256  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1257  User-Agent: curl/7.27.0
  1258  Host: localhost:8080
  1259  Accept: */*
  1260  `)
  1261  }
  1262  
  1263  func BenchmarkReadRequestApachebench(b *testing.B) {
  1264  	// ab -n 1 -c 1 http://localhost:8080/
  1265  	benchmarkReadRequest(b, `GET / HTTP/1.0
  1266  Host: localhost:8080
  1267  User-Agent: ApacheBench/2.3
  1268  Accept: */*
  1269  `)
  1270  }
  1271  
  1272  func BenchmarkReadRequestSiege(b *testing.B) {
  1273  	// siege -r 1 -c 1 http://localhost:8080/
  1274  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1275  Host: localhost:8080
  1276  Accept: */*
  1277  Accept-Encoding: gzip
  1278  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
  1279  Connection: keep-alive
  1280  `)
  1281  }
  1282  
  1283  func BenchmarkReadRequestWrk(b *testing.B) {
  1284  	// wrk -t 1 -r 1 -c 1 http://localhost:8080/
  1285  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1286  Host: localhost:8080
  1287  `)
  1288  }
  1289  
  1290  const (
  1291  	withTLS = true
  1292  	noTLS   = false
  1293  )
  1294  
  1295  func BenchmarkFileAndServer_1KB(b *testing.B) {
  1296  	benchmarkFileAndServer(b, 1<<10)
  1297  }
  1298  
  1299  func BenchmarkFileAndServer_16MB(b *testing.B) {
  1300  	benchmarkFileAndServer(b, 1<<24)
  1301  }
  1302  
  1303  func BenchmarkFileAndServer_64MB(b *testing.B) {
  1304  	benchmarkFileAndServer(b, 1<<26)
  1305  }
  1306  
  1307  func benchmarkFileAndServer(b *testing.B, n int64) {
  1308  	f, err := os.CreateTemp(os.TempDir(), "go-bench-http-file-and-server")
  1309  	if err != nil {
  1310  		b.Fatalf("Failed to create temp file: %v", err)
  1311  	}
  1312  
  1313  	defer func() {
  1314  		f.Close()
  1315  		os.RemoveAll(f.Name())
  1316  	}()
  1317  
  1318  	if _, err := io.CopyN(f, rand.Reader, n); err != nil {
  1319  		b.Fatalf("Failed to copy %d bytes: %v", n, err)
  1320  	}
  1321  
  1322  	b.Run("NoTLS", func(b *testing.B) {
  1323  		runFileAndServerBenchmarks(b, noTLS, f, n)
  1324  	})
  1325  
  1326  	b.Run("TLS", func(b *testing.B) {
  1327  		runFileAndServerBenchmarks(b, withTLS, f, n)
  1328  	})
  1329  }
  1330  
  1331  func runFileAndServerBenchmarks(b *testing.B, tlsOption bool, f *os.File, n int64) {
  1332  	handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
  1333  		defer req.Body.Close()
  1334  		nc, err := io.Copy(io.Discard, req.Body)
  1335  		if err != nil {
  1336  			panic(err)
  1337  		}
  1338  
  1339  		if nc != n {
  1340  			panic(fmt.Errorf("Copied %d Wanted %d bytes", nc, n))
  1341  		}
  1342  	})
  1343  
  1344  	var cst *httptest.Server
  1345  	if tlsOption == withTLS {
  1346  		cst = httptest.NewTLSServer(handler)
  1347  	} else {
  1348  		cst = httptest.NewServer(handler)
  1349  	}
  1350  
  1351  	defer cst.Close()
  1352  	b.ResetTimer()
  1353  	for i := 0; i < b.N; i++ {
  1354  		// Perform some setup.
  1355  		b.StopTimer()
  1356  		if _, err := f.Seek(0, 0); err != nil {
  1357  			b.Fatalf("Failed to seek back to file: %v", err)
  1358  		}
  1359  
  1360  		b.StartTimer()
  1361  		req, err := NewRequest("PUT", cst.URL, io.NopCloser(f))
  1362  		if err != nil {
  1363  			b.Fatal(err)
  1364  		}
  1365  
  1366  		req.ContentLength = n
  1367  		// Prevent mime sniffing by setting the Content-Type.
  1368  		req.Header.Set("Content-Type", "application/octet-stream")
  1369  		res, err := cst.Client().Do(req)
  1370  		if err != nil {
  1371  			b.Fatalf("Failed to make request to backend: %v", err)
  1372  		}
  1373  
  1374  		res.Body.Close()
  1375  		b.SetBytes(n)
  1376  	}
  1377  }
  1378  

View as plain text