github.com/remobjects/goldbaselibrary@v0.0.0-20230924164425-d458680a936b/Source/Gold/net/http/request_test.go (about)

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