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  	"io/ioutil"
    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  func TestParseFormQuery(t *testing.T) {
    36  	req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&orphan=nope&empty=not",
    37  		strings.NewReader("z=post&both=y&prio=2&=nokey&orphan;empty=&"))
    38  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
    39  
    40  	if q := req.FormValue("q"); q != "foo" {
    41  		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
    42  	}
    43  	if z := req.FormValue("z"); z != "post" {
    44  		t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
    45  	}
    46  	if bq, found := req.PostForm["q"]; found {
    47  		t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
    48  	}
    49  	if bz := req.PostFormValue("z"); bz != "post" {
    50  		t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
    51  	}
    52  	if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
    53  		t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
    54  	}
    55  	if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
    56  		t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
    57  	}
    58  	if prio := req.FormValue("prio"); prio != "2" {
    59  		t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
    60  	}
    61  	if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
    62  		t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
    63  	}
    64  	if empty := req.Form["empty"]; !reflect.DeepEqual(empty, []string{"", "not"}) {
    65  		t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
    66  	}
    67  	if nokey := req.Form[""]; !reflect.DeepEqual(nokey, []string{"nokey"}) {
    68  		t.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey)
    69  	}
    70  }
    71  
    72  // Tests that we only parse the form automatically for certain methods.
    73  func TestParseFormQueryMethods(t *testing.T) {
    74  	for _, method := range []string{"POST", "PATCH", "PUT", "FOO"} {
    75  		req, _ := NewRequest(method, "http://www.google.com/search",
    76  			strings.NewReader("foo=bar"))
    77  		req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
    78  		want := "bar"
    79  		if method == "FOO" {
    80  			want = ""
    81  		}
    82  		if got := req.FormValue("foo"); got != want {
    83  			t.Errorf(`for method %s, FormValue("foo") = %q; want %q`, method, got, want)
    84  		}
    85  	}
    86  }
    87  
    88  func TestParseFormUnknownContentType(t *testing.T) {
    89  	for _, test := range []struct {
    90  		name        string
    91  		wantErr     string
    92  		contentType Header
    93  	}{
    94  		{"text", "", Header{"Content-Type": {"text/plain"}}},
    95  		// Empty content type is legal - may be treated as
    96  		// application/octet-stream (RFC 7231, section 3.1.1.5)
    97  		{"empty", "", Header{}},
    98  		{"boundary", "mime: invalid media parameter", Header{"Content-Type": {"text/plain; boundary="}}},
    99  		{"unknown", "", Header{"Content-Type": {"application/unknown"}}},
   100  	} {
   101  		t.Run(test.name,
   102  			func(t *testing.T) {
   103  				req := &Request{
   104  					Method: "POST",
   105  					Header: test.contentType,
   106  					Body:   ioutil.NopCloser(strings.NewReader("body")),
   107  				}
   108  				err := req.ParseForm()
   109  				switch {
   110  				case err == nil && test.wantErr != "":
   111  					t.Errorf("unexpected success; want error %q", test.wantErr)
   112  				case err != nil && test.wantErr == "":
   113  					t.Errorf("want success, got error: %v", err)
   114  				case test.wantErr != "" && test.wantErr != fmt.Sprint(err):
   115  					t.Errorf("got error %q; want %q", err, test.wantErr)
   116  				}
   117  			},
   118  		)
   119  	}
   120  }
   121  
   122  func TestParseFormInitializeOnError(t *testing.T) {
   123  	nilBody, _ := NewRequest("POST", "http://www.google.com/search?q=foo", nil)
   124  	tests := []*Request{
   125  		nilBody,
   126  		{Method: "GET", URL: nil},
   127  	}
   128  	for i, req := range tests {
   129  		err := req.ParseForm()
   130  		if req.Form == nil {
   131  			t.Errorf("%d. Form not initialized, error %v", i, err)
   132  		}
   133  		if req.PostForm == nil {
   134  			t.Errorf("%d. PostForm not initialized, error %v", i, err)
   135  		}
   136  	}
   137  }
   138  
   139  func TestMultipartReader(t *testing.T) {
   140  	tests := []struct {
   141  		shouldError bool
   142  		contentType string
   143  	}{
   144  		{false, `multipart/form-data; boundary="foo123"`},
   145  		{false, `multipart/mixed; boundary="foo123"`},
   146  		{true, `text/plain`},
   147  	}
   148  
   149  	for i, test := range tests {
   150  		req := &Request{
   151  			Method: "POST",
   152  			Header: Header{"Content-Type": {test.contentType}},
   153  			Body:   ioutil.NopCloser(new(bytes.Buffer)),
   154  		}
   155  		multipart, err := req.MultipartReader()
   156  		if test.shouldError {
   157  			if err == nil || multipart != nil {
   158  				t.Errorf("test %d: unexpectedly got nil-error (%v) or non-nil-multipart (%v)", i, err, multipart)
   159  			}
   160  			continue
   161  		}
   162  		if err != nil || multipart == nil {
   163  			t.Errorf("test %d: unexpectedly got error (%v) or nil-multipart (%v)", i, err, multipart)
   164  		}
   165  	}
   166  }
   167  
   168  // Issue 9305: ParseMultipartForm should populate PostForm too
   169  func TestParseMultipartFormPopulatesPostForm(t *testing.T) {
   170  	postData :=
   171  		`--xxx
   172  Content-Disposition: form-data; name="field1"
   173  
   174  value1
   175  --xxx
   176  Content-Disposition: form-data; name="field2"
   177  
   178  value2
   179  --xxx
   180  Content-Disposition: form-data; name="file"; filename="file"
   181  Content-Type: application/octet-stream
   182  Content-Transfer-Encoding: binary
   183  
   184  binary data
   185  --xxx--
   186  `
   187  	req := &Request{
   188  		Method: "POST",
   189  		Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
   190  		Body:   ioutil.NopCloser(strings.NewReader(postData)),
   191  	}
   192  
   193  	initialFormItems := map[string]string{
   194  		"language": "Go",
   195  		"name":     "gopher",
   196  		"skill":    "go-ing",
   197  		"field2":   "initial-value2",
   198  	}
   199  
   200  	req.Form = make(url.Values)
   201  	for k, v := range initialFormItems {
   202  		req.Form.Add(k, v)
   203  	}
   204  
   205  	err := req.ParseMultipartForm(10000)
   206  	if err != nil {
   207  		t.Fatalf("unexpected multipart error %v", err)
   208  	}
   209  
   210  	wantForm := url.Values{
   211  		"language": []string{"Go"},
   212  		"name":     []string{"gopher"},
   213  		"skill":    []string{"go-ing"},
   214  		"field1":   []string{"value1"},
   215  		"field2":   []string{"initial-value2", "value2"},
   216  	}
   217  	if !reflect.DeepEqual(req.Form, wantForm) {
   218  		t.Fatalf("req.Form = %v, want %v", req.Form, wantForm)
   219  	}
   220  
   221  	wantPostForm := url.Values{
   222  		"field1": []string{"value1"},
   223  		"field2": []string{"value2"},
   224  	}
   225  	if !reflect.DeepEqual(req.PostForm, wantPostForm) {
   226  		t.Fatalf("req.PostForm = %v, want %v", req.PostForm, wantPostForm)
   227  	}
   228  }
   229  
   230  func TestParseMultipartForm(t *testing.T) {
   231  	req := &Request{
   232  		Method: "POST",
   233  		Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
   234  		Body:   ioutil.NopCloser(new(bytes.Buffer)),
   235  	}
   236  	err := req.ParseMultipartForm(25)
   237  	if err == nil {
   238  		t.Error("expected multipart EOF, got nil")
   239  	}
   240  
   241  	req.Header = Header{"Content-Type": {"text/plain"}}
   242  	err = req.ParseMultipartForm(25)
   243  	if err != ErrNotMultipart {
   244  		t.Error("expected ErrNotMultipart for text/plain")
   245  	}
   246  }
   247  
   248  func TestRedirect_h1(t *testing.T) { testRedirect(t, h1Mode) }
   249  func TestRedirect_h2(t *testing.T) { testRedirect(t, h2Mode) }
   250  func testRedirect(t *testing.T, h2 bool) {
   251  	defer afterTest(t)
   252  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   253  		switch r.URL.Path {
   254  		case "/":
   255  			w.Header().Set("Location", "/foo/")
   256  			w.WriteHeader(StatusSeeOther)
   257  		case "/foo/":
   258  			fmt.Fprintf(w, "foo")
   259  		default:
   260  			w.WriteHeader(StatusBadRequest)
   261  		}
   262  	}))
   263  	defer cst.close()
   264  
   265  	var end = regexp.MustCompile("/foo/$")
   266  	r, err := cst.c.Get(cst.ts.URL)
   267  	if err != nil {
   268  		t.Fatal(err)
   269  	}
   270  	r.Body.Close()
   271  	url := r.Request.URL.String()
   272  	if r.StatusCode != 200 || !end.MatchString(url) {
   273  		t.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r.StatusCode, url)
   274  	}
   275  }
   276  
   277  func TestSetBasicAuth(t *testing.T) {
   278  	r, _ := NewRequest("GET", "http://example.com/", nil)
   279  	r.SetBasicAuth("Aladdin", "open sesame")
   280  	if g, e := r.Header.Get("Authorization"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; g != e {
   281  		t.Errorf("got header %q, want %q", g, e)
   282  	}
   283  }
   284  
   285  func TestMultipartRequest(t *testing.T) {
   286  	// Test that we can read the values and files of a
   287  	// multipart request with FormValue and FormFile,
   288  	// and that ParseMultipartForm can be called multiple times.
   289  	req := newTestMultipartRequest(t)
   290  	if err := req.ParseMultipartForm(25); err != nil {
   291  		t.Fatal("ParseMultipartForm first call:", err)
   292  	}
   293  	defer req.MultipartForm.RemoveAll()
   294  	validateTestMultipartContents(t, req, false)
   295  	if err := req.ParseMultipartForm(25); err != nil {
   296  		t.Fatal("ParseMultipartForm second call:", err)
   297  	}
   298  	validateTestMultipartContents(t, req, false)
   299  }
   300  
   301  func TestMultipartRequestAuto(t *testing.T) {
   302  	// Test that FormValue and FormFile automatically invoke
   303  	// ParseMultipartForm and return the right values.
   304  	req := newTestMultipartRequest(t)
   305  	defer func() {
   306  		if req.MultipartForm != nil {
   307  			req.MultipartForm.RemoveAll()
   308  		}
   309  	}()
   310  	validateTestMultipartContents(t, req, true)
   311  }
   312  
   313  func TestMissingFileMultipartRequest(t *testing.T) {
   314  	// Test that FormFile returns an error if
   315  	// the named file is missing.
   316  	req := newTestMultipartRequest(t)
   317  	testMissingFile(t, req)
   318  }
   319  
   320  // Test that FormValue invokes ParseMultipartForm.
   321  func TestFormValueCallsParseMultipartForm(t *testing.T) {
   322  	req, _ := NewRequest("POST", "http://www.google.com/", strings.NewReader("z=post"))
   323  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
   324  	if req.Form != nil {
   325  		t.Fatal("Unexpected request Form, want nil")
   326  	}
   327  	req.FormValue("z")
   328  	if req.Form == nil {
   329  		t.Fatal("ParseMultipartForm not called by FormValue")
   330  	}
   331  }
   332  
   333  // Test that FormFile invokes ParseMultipartForm.
   334  func TestFormFileCallsParseMultipartForm(t *testing.T) {
   335  	req := newTestMultipartRequest(t)
   336  	if req.Form != nil {
   337  		t.Fatal("Unexpected request Form, want nil")
   338  	}
   339  	req.FormFile("")
   340  	if req.Form == nil {
   341  		t.Fatal("ParseMultipartForm not called by FormFile")
   342  	}
   343  }
   344  
   345  // Test that ParseMultipartForm errors if called
   346  // after MultipartReader on the same request.
   347  func TestParseMultipartFormOrder(t *testing.T) {
   348  	req := newTestMultipartRequest(t)
   349  	if _, err := req.MultipartReader(); err != nil {
   350  		t.Fatalf("MultipartReader: %v", err)
   351  	}
   352  	if err := req.ParseMultipartForm(1024); err == nil {
   353  		t.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
   354  	}
   355  }
   356  
   357  // Test that MultipartReader errors if called
   358  // after ParseMultipartForm on the same request.
   359  func TestMultipartReaderOrder(t *testing.T) {
   360  	req := newTestMultipartRequest(t)
   361  	if err := req.ParseMultipartForm(25); err != nil {
   362  		t.Fatalf("ParseMultipartForm: %v", err)
   363  	}
   364  	defer req.MultipartForm.RemoveAll()
   365  	if _, err := req.MultipartReader(); err == nil {
   366  		t.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
   367  	}
   368  }
   369  
   370  // Test that FormFile errors if called after
   371  // MultipartReader on the same request.
   372  func TestFormFileOrder(t *testing.T) {
   373  	req := newTestMultipartRequest(t)
   374  	if _, err := req.MultipartReader(); err != nil {
   375  		t.Fatalf("MultipartReader: %v", err)
   376  	}
   377  	if _, _, err := req.FormFile(""); err == nil {
   378  		t.Fatal("expected an error from FormFile after call to MultipartReader")
   379  	}
   380  }
   381  
   382  var readRequestErrorTests = []struct {
   383  	in  string
   384  	err string
   385  
   386  	header Header
   387  }{
   388  	0: {"GET / HTTP/1.1\r\nheader:foo\r\n\r\n", "", Header{"Header": {"foo"}}},
   389  	1: {"GET / HTTP/1.1\r\nheader:foo\r\n", io.ErrUnexpectedEOF.Error(), nil},
   390  	2: {"", io.EOF.Error(), nil},
   391  	3: {
   392  		in:  "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
   393  		err: "http: method cannot contain a Content-Length",
   394  	},
   395  	4: {
   396  		in:     "HEAD / HTTP/1.1\r\n\r\n",
   397  		header: Header{},
   398  	},
   399  
   400  	// Multiple Content-Length values should either be
   401  	// deduplicated if same or reject otherwise
   402  	// See Issue 16490.
   403  	5: {
   404  		in:  "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\nGopher hey\r\n",
   405  		err: "cannot contain multiple Content-Length headers",
   406  	},
   407  	6: {
   408  		in:  "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\nGopher\r\n",
   409  		err: "cannot contain multiple Content-Length headers",
   410  	},
   411  	7: {
   412  		in:     "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
   413  		err:    "",
   414  		header: Header{"Content-Length": {"6"}},
   415  	},
   416  	8: {
   417  		in:  "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
   418  		err: "cannot contain multiple Content-Length headers",
   419  	},
   420  	9: {
   421  		in:  "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
   422  		err: "cannot contain multiple Content-Length headers",
   423  	},
   424  	10: {
   425  		in:     "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
   426  		header: Header{"Content-Length": {"0"}},
   427  	},
   428  }
   429  
   430  func TestReadRequestErrors(t *testing.T) {
   431  	for i, tt := range readRequestErrorTests {
   432  		req, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.in)))
   433  		if err == nil {
   434  			if tt.err != "" {
   435  				t.Errorf("#%d: got nil err; want %q", i, tt.err)
   436  			}
   437  
   438  			if !reflect.DeepEqual(tt.header, req.Header) {
   439  				t.Errorf("#%d: gotHeader: %q wantHeader: %q", i, req.Header, tt.header)
   440  			}
   441  			continue
   442  		}
   443  
   444  		if tt.err == "" || !strings.Contains(err.Error(), tt.err) {
   445  			t.Errorf("%d: got error = %v; want %v", i, err, tt.err)
   446  		}
   447  	}
   448  }
   449  
   450  var newRequestHostTests = []struct {
   451  	in, out string
   452  }{
   453  	{"http://www.example.com/", "www.example.com"},
   454  	{"http://www.example.com:8080/", "www.example.com:8080"},
   455  
   456  	{"http://192.168.0.1/", "192.168.0.1"},
   457  	{"http://192.168.0.1:8080/", "192.168.0.1:8080"},
   458  	{"http://192.168.0.1:/", "192.168.0.1"},
   459  
   460  	{"http://[fe80::1]/", "[fe80::1]"},
   461  	{"http://[fe80::1]:8080/", "[fe80::1]:8080"},
   462  	{"http://[fe80::1%25en0]/", "[fe80::1%en0]"},
   463  	{"http://[fe80::1%25en0]:8080/", "[fe80::1%en0]:8080"},
   464  	{"http://[fe80::1%25en0]:/", "[fe80::1%en0]"},
   465  }
   466  
   467  func TestNewRequestHost(t *testing.T) {
   468  	for i, tt := range newRequestHostTests {
   469  		req, err := NewRequest("GET", tt.in, nil)
   470  		if err != nil {
   471  			t.Errorf("#%v: %v", i, err)
   472  			continue
   473  		}
   474  		if req.Host != tt.out {
   475  			t.Errorf("got %q; want %q", req.Host, tt.out)
   476  		}
   477  	}
   478  }
   479  
   480  func TestRequestInvalidMethod(t *testing.T) {
   481  	_, err := NewRequest("bad method", "http://foo.com/", nil)
   482  	if err == nil {
   483  		t.Error("expected error from NewRequest with invalid method")
   484  	}
   485  	req, err := NewRequest("GET", "http://foo.example/", nil)
   486  	if err != nil {
   487  		t.Fatal(err)
   488  	}
   489  	req.Method = "bad method"
   490  	_, err = DefaultClient.Do(req)
   491  	if err == nil || !strings.Contains(err.Error(), "invalid method") {
   492  		t.Errorf("Transport error = %v; want invalid method", err)
   493  	}
   494  
   495  	req, err = NewRequest("", "http://foo.com/", nil)
   496  	if err != nil {
   497  		t.Errorf("NewRequest(empty method) = %v; want nil", err)
   498  	} else if req.Method != "GET" {
   499  		t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method)
   500  	}
   501  }
   502  
   503  func TestNewRequestContentLength(t *testing.T) {
   504  	readByte := func(r io.Reader) io.Reader {
   505  		var b [1]byte
   506  		r.Read(b[:])
   507  		return r
   508  	}
   509  	tests := []struct {
   510  		r    io.Reader
   511  		want int64
   512  	}{
   513  		{bytes.NewReader([]byte("123")), 3},
   514  		{bytes.NewBuffer([]byte("1234")), 4},
   515  		{strings.NewReader("12345"), 5},
   516  		{strings.NewReader(""), 0},
   517  		{NoBody, 0},
   518  
   519  		// Not detected. During Go 1.8 we tried to make these set to -1, but
   520  		// due to Issue 18117, we keep these returning 0, even though they're
   521  		// unknown.
   522  		{struct{ io.Reader }{strings.NewReader("xyz")}, 0},
   523  		{io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
   524  		{readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
   525  	}
   526  	for i, tt := range tests {
   527  		req, err := NewRequest("POST", "http://localhost/", tt.r)
   528  		if err != nil {
   529  			t.Fatal(err)
   530  		}
   531  		if req.ContentLength != tt.want {
   532  			t.Errorf("test[%d]: ContentLength(%T) = %d; want %d", i, tt.r, req.ContentLength, tt.want)
   533  		}
   534  	}
   535  }
   536  
   537  var parseHTTPVersionTests = []struct {
   538  	vers         string
   539  	major, minor int
   540  	ok           bool
   541  }{
   542  	{"HTTP/0.9", 0, 9, true},
   543  	{"HTTP/1.0", 1, 0, true},
   544  	{"HTTP/1.1", 1, 1, true},
   545  	{"HTTP/3.14", 3, 14, true},
   546  
   547  	{"HTTP", 0, 0, false},
   548  	{"HTTP/one.one", 0, 0, false},
   549  	{"HTTP/1.1/", 0, 0, false},
   550  	{"HTTP/-1,0", 0, 0, false},
   551  	{"HTTP/0,-1", 0, 0, false},
   552  	{"HTTP/", 0, 0, false},
   553  	{"HTTP/1,1", 0, 0, false},
   554  }
   555  
   556  func TestParseHTTPVersion(t *testing.T) {
   557  	for _, tt := range parseHTTPVersionTests {
   558  		major, minor, ok := ParseHTTPVersion(tt.vers)
   559  		if ok != tt.ok || major != tt.major || minor != tt.minor {
   560  			type version struct {
   561  				major, minor int
   562  				ok           bool
   563  			}
   564  			t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
   565  		}
   566  	}
   567  }
   568  
   569  type getBasicAuthTest struct {
   570  	username, password string
   571  	ok                 bool
   572  }
   573  
   574  type basicAuthCredentialsTest struct {
   575  	username, password string
   576  }
   577  
   578  var getBasicAuthTests = []struct {
   579  	username, password string
   580  	ok                 bool
   581  }{
   582  	{"Aladdin", "open sesame", true},
   583  	{"Aladdin", "open:sesame", true},
   584  	{"", "", true},
   585  }
   586  
   587  func TestGetBasicAuth(t *testing.T) {
   588  	for _, tt := range getBasicAuthTests {
   589  		r, _ := NewRequest("GET", "http://example.com/", nil)
   590  		r.SetBasicAuth(tt.username, tt.password)
   591  		username, password, ok := r.BasicAuth()
   592  		if ok != tt.ok || username != tt.username || password != tt.password {
   593  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   594  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   595  		}
   596  	}
   597  	// Unauthenticated request.
   598  	r, _ := NewRequest("GET", "http://example.com/", nil)
   599  	username, password, ok := r.BasicAuth()
   600  	if ok {
   601  		t.Errorf("expected false from BasicAuth when the request is unauthenticated")
   602  	}
   603  	want := basicAuthCredentialsTest{"", ""}
   604  	if username != want.username || password != want.password {
   605  		t.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
   606  			want, basicAuthCredentialsTest{username, password})
   607  	}
   608  }
   609  
   610  var parseBasicAuthTests = []struct {
   611  	header, username, password string
   612  	ok                         bool
   613  }{
   614  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   615  
   616  	// Case doesn't matter:
   617  	{"BASIC " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   618  	{"basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   619  
   620  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
   621  	{"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
   622  	{"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   623  	{base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   624  	{"Basic ", "", "", false},
   625  	{"Basic Aladdin:open sesame", "", "", false},
   626  	{`Digest username="Aladdin"`, "", "", false},
   627  }
   628  
   629  func TestParseBasicAuth(t *testing.T) {
   630  	for _, tt := range parseBasicAuthTests {
   631  		r, _ := NewRequest("GET", "http://example.com/", nil)
   632  		r.Header.Set("Authorization", tt.header)
   633  		username, password, ok := r.BasicAuth()
   634  		if ok != tt.ok || username != tt.username || password != tt.password {
   635  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   636  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   637  		}
   638  	}
   639  }
   640  
   641  type logWrites struct {
   642  	t   *testing.T
   643  	dst *[]string
   644  }
   645  
   646  func (l logWrites) WriteByte(c byte) error {
   647  	l.t.Fatalf("unexpected WriteByte call")
   648  	return nil
   649  }
   650  
   651  func (l logWrites) Write(p []byte) (n int, err error) {
   652  	*l.dst = append(*l.dst, string(p))
   653  	return len(p), nil
   654  }
   655  
   656  func TestRequestWriteBufferedWriter(t *testing.T) {
   657  	got := []string{}
   658  	req, _ := NewRequest("GET", "http://foo.com/", nil)
   659  	req.Write(logWrites{t, &got})
   660  	want := []string{
   661  		"GET / HTTP/1.1\r\n",
   662  		"Host: foo.com\r\n",
   663  		"User-Agent: " + DefaultUserAgent + "\r\n",
   664  		"\r\n",
   665  	}
   666  	if !reflect.DeepEqual(got, want) {
   667  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   668  	}
   669  }
   670  
   671  func TestRequestBadHost(t *testing.T) {
   672  	got := []string{}
   673  	req, err := NewRequest("GET", "http://foo/after", nil)
   674  	if err != nil {
   675  		t.Fatal(err)
   676  	}
   677  	req.Host = "foo.com with spaces"
   678  	req.URL.Host = "foo.com with spaces"
   679  	req.Write(logWrites{t, &got})
   680  	want := []string{
   681  		"GET /after HTTP/1.1\r\n",
   682  		"Host: foo.com\r\n",
   683  		"User-Agent: " + DefaultUserAgent + "\r\n",
   684  		"\r\n",
   685  	}
   686  	if !reflect.DeepEqual(got, want) {
   687  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   688  	}
   689  }
   690  
   691  func TestStarRequest(t *testing.T) {
   692  	req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
   693  	if err != nil {
   694  		return
   695  	}
   696  	if req.ContentLength != 0 {
   697  		t.Errorf("ContentLength = %d; want 0", req.ContentLength)
   698  	}
   699  	if req.Body == nil {
   700  		t.Errorf("Body = nil; want non-nil")
   701  	}
   702  
   703  	// Request.Write has Client semantics for Body/ContentLength,
   704  	// where ContentLength 0 means unknown if Body is non-nil, and
   705  	// thus chunking will happen unless we change semantics and
   706  	// signal that we want to serialize it as exactly zero.  The
   707  	// only way to do that for outbound requests is with a nil
   708  	// Body:
   709  	clientReq := *req
   710  	clientReq.Body = nil
   711  
   712  	var out bytes.Buffer
   713  	if err := clientReq.Write(&out); err != nil {
   714  		t.Fatal(err)
   715  	}
   716  
   717  	if strings.Contains(out.String(), "chunked") {
   718  		t.Error("wrote chunked request; want no body")
   719  	}
   720  	back, err := ReadRequest(bufio.NewReader(bytes.NewReader(out.Bytes())))
   721  	if err != nil {
   722  		t.Fatal(err)
   723  	}
   724  	// Ignore the Headers (the User-Agent breaks the deep equal,
   725  	// but we don't care about it)
   726  	req.Header = nil
   727  	back.Header = nil
   728  	if !reflect.DeepEqual(req, back) {
   729  		t.Errorf("Original request doesn't match Request read back.")
   730  		t.Logf("Original: %#v", req)
   731  		t.Logf("Original.URL: %#v", req.URL)
   732  		t.Logf("Wrote: %s", out.Bytes())
   733  		t.Logf("Read back (doesn't match Original): %#v", back)
   734  	}
   735  }
   736  
   737  type responseWriterJustWriter struct {
   738  	io.Writer
   739  }
   740  
   741  func (responseWriterJustWriter) Header() Header  { panic("should not be called") }
   742  func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
   743  
   744  // delayedEOFReader never returns (n > 0, io.EOF), instead putting
   745  // off the io.EOF until a subsequent Read call.
   746  type delayedEOFReader struct {
   747  	r io.Reader
   748  }
   749  
   750  func (dr delayedEOFReader) Read(p []byte) (n int, err error) {
   751  	n, err = dr.r.Read(p)
   752  	if n > 0 && err == io.EOF {
   753  		err = nil
   754  	}
   755  	return
   756  }
   757  
   758  func TestIssue10884_MaxBytesEOF(t *testing.T) {
   759  	dst := ioutil.Discard
   760  	_, err := io.Copy(dst, MaxBytesReader(
   761  		responseWriterJustWriter{dst},
   762  		ioutil.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
   763  		5))
   764  	if err != nil {
   765  		t.Fatal(err)
   766  	}
   767  }
   768  
   769  // Issue 14981: MaxBytesReader's return error wasn't sticky. It
   770  // doesn't technically need to be, but people expected it to be.
   771  func TestMaxBytesReaderStickyError(t *testing.T) {
   772  	isSticky := func(r io.Reader) error {
   773  		var log bytes.Buffer
   774  		buf := make([]byte, 1000)
   775  		var firstErr error
   776  		for {
   777  			n, err := r.Read(buf)
   778  			fmt.Fprintf(&log, "Read(%d) = %d, %v\n", len(buf), n, err)
   779  			if err == nil {
   780  				continue
   781  			}
   782  			if firstErr == nil {
   783  				firstErr = err
   784  				continue
   785  			}
   786  			if !reflect.DeepEqual(err, firstErr) {
   787  				return fmt.Errorf("non-sticky error. got log:\n%s", log.Bytes())
   788  			}
   789  			t.Logf("Got log: %s", log.Bytes())
   790  			return nil
   791  		}
   792  	}
   793  	tests := [...]struct {
   794  		readable int
   795  		limit    int64
   796  	}{
   797  		0: {99, 100},
   798  		1: {100, 100},
   799  		2: {101, 100},
   800  	}
   801  	for i, tt := range tests {
   802  		rc := MaxBytesReader(nil, ioutil.NopCloser(bytes.NewReader(make([]byte, tt.readable))), tt.limit)
   803  		if err := isSticky(rc); err != nil {
   804  			t.Errorf("%d. error: %v", i, err)
   805  		}
   806  	}
   807  }
   808  
   809  func TestWithContextDeepCopiesURL(t *testing.T) {
   810  	req, err := NewRequest("POST", "https://golang.org/", nil)
   811  	if err != nil {
   812  		t.Fatal(err)
   813  	}
   814  
   815  	reqCopy := req.WithContext(context.Background())
   816  	reqCopy.URL.Scheme = "http"
   817  
   818  	firstURL, secondURL := req.URL.String(), reqCopy.URL.String()
   819  	if firstURL == secondURL {
   820  		t.Errorf("unexpected change to original request's URL")
   821  	}
   822  
   823  	// And also check we don't crash on nil (Issue 20601)
   824  	req.URL = nil
   825  	reqCopy = req.WithContext(context.Background())
   826  	if reqCopy.URL != nil {
   827  		t.Error("expected nil URL in cloned request")
   828  	}
   829  }
   830  
   831  func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
   832  	testNoPanicWithBasicAuth(t, h1Mode)
   833  }
   834  
   835  func TestNoPanicOnRoundTripWithBasicAuth_h2(t *testing.T) {
   836  	testNoPanicWithBasicAuth(t, h2Mode)
   837  }
   838  
   839  // Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
   840  func testNoPanicWithBasicAuth(t *testing.T, h2 bool) {
   841  	defer afterTest(t)
   842  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {}))
   843  	defer cst.close()
   844  
   845  	u, err := url.Parse(cst.ts.URL)
   846  	if err != nil {
   847  		t.Fatal(err)
   848  	}
   849  	u.User = url.UserPassword("foo", "bar")
   850  	req := &Request{
   851  		URL:    u,
   852  		Method: "GET",
   853  	}
   854  	if _, err := cst.c.Do(req); err != nil {
   855  		t.Fatalf("Unexpected error: %v", err)
   856  	}
   857  }
   858  
   859  // verify that NewRequest sets Request.GetBody and that it works
   860  func TestNewRequestGetBody(t *testing.T) {
   861  	tests := []struct {
   862  		r io.Reader
   863  	}{
   864  		{r: strings.NewReader("hello")},
   865  		{r: bytes.NewReader([]byte("hello"))},
   866  		{r: bytes.NewBuffer([]byte("hello"))},
   867  	}
   868  	for i, tt := range tests {
   869  		req, err := NewRequest("POST", "http://foo.tld/", tt.r)
   870  		if err != nil {
   871  			t.Errorf("test[%d]: %v", i, err)
   872  			continue
   873  		}
   874  		if req.Body == nil {
   875  			t.Errorf("test[%d]: Body = nil", i)
   876  			continue
   877  		}
   878  		if req.GetBody == nil {
   879  			t.Errorf("test[%d]: GetBody = nil", i)
   880  			continue
   881  		}
   882  		slurp1, err := ioutil.ReadAll(req.Body)
   883  		if err != nil {
   884  			t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
   885  		}
   886  		newBody, err := req.GetBody()
   887  		if err != nil {
   888  			t.Errorf("test[%d]: GetBody = %v", i, err)
   889  		}
   890  		slurp2, err := ioutil.ReadAll(newBody)
   891  		if err != nil {
   892  			t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
   893  		}
   894  		if string(slurp1) != string(slurp2) {
   895  			t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
   896  		}
   897  	}
   898  }
   899  
   900  func testMissingFile(t *testing.T, req *Request) {
   901  	f, fh, err := req.FormFile("missing")
   902  	if f != nil {
   903  		t.Errorf("FormFile file = %v, want nil", f)
   904  	}
   905  	if fh != nil {
   906  		t.Errorf("FormFile file header = %q, want nil", fh)
   907  	}
   908  	if err != ErrMissingFile {
   909  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
   910  	}
   911  }
   912  
   913  func newTestMultipartRequest(t *testing.T) *Request {
   914  	b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
   915  	req, err := NewRequest("POST", "/", b)
   916  	if err != nil {
   917  		t.Fatal("NewRequest:", err)
   918  	}
   919  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
   920  	req.Header.Set("Content-type", ctype)
   921  	return req
   922  }
   923  
   924  func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
   925  	if g, e := req.FormValue("texta"), textaValue; g != e {
   926  		t.Errorf("texta value = %q, want %q", g, e)
   927  	}
   928  	if g, e := req.FormValue("textb"), textbValue; g != e {
   929  		t.Errorf("textb value = %q, want %q", g, e)
   930  	}
   931  	if g := req.FormValue("missing"); g != "" {
   932  		t.Errorf("missing value = %q, want empty string", g)
   933  	}
   934  
   935  	assertMem := func(n string, fd multipart.File) {
   936  		if _, ok := fd.(*os.File); ok {
   937  			t.Error(n, " is *os.File, should not be")
   938  		}
   939  	}
   940  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
   941  	defer fda.Close()
   942  	assertMem("filea", fda)
   943  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
   944  	defer fdb.Close()
   945  	if allMem {
   946  		assertMem("fileb", fdb)
   947  	} else {
   948  		if _, ok := fdb.(*os.File); !ok {
   949  			t.Errorf("fileb has unexpected underlying type %T", fdb)
   950  		}
   951  	}
   952  
   953  	testMissingFile(t, req)
   954  }
   955  
   956  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
   957  	f, fh, err := req.FormFile(key)
   958  	if err != nil {
   959  		t.Fatalf("FormFile(%q): %q", key, err)
   960  	}
   961  	if fh.Filename != expectFilename {
   962  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
   963  	}
   964  	var b bytes.Buffer
   965  	_, err = io.Copy(&b, f)
   966  	if err != nil {
   967  		t.Fatal("copying contents:", err)
   968  	}
   969  	if g := b.String(); g != expectContent {
   970  		t.Errorf("contents = %q, want %q", g, expectContent)
   971  	}
   972  	return f
   973  }
   974  
   975  const (
   976  	fileaContents = "This is a test file."
   977  	filebContents = "Another test file."
   978  	textaValue    = "foo"
   979  	textbValue    = "bar"
   980  	boundary      = `MyBoundary`
   981  )
   982  
   983  const message = `
   984  --MyBoundary
   985  Content-Disposition: form-data; name="filea"; filename="filea.txt"
   986  Content-Type: text/plain
   987  
   988  ` + fileaContents + `
   989  --MyBoundary
   990  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
   991  Content-Type: text/plain
   992  
   993  ` + filebContents + `
   994  --MyBoundary
   995  Content-Disposition: form-data; name="texta"
   996  
   997  ` + textaValue + `
   998  --MyBoundary
   999  Content-Disposition: form-data; name="textb"
  1000  
  1001  ` + textbValue + `
  1002  --MyBoundary--
  1003  `
  1004  
  1005  func benchmarkReadRequest(b *testing.B, request string) {
  1006  	request = request + "\n"                            // final \n
  1007  	request = strings.ReplaceAll(request, "\n", "\r\n") // expand \n to \r\n
  1008  	b.SetBytes(int64(len(request)))
  1009  	r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
  1010  	b.ReportAllocs()
  1011  	b.ResetTimer()
  1012  	for i := 0; i < b.N; i++ {
  1013  		_, err := ReadRequest(r)
  1014  		if err != nil {
  1015  			b.Fatalf("failed to read request: %v", err)
  1016  		}
  1017  	}
  1018  }
  1019  
  1020  // infiniteReader satisfies Read requests as if the contents of buf
  1021  // loop indefinitely.
  1022  type infiniteReader struct {
  1023  	buf    []byte
  1024  	offset int
  1025  }
  1026  
  1027  func (r *infiniteReader) Read(b []byte) (int, error) {
  1028  	n := copy(b, r.buf[r.offset:])
  1029  	r.offset = (r.offset + n) % len(r.buf)
  1030  	return n, nil
  1031  }
  1032  
  1033  func BenchmarkReadRequestChrome(b *testing.B) {
  1034  	// https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
  1035  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1036  Host: localhost:8080
  1037  Connection: keep-alive
  1038  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  1039  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
  1040  Accept-Encoding: gzip,deflate,sdch
  1041  Accept-Language: en-US,en;q=0.8
  1042  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  1043  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
  1044  `)
  1045  }
  1046  
  1047  func BenchmarkReadRequestCurl(b *testing.B) {
  1048  	// curl http://localhost:8080/
  1049  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1050  User-Agent: curl/7.27.0
  1051  Host: localhost:8080
  1052  Accept: */*
  1053  `)
  1054  }
  1055  
  1056  func BenchmarkReadRequestApachebench(b *testing.B) {
  1057  	// ab -n 1 -c 1 http://localhost:8080/
  1058  	benchmarkReadRequest(b, `GET / HTTP/1.0
  1059  Host: localhost:8080
  1060  User-Agent: ApacheBench/2.3
  1061  Accept: */*
  1062  `)
  1063  }
  1064  
  1065  func BenchmarkReadRequestSiege(b *testing.B) {
  1066  	// siege -r 1 -c 1 http://localhost:8080/
  1067  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1068  Host: localhost:8080
  1069  Accept: */*
  1070  Accept-Encoding: gzip
  1071  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
  1072  Connection: keep-alive
  1073  `)
  1074  }
  1075  
  1076  func BenchmarkReadRequestWrk(b *testing.B) {
  1077  	// wrk -t 1 -r 1 -c 1 http://localhost:8080/
  1078  	benchmarkReadRequest(b, `GET / HTTP/1.1
  1079  Host: localhost:8080
  1080  `)
  1081  }
  1082  
  1083  const (
  1084  	withTLS = true
  1085  	noTLS   = false
  1086  )
  1087  
  1088  func BenchmarkFileAndServer_1KB(b *testing.B) {
  1089  	benchmarkFileAndServer(b, 1<<10)
  1090  }
  1091  
  1092  func BenchmarkFileAndServer_16MB(b *testing.B) {
  1093  	benchmarkFileAndServer(b, 1<<24)
  1094  }
  1095  
  1096  func BenchmarkFileAndServer_64MB(b *testing.B) {
  1097  	benchmarkFileAndServer(b, 1<<26)
  1098  }
  1099  
  1100  func benchmarkFileAndServer(b *testing.B, n int64) {
  1101  	f, err := ioutil.TempFile(os.TempDir(), "go-bench-http-file-and-server")
  1102  	if err != nil {
  1103  		b.Fatalf("Failed to create temp file: %v", err)
  1104  	}
  1105  
  1106  	defer func() {
  1107  		f.Close()
  1108  		os.RemoveAll(f.Name())
  1109  	}()
  1110  
  1111  	if _, err := io.CopyN(f, rand.Reader, n); err != nil {
  1112  		b.Fatalf("Failed to copy %d bytes: %v", n, err)
  1113  	}
  1114  
  1115  	b.Run("NoTLS", func(b *testing.B) {
  1116  		runFileAndServerBenchmarks(b, noTLS, f, n)
  1117  	})
  1118  
  1119  	b.Run("TLS", func(b *testing.B) {
  1120  		runFileAndServerBenchmarks(b, withTLS, f, n)
  1121  	})
  1122  }
  1123  
  1124  func runFileAndServerBenchmarks(b *testing.B, tlsOption bool, f *os.File, n int64) {
  1125  	handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
  1126  		defer req.Body.Close()
  1127  		nc, err := io.Copy(ioutil.Discard, req.Body)
  1128  		if err != nil {
  1129  			panic(err)
  1130  		}
  1131  
  1132  		if nc != n {
  1133  			panic(fmt.Errorf("Copied %d Wanted %d bytes", nc, n))
  1134  		}
  1135  	})
  1136  
  1137  	var cst *httptest.Server
  1138  	if tlsOption == withTLS {
  1139  		cst = httptest.NewTLSServer(handler)
  1140  	} else {
  1141  		cst = httptest.NewServer(handler)
  1142  	}
  1143  
  1144  	defer cst.Close()
  1145  	b.ResetTimer()
  1146  	for i := 0; i < b.N; i++ {
  1147  		// Perform some setup.
  1148  		b.StopTimer()
  1149  		if _, err := f.Seek(0, 0); err != nil {
  1150  			b.Fatalf("Failed to seek back to file: %v", err)
  1151  		}
  1152  
  1153  		b.StartTimer()
  1154  		req, err := NewRequest("PUT", cst.URL, ioutil.NopCloser(f))
  1155  		if err != nil {
  1156  			b.Fatal(err)
  1157  		}
  1158  
  1159  		req.ContentLength = n
  1160  		// Prevent mime sniffing by setting the Content-Type.
  1161  		req.Header.Set("Content-Type", "application/octet-stream")
  1162  		res, err := cst.Client().Do(req)
  1163  		if err != nil {
  1164  			b.Fatalf("Failed to make request to backend: %v", err)
  1165  		}
  1166  
  1167  		res.Body.Close()
  1168  		b.SetBytes(n)
  1169  	}
  1170  }
  1171  

View as plain text