github.com/euank/go@v0.0.0-20160829210321-495514729181/src/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  	"encoding/base64"
    11  	"fmt"
    12  	"io"
    13  	"io/ioutil"
    14  	"mime/multipart"
    15  	. "net/http"
    16  	"net/url"
    17  	"os"
    18  	"reflect"
    19  	"regexp"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func TestQuery(t *testing.T) {
    25  	req := &Request{Method: "GET"}
    26  	req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar")
    27  	if q := req.FormValue("q"); q != "foo" {
    28  		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
    29  	}
    30  }
    31  
    32  func TestPostQuery(t *testing.T) {
    33  	req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&empty=not",
    34  		strings.NewReader("z=post&both=y&prio=2&empty="))
    35  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
    36  
    37  	if q := req.FormValue("q"); q != "foo" {
    38  		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
    39  	}
    40  	if z := req.FormValue("z"); z != "post" {
    41  		t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
    42  	}
    43  	if bq, found := req.PostForm["q"]; found {
    44  		t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
    45  	}
    46  	if bz := req.PostFormValue("z"); bz != "post" {
    47  		t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
    48  	}
    49  	if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
    50  		t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
    51  	}
    52  	if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
    53  		t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
    54  	}
    55  	if prio := req.FormValue("prio"); prio != "2" {
    56  		t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
    57  	}
    58  	if empty := req.FormValue("empty"); empty != "" {
    59  		t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
    60  	}
    61  }
    62  
    63  func TestPatchQuery(t *testing.T) {
    64  	req, _ := NewRequest("PATCH", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&empty=not",
    65  		strings.NewReader("z=post&both=y&prio=2&empty="))
    66  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
    67  
    68  	if q := req.FormValue("q"); q != "foo" {
    69  		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
    70  	}
    71  	if z := req.FormValue("z"); z != "post" {
    72  		t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
    73  	}
    74  	if bq, found := req.PostForm["q"]; found {
    75  		t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
    76  	}
    77  	if bz := req.PostFormValue("z"); bz != "post" {
    78  		t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
    79  	}
    80  	if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
    81  		t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
    82  	}
    83  	if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
    84  		t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
    85  	}
    86  	if prio := req.FormValue("prio"); prio != "2" {
    87  		t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
    88  	}
    89  	if empty := req.FormValue("empty"); empty != "" {
    90  		t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
    91  	}
    92  }
    93  
    94  type stringMap map[string][]string
    95  type parseContentTypeTest struct {
    96  	shouldError bool
    97  	contentType stringMap
    98  }
    99  
   100  var parseContentTypeTests = []parseContentTypeTest{
   101  	{false, stringMap{"Content-Type": {"text/plain"}}},
   102  	// Empty content type is legal - should be treated as
   103  	// application/octet-stream (RFC 2616, section 7.2.1)
   104  	{false, stringMap{}},
   105  	{true, stringMap{"Content-Type": {"text/plain; boundary="}}},
   106  	{false, stringMap{"Content-Type": {"application/unknown"}}},
   107  }
   108  
   109  func TestParseFormUnknownContentType(t *testing.T) {
   110  	for i, test := range parseContentTypeTests {
   111  		req := &Request{
   112  			Method: "POST",
   113  			Header: Header(test.contentType),
   114  			Body:   ioutil.NopCloser(strings.NewReader("body")),
   115  		}
   116  		err := req.ParseForm()
   117  		switch {
   118  		case err == nil && test.shouldError:
   119  			t.Errorf("test %d should have returned error", i)
   120  		case err != nil && !test.shouldError:
   121  			t.Errorf("test %d should not have returned error, got %v", i, err)
   122  		}
   123  	}
   124  }
   125  
   126  func TestParseFormInitializeOnError(t *testing.T) {
   127  	nilBody, _ := NewRequest("POST", "http://www.google.com/search?q=foo", nil)
   128  	tests := []*Request{
   129  		nilBody,
   130  		{Method: "GET", URL: nil},
   131  	}
   132  	for i, req := range tests {
   133  		err := req.ParseForm()
   134  		if req.Form == nil {
   135  			t.Errorf("%d. Form not initialized, error %v", i, err)
   136  		}
   137  		if req.PostForm == nil {
   138  			t.Errorf("%d. PostForm not initialized, error %v", i, err)
   139  		}
   140  	}
   141  }
   142  
   143  func TestMultipartReader(t *testing.T) {
   144  	req := &Request{
   145  		Method: "POST",
   146  		Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
   147  		Body:   ioutil.NopCloser(new(bytes.Buffer)),
   148  	}
   149  	multipart, err := req.MultipartReader()
   150  	if multipart == nil {
   151  		t.Errorf("expected multipart; error: %v", err)
   152  	}
   153  
   154  	req.Header = Header{"Content-Type": {"text/plain"}}
   155  	multipart, err = req.MultipartReader()
   156  	if multipart != nil {
   157  		t.Error("unexpected multipart for text/plain")
   158  	}
   159  }
   160  
   161  // Issue 9305: ParseMultipartForm should populate PostForm too
   162  func TestParseMultipartFormPopulatesPostForm(t *testing.T) {
   163  	postData :=
   164  		`--xxx
   165  Content-Disposition: form-data; name="field1"
   166  
   167  value1
   168  --xxx
   169  Content-Disposition: form-data; name="field2"
   170  
   171  value2
   172  --xxx
   173  Content-Disposition: form-data; name="file"; filename="file"
   174  Content-Type: application/octet-stream
   175  Content-Transfer-Encoding: binary
   176  
   177  binary data
   178  --xxx--
   179  `
   180  	req := &Request{
   181  		Method: "POST",
   182  		Header: Header{"Content-Type": {`multipart/form-data; boundary=xxx`}},
   183  		Body:   ioutil.NopCloser(strings.NewReader(postData)),
   184  	}
   185  
   186  	initialFormItems := map[string]string{
   187  		"language": "Go",
   188  		"name":     "gopher",
   189  		"skill":    "go-ing",
   190  		"field2":   "initial-value2",
   191  	}
   192  
   193  	req.Form = make(url.Values)
   194  	for k, v := range initialFormItems {
   195  		req.Form.Add(k, v)
   196  	}
   197  
   198  	err := req.ParseMultipartForm(10000)
   199  	if err != nil {
   200  		t.Fatalf("unexpected multipart error %v", err)
   201  	}
   202  
   203  	wantForm := url.Values{
   204  		"language": []string{"Go"},
   205  		"name":     []string{"gopher"},
   206  		"skill":    []string{"go-ing"},
   207  		"field1":   []string{"value1"},
   208  		"field2":   []string{"initial-value2", "value2"},
   209  	}
   210  	if !reflect.DeepEqual(req.Form, wantForm) {
   211  		t.Fatalf("req.Form = %v, want %v", req.Form, wantForm)
   212  	}
   213  
   214  	wantPostForm := url.Values{
   215  		"field1": []string{"value1"},
   216  		"field2": []string{"value2"},
   217  	}
   218  	if !reflect.DeepEqual(req.PostForm, wantPostForm) {
   219  		t.Fatalf("req.PostForm = %v, want %v", req.PostForm, wantPostForm)
   220  	}
   221  }
   222  
   223  func TestParseMultipartForm(t *testing.T) {
   224  	req := &Request{
   225  		Method: "POST",
   226  		Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
   227  		Body:   ioutil.NopCloser(new(bytes.Buffer)),
   228  	}
   229  	err := req.ParseMultipartForm(25)
   230  	if err == nil {
   231  		t.Error("expected multipart EOF, got nil")
   232  	}
   233  
   234  	req.Header = Header{"Content-Type": {"text/plain"}}
   235  	err = req.ParseMultipartForm(25)
   236  	if err != ErrNotMultipart {
   237  		t.Error("expected ErrNotMultipart for text/plain")
   238  	}
   239  }
   240  
   241  func TestRedirect_h1(t *testing.T) { testRedirect(t, h1Mode) }
   242  func TestRedirect_h2(t *testing.T) { testRedirect(t, h2Mode) }
   243  func testRedirect(t *testing.T, h2 bool) {
   244  	defer afterTest(t)
   245  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   246  		switch r.URL.Path {
   247  		case "/":
   248  			w.Header().Set("Location", "/foo/")
   249  			w.WriteHeader(StatusSeeOther)
   250  		case "/foo/":
   251  			fmt.Fprintf(w, "foo")
   252  		default:
   253  			w.WriteHeader(StatusBadRequest)
   254  		}
   255  	}))
   256  	defer cst.close()
   257  
   258  	var end = regexp.MustCompile("/foo/$")
   259  	r, err := cst.c.Get(cst.ts.URL)
   260  	if err != nil {
   261  		t.Fatal(err)
   262  	}
   263  	r.Body.Close()
   264  	url := r.Request.URL.String()
   265  	if r.StatusCode != 200 || !end.MatchString(url) {
   266  		t.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r.StatusCode, url)
   267  	}
   268  }
   269  
   270  func TestSetBasicAuth(t *testing.T) {
   271  	r, _ := NewRequest("GET", "http://example.com/", nil)
   272  	r.SetBasicAuth("Aladdin", "open sesame")
   273  	if g, e := r.Header.Get("Authorization"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; g != e {
   274  		t.Errorf("got header %q, want %q", g, e)
   275  	}
   276  }
   277  
   278  func TestMultipartRequest(t *testing.T) {
   279  	// Test that we can read the values and files of a
   280  	// multipart request with FormValue and FormFile,
   281  	// and that ParseMultipartForm can be called multiple times.
   282  	req := newTestMultipartRequest(t)
   283  	if err := req.ParseMultipartForm(25); err != nil {
   284  		t.Fatal("ParseMultipartForm first call:", err)
   285  	}
   286  	defer req.MultipartForm.RemoveAll()
   287  	validateTestMultipartContents(t, req, false)
   288  	if err := req.ParseMultipartForm(25); err != nil {
   289  		t.Fatal("ParseMultipartForm second call:", err)
   290  	}
   291  	validateTestMultipartContents(t, req, false)
   292  }
   293  
   294  func TestMultipartRequestAuto(t *testing.T) {
   295  	// Test that FormValue and FormFile automatically invoke
   296  	// ParseMultipartForm and return the right values.
   297  	req := newTestMultipartRequest(t)
   298  	defer func() {
   299  		if req.MultipartForm != nil {
   300  			req.MultipartForm.RemoveAll()
   301  		}
   302  	}()
   303  	validateTestMultipartContents(t, req, true)
   304  }
   305  
   306  func TestMissingFileMultipartRequest(t *testing.T) {
   307  	// Test that FormFile returns an error if
   308  	// the named file is missing.
   309  	req := newTestMultipartRequest(t)
   310  	testMissingFile(t, req)
   311  }
   312  
   313  // Test that FormValue invokes ParseMultipartForm.
   314  func TestFormValueCallsParseMultipartForm(t *testing.T) {
   315  	req, _ := NewRequest("POST", "http://www.google.com/", strings.NewReader("z=post"))
   316  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
   317  	if req.Form != nil {
   318  		t.Fatal("Unexpected request Form, want nil")
   319  	}
   320  	req.FormValue("z")
   321  	if req.Form == nil {
   322  		t.Fatal("ParseMultipartForm not called by FormValue")
   323  	}
   324  }
   325  
   326  // Test that FormFile invokes ParseMultipartForm.
   327  func TestFormFileCallsParseMultipartForm(t *testing.T) {
   328  	req := newTestMultipartRequest(t)
   329  	if req.Form != nil {
   330  		t.Fatal("Unexpected request Form, want nil")
   331  	}
   332  	req.FormFile("")
   333  	if req.Form == nil {
   334  		t.Fatal("ParseMultipartForm not called by FormFile")
   335  	}
   336  }
   337  
   338  // Test that ParseMultipartForm errors if called
   339  // after MultipartReader on the same request.
   340  func TestParseMultipartFormOrder(t *testing.T) {
   341  	req := newTestMultipartRequest(t)
   342  	if _, err := req.MultipartReader(); err != nil {
   343  		t.Fatalf("MultipartReader: %v", err)
   344  	}
   345  	if err := req.ParseMultipartForm(1024); err == nil {
   346  		t.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
   347  	}
   348  }
   349  
   350  // Test that MultipartReader errors if called
   351  // after ParseMultipartForm on the same request.
   352  func TestMultipartReaderOrder(t *testing.T) {
   353  	req := newTestMultipartRequest(t)
   354  	if err := req.ParseMultipartForm(25); err != nil {
   355  		t.Fatalf("ParseMultipartForm: %v", err)
   356  	}
   357  	defer req.MultipartForm.RemoveAll()
   358  	if _, err := req.MultipartReader(); err == nil {
   359  		t.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
   360  	}
   361  }
   362  
   363  // Test that FormFile errors if called after
   364  // MultipartReader on the same request.
   365  func TestFormFileOrder(t *testing.T) {
   366  	req := newTestMultipartRequest(t)
   367  	if _, err := req.MultipartReader(); err != nil {
   368  		t.Fatalf("MultipartReader: %v", err)
   369  	}
   370  	if _, _, err := req.FormFile(""); err == nil {
   371  		t.Fatal("expected an error from FormFile after call to MultipartReader")
   372  	}
   373  }
   374  
   375  var readRequestErrorTests = []struct {
   376  	in  string
   377  	err error
   378  }{
   379  	{"GET / HTTP/1.1\r\nheader:foo\r\n\r\n", nil},
   380  	{"GET / HTTP/1.1\r\nheader:foo\r\n", io.ErrUnexpectedEOF},
   381  	{"", io.EOF},
   382  }
   383  
   384  func TestReadRequestErrors(t *testing.T) {
   385  	for i, tt := range readRequestErrorTests {
   386  		_, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.in)))
   387  		if err != tt.err {
   388  			t.Errorf("%d. got error = %v; want %v", i, err, tt.err)
   389  		}
   390  	}
   391  }
   392  
   393  var newRequestHostTests = []struct {
   394  	in, out string
   395  }{
   396  	{"http://www.example.com/", "www.example.com"},
   397  	{"http://www.example.com:8080/", "www.example.com:8080"},
   398  
   399  	{"http://192.168.0.1/", "192.168.0.1"},
   400  	{"http://192.168.0.1:8080/", "192.168.0.1:8080"},
   401  	{"http://192.168.0.1:/", "192.168.0.1"},
   402  
   403  	{"http://[fe80::1]/", "[fe80::1]"},
   404  	{"http://[fe80::1]:8080/", "[fe80::1]:8080"},
   405  	{"http://[fe80::1%25en0]/", "[fe80::1%en0]"},
   406  	{"http://[fe80::1%25en0]:8080/", "[fe80::1%en0]:8080"},
   407  	{"http://[fe80::1%25en0]:/", "[fe80::1%en0]"},
   408  }
   409  
   410  func TestNewRequestHost(t *testing.T) {
   411  	for i, tt := range newRequestHostTests {
   412  		req, err := NewRequest("GET", tt.in, nil)
   413  		if err != nil {
   414  			t.Errorf("#%v: %v", i, err)
   415  			continue
   416  		}
   417  		if req.Host != tt.out {
   418  			t.Errorf("got %q; want %q", req.Host, tt.out)
   419  		}
   420  	}
   421  }
   422  
   423  func TestRequestInvalidMethod(t *testing.T) {
   424  	_, err := NewRequest("bad method", "http://foo.com/", nil)
   425  	if err == nil {
   426  		t.Error("expected error from NewRequest with invalid method")
   427  	}
   428  	req, err := NewRequest("GET", "http://foo.example/", nil)
   429  	if err != nil {
   430  		t.Fatal(err)
   431  	}
   432  	req.Method = "bad method"
   433  	_, err = DefaultClient.Do(req)
   434  	if err == nil || !strings.Contains(err.Error(), "invalid method") {
   435  		t.Errorf("Transport error = %v; want invalid method", err)
   436  	}
   437  
   438  	req, err = NewRequest("", "http://foo.com/", nil)
   439  	if err != nil {
   440  		t.Errorf("NewRequest(empty method) = %v; want nil", err)
   441  	} else if req.Method != "GET" {
   442  		t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method)
   443  	}
   444  }
   445  
   446  func TestNewRequestContentLength(t *testing.T) {
   447  	readByte := func(r io.Reader) io.Reader {
   448  		var b [1]byte
   449  		r.Read(b[:])
   450  		return r
   451  	}
   452  	tests := []struct {
   453  		r    io.Reader
   454  		want int64
   455  	}{
   456  		{bytes.NewReader([]byte("123")), 3},
   457  		{bytes.NewBuffer([]byte("1234")), 4},
   458  		{strings.NewReader("12345"), 5},
   459  		// Not detected:
   460  		{struct{ io.Reader }{strings.NewReader("xyz")}, 0},
   461  		{io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
   462  		{readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
   463  	}
   464  	for _, tt := range tests {
   465  		req, err := NewRequest("POST", "http://localhost/", tt.r)
   466  		if err != nil {
   467  			t.Fatal(err)
   468  		}
   469  		if req.ContentLength != tt.want {
   470  			t.Errorf("ContentLength(%T) = %d; want %d", tt.r, req.ContentLength, tt.want)
   471  		}
   472  	}
   473  }
   474  
   475  var parseHTTPVersionTests = []struct {
   476  	vers         string
   477  	major, minor int
   478  	ok           bool
   479  }{
   480  	{"HTTP/0.9", 0, 9, true},
   481  	{"HTTP/1.0", 1, 0, true},
   482  	{"HTTP/1.1", 1, 1, true},
   483  	{"HTTP/3.14", 3, 14, true},
   484  
   485  	{"HTTP", 0, 0, false},
   486  	{"HTTP/one.one", 0, 0, false},
   487  	{"HTTP/1.1/", 0, 0, false},
   488  	{"HTTP/-1,0", 0, 0, false},
   489  	{"HTTP/0,-1", 0, 0, false},
   490  	{"HTTP/", 0, 0, false},
   491  	{"HTTP/1,1", 0, 0, false},
   492  }
   493  
   494  func TestParseHTTPVersion(t *testing.T) {
   495  	for _, tt := range parseHTTPVersionTests {
   496  		major, minor, ok := ParseHTTPVersion(tt.vers)
   497  		if ok != tt.ok || major != tt.major || minor != tt.minor {
   498  			type version struct {
   499  				major, minor int
   500  				ok           bool
   501  			}
   502  			t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
   503  		}
   504  	}
   505  }
   506  
   507  type getBasicAuthTest struct {
   508  	username, password string
   509  	ok                 bool
   510  }
   511  
   512  type basicAuthCredentialsTest struct {
   513  	username, password string
   514  }
   515  
   516  var getBasicAuthTests = []struct {
   517  	username, password string
   518  	ok                 bool
   519  }{
   520  	{"Aladdin", "open sesame", true},
   521  	{"Aladdin", "open:sesame", true},
   522  	{"", "", true},
   523  }
   524  
   525  func TestGetBasicAuth(t *testing.T) {
   526  	for _, tt := range getBasicAuthTests {
   527  		r, _ := NewRequest("GET", "http://example.com/", nil)
   528  		r.SetBasicAuth(tt.username, tt.password)
   529  		username, password, ok := r.BasicAuth()
   530  		if ok != tt.ok || username != tt.username || password != tt.password {
   531  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   532  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   533  		}
   534  	}
   535  	// Unauthenticated request.
   536  	r, _ := NewRequest("GET", "http://example.com/", nil)
   537  	username, password, ok := r.BasicAuth()
   538  	if ok {
   539  		t.Errorf("expected false from BasicAuth when the request is unauthenticated")
   540  	}
   541  	want := basicAuthCredentialsTest{"", ""}
   542  	if username != want.username || password != want.password {
   543  		t.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
   544  			want, basicAuthCredentialsTest{username, password})
   545  	}
   546  }
   547  
   548  var parseBasicAuthTests = []struct {
   549  	header, username, password string
   550  	ok                         bool
   551  }{
   552  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   553  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
   554  	{"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
   555  	{"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   556  	{base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   557  	{"Basic ", "", "", false},
   558  	{"Basic Aladdin:open sesame", "", "", false},
   559  	{`Digest username="Aladdin"`, "", "", false},
   560  }
   561  
   562  func TestParseBasicAuth(t *testing.T) {
   563  	for _, tt := range parseBasicAuthTests {
   564  		r, _ := NewRequest("GET", "http://example.com/", nil)
   565  		r.Header.Set("Authorization", tt.header)
   566  		username, password, ok := r.BasicAuth()
   567  		if ok != tt.ok || username != tt.username || password != tt.password {
   568  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   569  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   570  		}
   571  	}
   572  }
   573  
   574  type logWrites struct {
   575  	t   *testing.T
   576  	dst *[]string
   577  }
   578  
   579  func (l logWrites) WriteByte(c byte) error {
   580  	l.t.Fatalf("unexpected WriteByte call")
   581  	return nil
   582  }
   583  
   584  func (l logWrites) Write(p []byte) (n int, err error) {
   585  	*l.dst = append(*l.dst, string(p))
   586  	return len(p), nil
   587  }
   588  
   589  func TestRequestWriteBufferedWriter(t *testing.T) {
   590  	got := []string{}
   591  	req, _ := NewRequest("GET", "http://foo.com/", nil)
   592  	req.Write(logWrites{t, &got})
   593  	want := []string{
   594  		"GET / HTTP/1.1\r\n",
   595  		"Host: foo.com\r\n",
   596  		"User-Agent: " + DefaultUserAgent + "\r\n",
   597  		"\r\n",
   598  	}
   599  	if !reflect.DeepEqual(got, want) {
   600  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   601  	}
   602  }
   603  
   604  func TestRequestBadHost(t *testing.T) {
   605  	got := []string{}
   606  	req, err := NewRequest("GET", "http://foo/after", nil)
   607  	if err != nil {
   608  		t.Fatal(err)
   609  	}
   610  	req.Host = "foo.com with spaces"
   611  	req.URL.Host = "foo.com with spaces"
   612  	req.Write(logWrites{t, &got})
   613  	want := []string{
   614  		"GET /after HTTP/1.1\r\n",
   615  		"Host: foo.com\r\n",
   616  		"User-Agent: " + DefaultUserAgent + "\r\n",
   617  		"\r\n",
   618  	}
   619  	if !reflect.DeepEqual(got, want) {
   620  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   621  	}
   622  }
   623  
   624  func TestStarRequest(t *testing.T) {
   625  	req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
   626  	if err != nil {
   627  		return
   628  	}
   629  	var out bytes.Buffer
   630  	if err := req.Write(&out); err != nil {
   631  		t.Fatal(err)
   632  	}
   633  	back, err := ReadRequest(bufio.NewReader(&out))
   634  	if err != nil {
   635  		t.Fatal(err)
   636  	}
   637  	// Ignore the Headers (the User-Agent breaks the deep equal,
   638  	// but we don't care about it)
   639  	req.Header = nil
   640  	back.Header = nil
   641  	if !reflect.DeepEqual(req, back) {
   642  		t.Errorf("Original request doesn't match Request read back.")
   643  		t.Logf("Original: %#v", req)
   644  		t.Logf("Original.URL: %#v", req.URL)
   645  		t.Logf("Wrote: %s", out.Bytes())
   646  		t.Logf("Read back (doesn't match Original): %#v", back)
   647  	}
   648  }
   649  
   650  type responseWriterJustWriter struct {
   651  	io.Writer
   652  }
   653  
   654  func (responseWriterJustWriter) Header() Header  { panic("should not be called") }
   655  func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
   656  
   657  // delayedEOFReader never returns (n > 0, io.EOF), instead putting
   658  // off the io.EOF until a subsequent Read call.
   659  type delayedEOFReader struct {
   660  	r io.Reader
   661  }
   662  
   663  func (dr delayedEOFReader) Read(p []byte) (n int, err error) {
   664  	n, err = dr.r.Read(p)
   665  	if n > 0 && err == io.EOF {
   666  		err = nil
   667  	}
   668  	return
   669  }
   670  
   671  func TestIssue10884_MaxBytesEOF(t *testing.T) {
   672  	dst := ioutil.Discard
   673  	_, err := io.Copy(dst, MaxBytesReader(
   674  		responseWriterJustWriter{dst},
   675  		ioutil.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
   676  		5))
   677  	if err != nil {
   678  		t.Fatal(err)
   679  	}
   680  }
   681  
   682  // Issue 14981: MaxBytesReader's return error wasn't sticky. It
   683  // doesn't technically need to be, but people expected it to be.
   684  func TestMaxBytesReaderStickyError(t *testing.T) {
   685  	isSticky := func(r io.Reader) error {
   686  		var log bytes.Buffer
   687  		buf := make([]byte, 1000)
   688  		var firstErr error
   689  		for {
   690  			n, err := r.Read(buf)
   691  			fmt.Fprintf(&log, "Read(%d) = %d, %v\n", len(buf), n, err)
   692  			if err == nil {
   693  				continue
   694  			}
   695  			if firstErr == nil {
   696  				firstErr = err
   697  				continue
   698  			}
   699  			if !reflect.DeepEqual(err, firstErr) {
   700  				return fmt.Errorf("non-sticky error. got log:\n%s", log.Bytes())
   701  			}
   702  			t.Logf("Got log: %s", log.Bytes())
   703  			return nil
   704  		}
   705  	}
   706  	tests := [...]struct {
   707  		readable int
   708  		limit    int64
   709  	}{
   710  		0: {99, 100},
   711  		1: {100, 100},
   712  		2: {101, 100},
   713  	}
   714  	for i, tt := range tests {
   715  		rc := MaxBytesReader(nil, ioutil.NopCloser(bytes.NewReader(make([]byte, tt.readable))), tt.limit)
   716  		if err := isSticky(rc); err != nil {
   717  			t.Errorf("%d. error: %v", i, err)
   718  		}
   719  	}
   720  }
   721  
   722  func testMissingFile(t *testing.T, req *Request) {
   723  	f, fh, err := req.FormFile("missing")
   724  	if f != nil {
   725  		t.Errorf("FormFile file = %v, want nil", f)
   726  	}
   727  	if fh != nil {
   728  		t.Errorf("FormFile file header = %q, want nil", fh)
   729  	}
   730  	if err != ErrMissingFile {
   731  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
   732  	}
   733  }
   734  
   735  func newTestMultipartRequest(t *testing.T) *Request {
   736  	b := strings.NewReader(strings.Replace(message, "\n", "\r\n", -1))
   737  	req, err := NewRequest("POST", "/", b)
   738  	if err != nil {
   739  		t.Fatal("NewRequest:", err)
   740  	}
   741  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
   742  	req.Header.Set("Content-type", ctype)
   743  	return req
   744  }
   745  
   746  func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
   747  	if g, e := req.FormValue("texta"), textaValue; g != e {
   748  		t.Errorf("texta value = %q, want %q", g, e)
   749  	}
   750  	if g, e := req.FormValue("textb"), textbValue; g != e {
   751  		t.Errorf("textb value = %q, want %q", g, e)
   752  	}
   753  	if g := req.FormValue("missing"); g != "" {
   754  		t.Errorf("missing value = %q, want empty string", g)
   755  	}
   756  
   757  	assertMem := func(n string, fd multipart.File) {
   758  		if _, ok := fd.(*os.File); ok {
   759  			t.Error(n, " is *os.File, should not be")
   760  		}
   761  	}
   762  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
   763  	defer fda.Close()
   764  	assertMem("filea", fda)
   765  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
   766  	defer fdb.Close()
   767  	if allMem {
   768  		assertMem("fileb", fdb)
   769  	} else {
   770  		if _, ok := fdb.(*os.File); !ok {
   771  			t.Errorf("fileb has unexpected underlying type %T", fdb)
   772  		}
   773  	}
   774  
   775  	testMissingFile(t, req)
   776  }
   777  
   778  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
   779  	f, fh, err := req.FormFile(key)
   780  	if err != nil {
   781  		t.Fatalf("FormFile(%q): %q", key, err)
   782  	}
   783  	if fh.Filename != expectFilename {
   784  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
   785  	}
   786  	var b bytes.Buffer
   787  	_, err = io.Copy(&b, f)
   788  	if err != nil {
   789  		t.Fatal("copying contents:", err)
   790  	}
   791  	if g := b.String(); g != expectContent {
   792  		t.Errorf("contents = %q, want %q", g, expectContent)
   793  	}
   794  	return f
   795  }
   796  
   797  const (
   798  	fileaContents = "This is a test file."
   799  	filebContents = "Another test file."
   800  	textaValue    = "foo"
   801  	textbValue    = "bar"
   802  	boundary      = `MyBoundary`
   803  )
   804  
   805  const message = `
   806  --MyBoundary
   807  Content-Disposition: form-data; name="filea"; filename="filea.txt"
   808  Content-Type: text/plain
   809  
   810  ` + fileaContents + `
   811  --MyBoundary
   812  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
   813  Content-Type: text/plain
   814  
   815  ` + filebContents + `
   816  --MyBoundary
   817  Content-Disposition: form-data; name="texta"
   818  
   819  ` + textaValue + `
   820  --MyBoundary
   821  Content-Disposition: form-data; name="textb"
   822  
   823  ` + textbValue + `
   824  --MyBoundary--
   825  `
   826  
   827  func benchmarkReadRequest(b *testing.B, request string) {
   828  	request = request + "\n"                             // final \n
   829  	request = strings.Replace(request, "\n", "\r\n", -1) // expand \n to \r\n
   830  	b.SetBytes(int64(len(request)))
   831  	r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
   832  	b.ReportAllocs()
   833  	b.ResetTimer()
   834  	for i := 0; i < b.N; i++ {
   835  		_, err := ReadRequest(r)
   836  		if err != nil {
   837  			b.Fatalf("failed to read request: %v", err)
   838  		}
   839  	}
   840  }
   841  
   842  // infiniteReader satisfies Read requests as if the contents of buf
   843  // loop indefinitely.
   844  type infiniteReader struct {
   845  	buf    []byte
   846  	offset int
   847  }
   848  
   849  func (r *infiniteReader) Read(b []byte) (int, error) {
   850  	n := copy(b, r.buf[r.offset:])
   851  	r.offset = (r.offset + n) % len(r.buf)
   852  	return n, nil
   853  }
   854  
   855  func BenchmarkReadRequestChrome(b *testing.B) {
   856  	// https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
   857  	benchmarkReadRequest(b, `GET / HTTP/1.1
   858  Host: localhost:8080
   859  Connection: keep-alive
   860  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
   861  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
   862  Accept-Encoding: gzip,deflate,sdch
   863  Accept-Language: en-US,en;q=0.8
   864  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
   865  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
   866  `)
   867  }
   868  
   869  func BenchmarkReadRequestCurl(b *testing.B) {
   870  	// curl http://localhost:8080/
   871  	benchmarkReadRequest(b, `GET / HTTP/1.1
   872  User-Agent: curl/7.27.0
   873  Host: localhost:8080
   874  Accept: */*
   875  `)
   876  }
   877  
   878  func BenchmarkReadRequestApachebench(b *testing.B) {
   879  	// ab -n 1 -c 1 http://localhost:8080/
   880  	benchmarkReadRequest(b, `GET / HTTP/1.0
   881  Host: localhost:8080
   882  User-Agent: ApacheBench/2.3
   883  Accept: */*
   884  `)
   885  }
   886  
   887  func BenchmarkReadRequestSiege(b *testing.B) {
   888  	// siege -r 1 -c 1 http://localhost:8080/
   889  	benchmarkReadRequest(b, `GET / HTTP/1.1
   890  Host: localhost:8080
   891  Accept: */*
   892  Accept-Encoding: gzip
   893  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
   894  Connection: keep-alive
   895  `)
   896  }
   897  
   898  func BenchmarkReadRequestWrk(b *testing.B) {
   899  	// wrk -t 1 -r 1 -c 1 http://localhost:8080/
   900  	benchmarkReadRequest(b, `GET / HTTP/1.1
   901  Host: localhost:8080
   902  `)
   903  }