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 TestWithContextNilURL(t *testing.T) {
  1002  	req, err := NewRequest("POST", "https://golang.org/", nil)
  1003  	if err != nil {
  1004  		t.Fatal(err)
  1005  	}
  1006  
  1007  	// Issue 20601
  1008  	req.URL = nil
  1009  	reqCopy := req.WithContext(context.Background())
  1010  	if reqCopy.URL != nil {
  1011  		t.Error("expected nil URL in cloned request")
  1012  	}
  1013  }
  1014  
  1015  // Ensure that Request.Clone creates a deep copy of TransferEncoding.
  1016  // See issue 41907.
  1017  func TestRequestCloneTransferEncoding(t *testing.T) {
  1018  	body := strings.NewReader("body")
  1019  	req, _ := NewRequest("POST", "https://example.org/", body)
  1020  	req.TransferEncoding = []string{
  1021  		"encoding1",
  1022  	}
  1023  
  1024  	clonedReq := req.Clone(context.Background())
  1025  	// modify original after deep copy
  1026  	req.TransferEncoding[0] = "encoding2"
  1027  
  1028  	if req.TransferEncoding[0] != "encoding2" {
  1029  		t.Error("expected req.TransferEncoding to be changed")
  1030  	}
  1031  	if clonedReq.TransferEncoding[0] != "encoding1" {
  1032  		t.Error("expected clonedReq.TransferEncoding to be unchanged")
  1033  	}
  1034  }
  1035  
  1036  func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
  1037  	testNoPanicWithBasicAuth(t, h1Mode)
  1038  }
  1039  
  1040  func TestNoPanicOnRoundTripWithBasicAuth_h2(t *testing.T) {
  1041  	testNoPanicWithBasicAuth(t, h2Mode)
  1042  }
  1043  
  1044  // Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
  1045  func testNoPanicWithBasicAuth(t *testing.T, h2 bool) {
  1046  	defer afterTest(t)
  1047  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {}))
  1048  	defer cst.close()
  1049  
  1050  	u, err := url.Parse(cst.ts.URL)
  1051  	if err != nil {
  1052  		t.Fatal(err)
  1053  	}
  1054  	u.User = url.UserPassword("foo", "bar")
  1055  	req := &Request{
  1056  		URL:    u,
  1057  		Method: "GET",
  1058  	}
  1059  	if _, err := cst.c.Do(req); err != nil {
  1060  		t.Fatalf("Unexpected error: %v", err)
  1061  	}
  1062  }
  1063  
  1064  // verify that NewRequest sets Request.GetBody and that it works
  1065  func TestNewRequestGetBody(t *testing.T) {
  1066  	tests := []struct {
  1067  		r io.Reader
  1068  	}{
  1069  		{r: strings.NewReader("hello")},
  1070  		{r: bytes.NewReader([]byte("hello"))},
  1071  		{r: bytes.NewBuffer([]byte("hello"))},
  1072  	}
  1073  	for i, tt := range tests {
  1074  		req, err := NewRequest("POST", "http://foo.tld/", tt.r)
  1075  		if err != nil {
  1076  			t.Errorf("test[%d]: %v", i, err)
  1077  			continue
  1078  		}
  1079  		if req.Body == nil {
  1080  			t.Errorf("test[%d]: Body = nil", i)
  1081  			continue
  1082  		}
  1083  		if req.GetBody == nil {
  1084  			t.Errorf("test[%d]: GetBody = nil", i)
  1085  			continue
  1086  		}
  1087  		slurp1, err := io.ReadAll(req.Body)
  1088  		if err != nil {
  1089  			t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
  1090  		}
  1091  		newBody, err := req.GetBody()
  1092  		if err != nil {
  1093  			t.Errorf("test[%d]: GetBody = %v", i, err)
  1094  		}
  1095  		slurp2, err := io.ReadAll(newBody)
  1096  		if err != nil {
  1097  			t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
  1098  		}
  1099  		if string(slurp1) != string(slurp2) {
  1100  			t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
  1101  		}
  1102  	}
  1103  }
  1104  
  1105  func testMissingFile(t *testing.T, req *Request) {
  1106  	f, fh, err := req.FormFile("missing")
  1107  	if f != nil {
  1108  		t.Errorf("FormFile file = %v, want nil", f)
  1109  	}
  1110  	if fh != nil {
  1111  		t.Errorf("FormFile file header = %q, want nil", fh)
  1112  	}
  1113  	if err != ErrMissingFile {
  1114  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
  1115  	}
  1116  }
  1117  
  1118  func newTestMultipartRequest(t *testing.T) *Request {
  1119  	b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
  1120  	req, err := NewRequest("POST", "/", b)
  1121  	if err != nil {
  1122  		t.Fatal("NewRequest:", err)
  1123  	}
  1124  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
  1125  	req.Header.Set("Content-type", ctype)
  1126  	return req
  1127  }
  1128  
  1129  func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
  1130  	if g, e := req.FormValue("texta"), textaValue; g != e {
  1131  		t.Errorf("texta value = %q, want %q", g, e)
  1132  	}
  1133  	if g, e := req.FormValue("textb"), textbValue; g != e {
  1134  		t.Errorf("textb value = %q, want %q", g, e)
  1135  	}
  1136  	if g := req.FormValue("missing"); g != "" {
  1137  		t.Errorf("missing value = %q, want empty string", g)
  1138  	}
  1139  
  1140  	assertMem := func(n string, fd multipart.File) {
  1141  		if _, ok := fd.(*os.File); ok {
  1142  			t.Error(n, " is *os.File, should not be")
  1143  		}
  1144  	}
  1145  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
  1146  	defer fda.Close()
  1147  	assertMem("filea", fda)
  1148  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
  1149  	defer fdb.Close()
  1150  	if allMem {
  1151  		assertMem("fileb", fdb)
  1152  	} else {
  1153  		if _, ok := fdb.(*os.File); !ok {
  1154  			t.Errorf("fileb has unexpected underlying type %T", fdb)
  1155  		}
  1156  	}
  1157  
  1158  	testMissingFile(t, req)
  1159  }
  1160  
  1161  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
  1162  	f, fh, err := req.FormFile(key)
  1163  	if err != nil {
  1164  		t.Fatalf("FormFile(%q): %q", key, err)
  1165  	}
  1166  	if fh.Filename != expectFilename {
  1167  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
  1168  	}
  1169  	var b bytes.Buffer
  1170  	_, err = io.Copy(&b, f)
  1171  	if err != nil {
  1172  		t.Fatal("copying contents:", err)
  1173  	}
  1174  	if g := b.String(); g != expectContent {
  1175  		t.Errorf("contents = %q, want %q", g, expectContent)
  1176  	}
  1177  	return f
  1178  }
  1179  
  1180  const (
  1181  	fileaContents = "This is a test file."
  1182  	filebContents = "Another test file."
  1183  	textaValue    = "foo"
  1184  	textbValue    = "bar"
  1185  	boundary      = `MyBoundary`
  1186  )
  1187  
  1188  const message = `
  1189  --MyBoundary
  1190  Content-Disposition: form-data; name="filea"; filename="filea.txt"
  1191  Content-Type: text/plain
  1192  
  1193  ` + fileaContents + `
  1194  --MyBoundary
  1195  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
  1196  Content-Type: text/plain
  1197  
  1198  ` + filebContents + `
  1199  --MyBoundary
  1200  Content-Disposition: form-data; name="texta"
  1201  
  1202  ` + textaValue + `
  1203  --MyBoundary
  1204  Content-Disposition: form-data; name="textb"
  1205  
  1206  ` + textbValue + `
  1207  --MyBoundary--
  1208  `
  1209  
  1210  func benchmarkReadRequest(b *testing.B, request string) {
  1211  	request = request + "\n"                            // final \n
  1212  	request = strings.ReplaceAll(request, "\n", "\r\n") // expand \n to \r\n
  1213  	b.SetBytes(int64(len(request)))
  1214  	r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
  1215  	b.ReportAllocs()
  1216  	b.ResetTimer()
  1217  	for i := 0; i < b.N; i++ {
  1218  		_, err := ReadRequest(r)
  1219  		if err != nil {
  1220  			b.Fatalf("failed to read request: %v", err)
  1221  		}
  1222  	}
  1223  }
  1224  
  1225  // infiniteReader satisfies Read requests as if the contents of buf
  1226  // loop indefinitely.
  1227  type infiniteReader struct {
  1228  	buf    []byte
  1229  	offset int
  1230  }
  1231  
  1232  func (r *infiniteReader) Read(b []byte) (int, error) {
  1233  	n := copy(b, r.buf[r.offset:])
  1234  	r.offset = (r.offset + n) % len(r.buf)
  1235  	return n, nil
  1236  }
  1237  
  1238  func BenchmarkReadRequestChrome(b *testing.B) {
  1239  	// https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
  1240  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1241  Host: localhost:8080
  1242  Connection: keep-alive
  1243  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  1244  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
  1245  Accept-Encoding: gzip,deflate,sdch
  1246  Accept-Language: en-US,en;q=0.8
  1247  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  1248  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
  1249  `)
  1250  }
  1251  
  1252  func BenchmarkReadRequestCurl(b *testing.B) {
  1253  	// curl http://localhost:8080/
  1254  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1255  User-Agent: curl/7.27.0
  1256  Host: localhost:8080
  1257  Accept: */*
  1258  `)
  1259  }
  1260  
  1261  func BenchmarkReadRequestApachebench(b *testing.B) {
  1262  	// ab -n 1 -c 1 http://localhost:8080/
  1263  	benchmarkReadRequest(b, `GET / HTTP/1.0
  1264  Host: localhost:8080
  1265  User-Agent: ApacheBench/2.3
  1266  Accept: */*
  1267  `)
  1268  }
  1269  
  1270  func BenchmarkReadRequestSiege(b *testing.B) {
  1271  	// siege -r 1 -c 1 http://localhost:8080/
  1272  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1273  Host: localhost:8080
  1274  Accept: */*
  1275  Accept-Encoding: gzip
  1276  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
  1277  Connection: keep-alive
  1278  `)
  1279  }
  1280  
  1281  func BenchmarkReadRequestWrk(b *testing.B) {
  1282  	// wrk -t 1 -r 1 -c 1 http://localhost:8080/
  1283  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1284  Host: localhost:8080
  1285  `)
  1286  }
  1287  
  1288  const (
  1289  	withTLS = true
  1290  	noTLS   = false
  1291  )
  1292  
  1293  func BenchmarkFileAndServer_1KB(b *testing.B) {
  1294  	benchmarkFileAndServer(b, 1<<10)
  1295  }
  1296  
  1297  func BenchmarkFileAndServer_16MB(b *testing.B) {
  1298  	benchmarkFileAndServer(b, 1<<24)
  1299  }
  1300  
  1301  func BenchmarkFileAndServer_64MB(b *testing.B) {
  1302  	benchmarkFileAndServer(b, 1<<26)
  1303  }
  1304  
  1305  func benchmarkFileAndServer(b *testing.B, n int64) {
  1306  	f, err := os.CreateTemp(os.TempDir(), "go-bench-http-file-and-server")
  1307  	if err != nil {
  1308  		b.Fatalf("Failed to create temp file: %v", err)
  1309  	}
  1310  
  1311  	defer func() {
  1312  		f.Close()
  1313  		os.RemoveAll(f.Name())
  1314  	}()
  1315  
  1316  	if _, err := io.CopyN(f, rand.Reader, n); err != nil {
  1317  		b.Fatalf("Failed to copy %d bytes: %v", n, err)
  1318  	}
  1319  
  1320  	b.Run("NoTLS", func(b *testing.B) {
  1321  		runFileAndServerBenchmarks(b, noTLS, f, n)
  1322  	})
  1323  
  1324  	b.Run("TLS", func(b *testing.B) {
  1325  		runFileAndServerBenchmarks(b, withTLS, f, n)
  1326  	})
  1327  }
  1328  
  1329  func runFileAndServerBenchmarks(b *testing.B, tlsOption bool, f *os.File, n int64) {
  1330  	handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
  1331  		defer req.Body.Close()
  1332  		nc, err := io.Copy(io.Discard, req.Body)
  1333  		if err != nil {
  1334  			panic(err)
  1335  		}
  1336  
  1337  		if nc != n {
  1338  			panic(fmt.Errorf("Copied %d Wanted %d bytes", nc, n))
  1339  		}
  1340  	})
  1341  
  1342  	var cst *httptest.Server
  1343  	if tlsOption == withTLS {
  1344  		cst = httptest.NewTLSServer(handler)
  1345  	} else {
  1346  		cst = httptest.NewServer(handler)
  1347  	}
  1348  
  1349  	defer cst.Close()
  1350  	b.ResetTimer()
  1351  	for i := 0; i < b.N; i++ {
  1352  		// Perform some setup.
  1353  		b.StopTimer()
  1354  		if _, err := f.Seek(0, 0); err != nil {
  1355  			b.Fatalf("Failed to seek back to file: %v", err)
  1356  		}
  1357  
  1358  		b.StartTimer()
  1359  		req, err := NewRequest("PUT", cst.URL, io.NopCloser(f))
  1360  		if err != nil {
  1361  			b.Fatal(err)
  1362  		}
  1363  
  1364  		req.ContentLength = n
  1365  		// Prevent mime sniffing by setting the Content-Type.
  1366  		req.Header.Set("Content-Type", "application/octet-stream")
  1367  		res, err := cst.Client().Do(req)
  1368  		if err != nil {
  1369  			b.Fatalf("Failed to make request to backend: %v", err)
  1370  		}
  1371  
  1372  		res.Body.Close()
  1373  		b.SetBytes(n)
  1374  	}
  1375  }
  1376  

View as plain text