github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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  
   402  	{"http://[fe80::1]/", "[fe80::1]"},
   403  	{"http://[fe80::1]:8080/", "[fe80::1]:8080"},
   404  	{"http://[fe80::1%25en0]/", "[fe80::1%en0]"},
   405  	{"http://[fe80::1%25en0]:8080/", "[fe80::1%en0]:8080"},
   406  }
   407  
   408  func TestNewRequestHost(t *testing.T) {
   409  	for i, tt := range newRequestHostTests {
   410  		req, err := NewRequest("GET", tt.in, nil)
   411  		if err != nil {
   412  			t.Errorf("#%v: %v", i, err)
   413  			continue
   414  		}
   415  		if req.Host != tt.out {
   416  			t.Errorf("got %q; want %q", req.Host, tt.out)
   417  		}
   418  	}
   419  }
   420  
   421  func TestRequestInvalidMethod(t *testing.T) {
   422  	_, err := NewRequest("bad method", "http://foo.com/", nil)
   423  	if err == nil {
   424  		t.Error("expected error from NewRequest with invalid method")
   425  	}
   426  	req, err := NewRequest("GET", "http://foo.example/", nil)
   427  	if err != nil {
   428  		t.Fatal(err)
   429  	}
   430  	req.Method = "bad method"
   431  	_, err = DefaultClient.Do(req)
   432  	if err == nil || !strings.Contains(err.Error(), "invalid method") {
   433  		t.Errorf("Transport error = %v; want invalid method", err)
   434  	}
   435  
   436  	req, err = NewRequest("", "http://foo.com/", nil)
   437  	if err != nil {
   438  		t.Errorf("NewRequest(empty method) = %v; want nil", err)
   439  	} else if req.Method != "GET" {
   440  		t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method)
   441  	}
   442  }
   443  
   444  func TestNewRequestContentLength(t *testing.T) {
   445  	readByte := func(r io.Reader) io.Reader {
   446  		var b [1]byte
   447  		r.Read(b[:])
   448  		return r
   449  	}
   450  	tests := []struct {
   451  		r    io.Reader
   452  		want int64
   453  	}{
   454  		{bytes.NewReader([]byte("123")), 3},
   455  		{bytes.NewBuffer([]byte("1234")), 4},
   456  		{strings.NewReader("12345"), 5},
   457  		// Not detected:
   458  		{struct{ io.Reader }{strings.NewReader("xyz")}, 0},
   459  		{io.NewSectionReader(strings.NewReader("x"), 0, 6), 0},
   460  		{readByte(io.NewSectionReader(strings.NewReader("xy"), 0, 6)), 0},
   461  	}
   462  	for _, tt := range tests {
   463  		req, err := NewRequest("POST", "http://localhost/", tt.r)
   464  		if err != nil {
   465  			t.Fatal(err)
   466  		}
   467  		if req.ContentLength != tt.want {
   468  			t.Errorf("ContentLength(%T) = %d; want %d", tt.r, req.ContentLength, tt.want)
   469  		}
   470  	}
   471  }
   472  
   473  var parseHTTPVersionTests = []struct {
   474  	vers         string
   475  	major, minor int
   476  	ok           bool
   477  }{
   478  	{"HTTP/0.9", 0, 9, true},
   479  	{"HTTP/1.0", 1, 0, true},
   480  	{"HTTP/1.1", 1, 1, true},
   481  	{"HTTP/3.14", 3, 14, true},
   482  
   483  	{"HTTP", 0, 0, false},
   484  	{"HTTP/one.one", 0, 0, false},
   485  	{"HTTP/1.1/", 0, 0, false},
   486  	{"HTTP/-1,0", 0, 0, false},
   487  	{"HTTP/0,-1", 0, 0, false},
   488  	{"HTTP/", 0, 0, false},
   489  	{"HTTP/1,1", 0, 0, false},
   490  }
   491  
   492  func TestParseHTTPVersion(t *testing.T) {
   493  	for _, tt := range parseHTTPVersionTests {
   494  		major, minor, ok := ParseHTTPVersion(tt.vers)
   495  		if ok != tt.ok || major != tt.major || minor != tt.minor {
   496  			type version struct {
   497  				major, minor int
   498  				ok           bool
   499  			}
   500  			t.Errorf("failed to parse %q, expected: %#v, got %#v", tt.vers, version{tt.major, tt.minor, tt.ok}, version{major, minor, ok})
   501  		}
   502  	}
   503  }
   504  
   505  type getBasicAuthTest struct {
   506  	username, password string
   507  	ok                 bool
   508  }
   509  
   510  type basicAuthCredentialsTest struct {
   511  	username, password string
   512  }
   513  
   514  var getBasicAuthTests = []struct {
   515  	username, password string
   516  	ok                 bool
   517  }{
   518  	{"Aladdin", "open sesame", true},
   519  	{"Aladdin", "open:sesame", true},
   520  	{"", "", true},
   521  }
   522  
   523  func TestGetBasicAuth(t *testing.T) {
   524  	for _, tt := range getBasicAuthTests {
   525  		r, _ := NewRequest("GET", "http://example.com/", nil)
   526  		r.SetBasicAuth(tt.username, tt.password)
   527  		username, password, ok := r.BasicAuth()
   528  		if ok != tt.ok || username != tt.username || password != tt.password {
   529  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   530  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   531  		}
   532  	}
   533  	// Unauthenticated request.
   534  	r, _ := NewRequest("GET", "http://example.com/", nil)
   535  	username, password, ok := r.BasicAuth()
   536  	if ok {
   537  		t.Errorf("expected false from BasicAuth when the request is unauthenticated")
   538  	}
   539  	want := basicAuthCredentialsTest{"", ""}
   540  	if username != want.username || password != want.password {
   541  		t.Errorf("expected credentials: %#v when the request is unauthenticated, got %#v",
   542  			want, basicAuthCredentialsTest{username, password})
   543  	}
   544  }
   545  
   546  var parseBasicAuthTests = []struct {
   547  	header, username, password string
   548  	ok                         bool
   549  }{
   550  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
   551  	{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
   552  	{"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
   553  	{"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   554  	{base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
   555  	{"Basic ", "", "", false},
   556  	{"Basic Aladdin:open sesame", "", "", false},
   557  	{`Digest username="Aladdin"`, "", "", false},
   558  }
   559  
   560  func TestParseBasicAuth(t *testing.T) {
   561  	for _, tt := range parseBasicAuthTests {
   562  		r, _ := NewRequest("GET", "http://example.com/", nil)
   563  		r.Header.Set("Authorization", tt.header)
   564  		username, password, ok := r.BasicAuth()
   565  		if ok != tt.ok || username != tt.username || password != tt.password {
   566  			t.Errorf("BasicAuth() = %#v, want %#v", getBasicAuthTest{username, password, ok},
   567  				getBasicAuthTest{tt.username, tt.password, tt.ok})
   568  		}
   569  	}
   570  }
   571  
   572  type logWrites struct {
   573  	t   *testing.T
   574  	dst *[]string
   575  }
   576  
   577  func (l logWrites) WriteByte(c byte) error {
   578  	l.t.Fatalf("unexpected WriteByte call")
   579  	return nil
   580  }
   581  
   582  func (l logWrites) Write(p []byte) (n int, err error) {
   583  	*l.dst = append(*l.dst, string(p))
   584  	return len(p), nil
   585  }
   586  
   587  func TestRequestWriteBufferedWriter(t *testing.T) {
   588  	got := []string{}
   589  	req, _ := NewRequest("GET", "http://foo.com/", nil)
   590  	req.Write(logWrites{t, &got})
   591  	want := []string{
   592  		"GET / HTTP/1.1\r\n",
   593  		"Host: foo.com\r\n",
   594  		"User-Agent: " + DefaultUserAgent + "\r\n",
   595  		"\r\n",
   596  	}
   597  	if !reflect.DeepEqual(got, want) {
   598  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   599  	}
   600  }
   601  
   602  func TestRequestBadHost(t *testing.T) {
   603  	got := []string{}
   604  	req, err := NewRequest("GET", "http://foo/after", nil)
   605  	if err != nil {
   606  		t.Fatal(err)
   607  	}
   608  	req.Host = "foo.com with spaces"
   609  	req.URL.Host = "foo.com with spaces"
   610  	req.Write(logWrites{t, &got})
   611  	want := []string{
   612  		"GET /after HTTP/1.1\r\n",
   613  		"Host: foo.com\r\n",
   614  		"User-Agent: " + DefaultUserAgent + "\r\n",
   615  		"\r\n",
   616  	}
   617  	if !reflect.DeepEqual(got, want) {
   618  		t.Errorf("Writes = %q\n  Want = %q", got, want)
   619  	}
   620  }
   621  
   622  func TestStarRequest(t *testing.T) {
   623  	req, err := ReadRequest(bufio.NewReader(strings.NewReader("M-SEARCH * HTTP/1.1\r\n\r\n")))
   624  	if err != nil {
   625  		return
   626  	}
   627  	var out bytes.Buffer
   628  	if err := req.Write(&out); err != nil {
   629  		t.Fatal(err)
   630  	}
   631  	back, err := ReadRequest(bufio.NewReader(&out))
   632  	if err != nil {
   633  		t.Fatal(err)
   634  	}
   635  	// Ignore the Headers (the User-Agent breaks the deep equal,
   636  	// but we don't care about it)
   637  	req.Header = nil
   638  	back.Header = nil
   639  	if !reflect.DeepEqual(req, back) {
   640  		t.Errorf("Original request doesn't match Request read back.")
   641  		t.Logf("Original: %#v", req)
   642  		t.Logf("Original.URL: %#v", req.URL)
   643  		t.Logf("Wrote: %s", out.Bytes())
   644  		t.Logf("Read back (doesn't match Original): %#v", back)
   645  	}
   646  }
   647  
   648  type responseWriterJustWriter struct {
   649  	io.Writer
   650  }
   651  
   652  func (responseWriterJustWriter) Header() Header  { panic("should not be called") }
   653  func (responseWriterJustWriter) WriteHeader(int) { panic("should not be called") }
   654  
   655  // delayedEOFReader never returns (n > 0, io.EOF), instead putting
   656  // off the io.EOF until a subsequent Read call.
   657  type delayedEOFReader struct {
   658  	r io.Reader
   659  }
   660  
   661  func (dr delayedEOFReader) Read(p []byte) (n int, err error) {
   662  	n, err = dr.r.Read(p)
   663  	if n > 0 && err == io.EOF {
   664  		err = nil
   665  	}
   666  	return
   667  }
   668  
   669  func TestIssue10884_MaxBytesEOF(t *testing.T) {
   670  	dst := ioutil.Discard
   671  	_, err := io.Copy(dst, MaxBytesReader(
   672  		responseWriterJustWriter{dst},
   673  		ioutil.NopCloser(delayedEOFReader{strings.NewReader("12345")}),
   674  		5))
   675  	if err != nil {
   676  		t.Fatal(err)
   677  	}
   678  }
   679  
   680  func testMissingFile(t *testing.T, req *Request) {
   681  	f, fh, err := req.FormFile("missing")
   682  	if f != nil {
   683  		t.Errorf("FormFile file = %v, want nil", f)
   684  	}
   685  	if fh != nil {
   686  		t.Errorf("FormFile file header = %q, want nil", fh)
   687  	}
   688  	if err != ErrMissingFile {
   689  		t.Errorf("FormFile err = %q, want ErrMissingFile", err)
   690  	}
   691  }
   692  
   693  func newTestMultipartRequest(t *testing.T) *Request {
   694  	b := strings.NewReader(strings.Replace(message, "\n", "\r\n", -1))
   695  	req, err := NewRequest("POST", "/", b)
   696  	if err != nil {
   697  		t.Fatal("NewRequest:", err)
   698  	}
   699  	ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
   700  	req.Header.Set("Content-type", ctype)
   701  	return req
   702  }
   703  
   704  func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
   705  	if g, e := req.FormValue("texta"), textaValue; g != e {
   706  		t.Errorf("texta value = %q, want %q", g, e)
   707  	}
   708  	if g, e := req.FormValue("textb"), textbValue; g != e {
   709  		t.Errorf("textb value = %q, want %q", g, e)
   710  	}
   711  	if g := req.FormValue("missing"); g != "" {
   712  		t.Errorf("missing value = %q, want empty string", g)
   713  	}
   714  
   715  	assertMem := func(n string, fd multipart.File) {
   716  		if _, ok := fd.(*os.File); ok {
   717  			t.Error(n, " is *os.File, should not be")
   718  		}
   719  	}
   720  	fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
   721  	defer fda.Close()
   722  	assertMem("filea", fda)
   723  	fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
   724  	defer fdb.Close()
   725  	if allMem {
   726  		assertMem("fileb", fdb)
   727  	} else {
   728  		if _, ok := fdb.(*os.File); !ok {
   729  			t.Errorf("fileb has unexpected underlying type %T", fdb)
   730  		}
   731  	}
   732  
   733  	testMissingFile(t, req)
   734  }
   735  
   736  func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
   737  	f, fh, err := req.FormFile(key)
   738  	if err != nil {
   739  		t.Fatalf("FormFile(%q): %q", key, err)
   740  	}
   741  	if fh.Filename != expectFilename {
   742  		t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
   743  	}
   744  	var b bytes.Buffer
   745  	_, err = io.Copy(&b, f)
   746  	if err != nil {
   747  		t.Fatal("copying contents:", err)
   748  	}
   749  	if g := b.String(); g != expectContent {
   750  		t.Errorf("contents = %q, want %q", g, expectContent)
   751  	}
   752  	return f
   753  }
   754  
   755  const (
   756  	fileaContents = "This is a test file."
   757  	filebContents = "Another test file."
   758  	textaValue    = "foo"
   759  	textbValue    = "bar"
   760  	boundary      = `MyBoundary`
   761  )
   762  
   763  const message = `
   764  --MyBoundary
   765  Content-Disposition: form-data; name="filea"; filename="filea.txt"
   766  Content-Type: text/plain
   767  
   768  ` + fileaContents + `
   769  --MyBoundary
   770  Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
   771  Content-Type: text/plain
   772  
   773  ` + filebContents + `
   774  --MyBoundary
   775  Content-Disposition: form-data; name="texta"
   776  
   777  ` + textaValue + `
   778  --MyBoundary
   779  Content-Disposition: form-data; name="textb"
   780  
   781  ` + textbValue + `
   782  --MyBoundary--
   783  `
   784  
   785  func benchmarkReadRequest(b *testing.B, request string) {
   786  	request = request + "\n"                             // final \n
   787  	request = strings.Replace(request, "\n", "\r\n", -1) // expand \n to \r\n
   788  	b.SetBytes(int64(len(request)))
   789  	r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
   790  	b.ReportAllocs()
   791  	b.ResetTimer()
   792  	for i := 0; i < b.N; i++ {
   793  		_, err := ReadRequest(r)
   794  		if err != nil {
   795  			b.Fatalf("failed to read request: %v", err)
   796  		}
   797  	}
   798  }
   799  
   800  // infiniteReader satisfies Read requests as if the contents of buf
   801  // loop indefinitely.
   802  type infiniteReader struct {
   803  	buf    []byte
   804  	offset int
   805  }
   806  
   807  func (r *infiniteReader) Read(b []byte) (int, error) {
   808  	n := copy(b, r.buf[r.offset:])
   809  	r.offset = (r.offset + n) % len(r.buf)
   810  	return n, nil
   811  }
   812  
   813  func BenchmarkReadRequestChrome(b *testing.B) {
   814  	// https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
   815  	benchmarkReadRequest(b, `GET / HTTP/1.1
   816  Host: localhost:8080
   817  Connection: keep-alive
   818  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
   819  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
   820  Accept-Encoding: gzip,deflate,sdch
   821  Accept-Language: en-US,en;q=0.8
   822  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
   823  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
   824  `)
   825  }
   826  
   827  func BenchmarkReadRequestCurl(b *testing.B) {
   828  	// curl http://localhost:8080/
   829  	benchmarkReadRequest(b, `GET / HTTP/1.1
   830  User-Agent: curl/7.27.0
   831  Host: localhost:8080
   832  Accept: */*
   833  `)
   834  }
   835  
   836  func BenchmarkReadRequestApachebench(b *testing.B) {
   837  	// ab -n 1 -c 1 http://localhost:8080/
   838  	benchmarkReadRequest(b, `GET / HTTP/1.0
   839  Host: localhost:8080
   840  User-Agent: ApacheBench/2.3
   841  Accept: */*
   842  `)
   843  }
   844  
   845  func BenchmarkReadRequestSiege(b *testing.B) {
   846  	// siege -r 1 -c 1 http://localhost:8080/
   847  	benchmarkReadRequest(b, `GET / HTTP/1.1
   848  Host: localhost:8080
   849  Accept: */*
   850  Accept-Encoding: gzip
   851  User-Agent: JoeDog/1.00 [en] (X11; I; Siege 2.70)
   852  Connection: keep-alive
   853  `)
   854  }
   855  
   856  func BenchmarkReadRequestWrk(b *testing.B) {
   857  	// wrk -t 1 -r 1 -c 1 http://localhost:8080/
   858  	benchmarkReadRequest(b, `GET / HTTP/1.1
   859  Host: localhost:8080
   860  `)
   861  }