github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/net/http/serve_test.go (about)

     1  // Copyright 2010 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  // End-to-end serving tests
     6  
     7  package http_test
     8  
     9  import (
    10  	"bufio"
    11  	"bytes"
    12  	"crypto/tls"
    13  	"errors"
    14  	"fmt"
    15  	"io"
    16  	"io/ioutil"
    17  	"log"
    18  	"net"
    19  	. "net/http"
    20  	"net/http/httptest"
    21  	"net/http/httputil"
    22  	"net/url"
    23  	"os"
    24  	"os/exec"
    25  	"reflect"
    26  	"runtime"
    27  	"strconv"
    28  	"strings"
    29  	"sync"
    30  	"sync/atomic"
    31  	"syscall"
    32  	"testing"
    33  	"time"
    34  )
    35  
    36  type dummyAddr string
    37  type oneConnListener struct {
    38  	conn net.Conn
    39  }
    40  
    41  func (l *oneConnListener) Accept() (c net.Conn, err error) {
    42  	c = l.conn
    43  	if c == nil {
    44  		err = io.EOF
    45  		return
    46  	}
    47  	err = nil
    48  	l.conn = nil
    49  	return
    50  }
    51  
    52  func (l *oneConnListener) Close() error {
    53  	return nil
    54  }
    55  
    56  func (l *oneConnListener) Addr() net.Addr {
    57  	return dummyAddr("test-address")
    58  }
    59  
    60  func (a dummyAddr) Network() string {
    61  	return string(a)
    62  }
    63  
    64  func (a dummyAddr) String() string {
    65  	return string(a)
    66  }
    67  
    68  type noopConn struct{}
    69  
    70  func (noopConn) LocalAddr() net.Addr                { return dummyAddr("local-addr") }
    71  func (noopConn) RemoteAddr() net.Addr               { return dummyAddr("remote-addr") }
    72  func (noopConn) SetDeadline(t time.Time) error      { return nil }
    73  func (noopConn) SetReadDeadline(t time.Time) error  { return nil }
    74  func (noopConn) SetWriteDeadline(t time.Time) error { return nil }
    75  
    76  type rwTestConn struct {
    77  	io.Reader
    78  	io.Writer
    79  	noopConn
    80  
    81  	closeFunc func() error // called if non-nil
    82  	closec    chan bool    // else, if non-nil, send value to it on close
    83  }
    84  
    85  func (c *rwTestConn) Close() error {
    86  	if c.closeFunc != nil {
    87  		return c.closeFunc()
    88  	}
    89  	select {
    90  	case c.closec <- true:
    91  	default:
    92  	}
    93  	return nil
    94  }
    95  
    96  type testConn struct {
    97  	readBuf  bytes.Buffer
    98  	writeBuf bytes.Buffer
    99  	closec   chan bool // if non-nil, send value to it on close
   100  	noopConn
   101  }
   102  
   103  func (c *testConn) Read(b []byte) (int, error) {
   104  	return c.readBuf.Read(b)
   105  }
   106  
   107  func (c *testConn) Write(b []byte) (int, error) {
   108  	return c.writeBuf.Write(b)
   109  }
   110  
   111  func (c *testConn) Close() error {
   112  	select {
   113  	case c.closec <- true:
   114  	default:
   115  	}
   116  	return nil
   117  }
   118  
   119  // reqBytes treats req as a request (with \n delimiters) and returns it with \r\n delimiters,
   120  // ending in \r\n\r\n
   121  func reqBytes(req string) []byte {
   122  	return []byte(strings.Replace(strings.TrimSpace(req), "\n", "\r\n", -1) + "\r\n\r\n")
   123  }
   124  
   125  type handlerTest struct {
   126  	handler Handler
   127  }
   128  
   129  func newHandlerTest(h Handler) handlerTest {
   130  	return handlerTest{h}
   131  }
   132  
   133  func (ht handlerTest) rawResponse(req string) string {
   134  	reqb := reqBytes(req)
   135  	var output bytes.Buffer
   136  	conn := &rwTestConn{
   137  		Reader: bytes.NewReader(reqb),
   138  		Writer: &output,
   139  		closec: make(chan bool, 1),
   140  	}
   141  	ln := &oneConnListener{conn: conn}
   142  	go Serve(ln, ht.handler)
   143  	<-conn.closec
   144  	return output.String()
   145  }
   146  
   147  func TestConsumingBodyOnNextConn(t *testing.T) {
   148  	conn := new(testConn)
   149  	for i := 0; i < 2; i++ {
   150  		conn.readBuf.Write([]byte(
   151  			"POST / HTTP/1.1\r\n" +
   152  				"Host: test\r\n" +
   153  				"Content-Length: 11\r\n" +
   154  				"\r\n" +
   155  				"foo=1&bar=1"))
   156  	}
   157  
   158  	reqNum := 0
   159  	ch := make(chan *Request)
   160  	servech := make(chan error)
   161  	listener := &oneConnListener{conn}
   162  	handler := func(res ResponseWriter, req *Request) {
   163  		reqNum++
   164  		ch <- req
   165  	}
   166  
   167  	go func() {
   168  		servech <- Serve(listener, HandlerFunc(handler))
   169  	}()
   170  
   171  	var req *Request
   172  	req = <-ch
   173  	if req == nil {
   174  		t.Fatal("Got nil first request.")
   175  	}
   176  	if req.Method != "POST" {
   177  		t.Errorf("For request #1's method, got %q; expected %q",
   178  			req.Method, "POST")
   179  	}
   180  
   181  	req = <-ch
   182  	if req == nil {
   183  		t.Fatal("Got nil first request.")
   184  	}
   185  	if req.Method != "POST" {
   186  		t.Errorf("For request #2's method, got %q; expected %q",
   187  			req.Method, "POST")
   188  	}
   189  
   190  	if serveerr := <-servech; serveerr != io.EOF {
   191  		t.Errorf("Serve returned %q; expected EOF", serveerr)
   192  	}
   193  }
   194  
   195  type stringHandler string
   196  
   197  func (s stringHandler) ServeHTTP(w ResponseWriter, r *Request) {
   198  	w.Header().Set("Result", string(s))
   199  }
   200  
   201  var handlers = []struct {
   202  	pattern string
   203  	msg     string
   204  }{
   205  	{"/", "Default"},
   206  	{"/someDir/", "someDir"},
   207  	{"someHost.com/someDir/", "someHost.com/someDir"},
   208  }
   209  
   210  var vtests = []struct {
   211  	url      string
   212  	expected string
   213  }{
   214  	{"http://localhost/someDir/apage", "someDir"},
   215  	{"http://localhost/otherDir/apage", "Default"},
   216  	{"http://someHost.com/someDir/apage", "someHost.com/someDir"},
   217  	{"http://otherHost.com/someDir/apage", "someDir"},
   218  	{"http://otherHost.com/aDir/apage", "Default"},
   219  	// redirections for trees
   220  	{"http://localhost/someDir", "/someDir/"},
   221  	{"http://someHost.com/someDir", "/someDir/"},
   222  }
   223  
   224  func TestHostHandlers(t *testing.T) {
   225  	defer afterTest(t)
   226  	mux := NewServeMux()
   227  	for _, h := range handlers {
   228  		mux.Handle(h.pattern, stringHandler(h.msg))
   229  	}
   230  	ts := httptest.NewServer(mux)
   231  	defer ts.Close()
   232  
   233  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   234  	if err != nil {
   235  		t.Fatal(err)
   236  	}
   237  	defer conn.Close()
   238  	cc := httputil.NewClientConn(conn, nil)
   239  	for _, vt := range vtests {
   240  		var r *Response
   241  		var req Request
   242  		if req.URL, err = url.Parse(vt.url); err != nil {
   243  			t.Errorf("cannot parse url: %v", err)
   244  			continue
   245  		}
   246  		if err := cc.Write(&req); err != nil {
   247  			t.Errorf("writing request: %v", err)
   248  			continue
   249  		}
   250  		r, err := cc.Read(&req)
   251  		if err != nil {
   252  			t.Errorf("reading response: %v", err)
   253  			continue
   254  		}
   255  		switch r.StatusCode {
   256  		case StatusOK:
   257  			s := r.Header.Get("Result")
   258  			if s != vt.expected {
   259  				t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
   260  			}
   261  		case StatusMovedPermanently:
   262  			s := r.Header.Get("Location")
   263  			if s != vt.expected {
   264  				t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
   265  			}
   266  		default:
   267  			t.Errorf("Get(%q) unhandled status code %d", vt.url, r.StatusCode)
   268  		}
   269  	}
   270  }
   271  
   272  var serveMuxRegister = []struct {
   273  	pattern string
   274  	h       Handler
   275  }{
   276  	{"/dir/", serve(200)},
   277  	{"/search", serve(201)},
   278  	{"codesearch.google.com/search", serve(202)},
   279  	{"codesearch.google.com/", serve(203)},
   280  	{"example.com/", HandlerFunc(checkQueryStringHandler)},
   281  }
   282  
   283  // serve returns a handler that sends a response with the given code.
   284  func serve(code int) HandlerFunc {
   285  	return func(w ResponseWriter, r *Request) {
   286  		w.WriteHeader(code)
   287  	}
   288  }
   289  
   290  // checkQueryStringHandler checks if r.URL.RawQuery has the same value
   291  // as the URL excluding the scheme and the query string and sends 200
   292  // response code if it is, 500 otherwise.
   293  func checkQueryStringHandler(w ResponseWriter, r *Request) {
   294  	u := *r.URL
   295  	u.Scheme = "http"
   296  	u.Host = r.Host
   297  	u.RawQuery = ""
   298  	if "http://"+r.URL.RawQuery == u.String() {
   299  		w.WriteHeader(200)
   300  	} else {
   301  		w.WriteHeader(500)
   302  	}
   303  }
   304  
   305  var serveMuxTests = []struct {
   306  	method  string
   307  	host    string
   308  	path    string
   309  	code    int
   310  	pattern string
   311  }{
   312  	{"GET", "google.com", "/", 404, ""},
   313  	{"GET", "google.com", "/dir", 301, "/dir/"},
   314  	{"GET", "google.com", "/dir/", 200, "/dir/"},
   315  	{"GET", "google.com", "/dir/file", 200, "/dir/"},
   316  	{"GET", "google.com", "/search", 201, "/search"},
   317  	{"GET", "google.com", "/search/", 404, ""},
   318  	{"GET", "google.com", "/search/foo", 404, ""},
   319  	{"GET", "codesearch.google.com", "/search", 202, "codesearch.google.com/search"},
   320  	{"GET", "codesearch.google.com", "/search/", 203, "codesearch.google.com/"},
   321  	{"GET", "codesearch.google.com", "/search/foo", 203, "codesearch.google.com/"},
   322  	{"GET", "codesearch.google.com", "/", 203, "codesearch.google.com/"},
   323  	{"GET", "images.google.com", "/search", 201, "/search"},
   324  	{"GET", "images.google.com", "/search/", 404, ""},
   325  	{"GET", "images.google.com", "/search/foo", 404, ""},
   326  	{"GET", "google.com", "/../search", 301, "/search"},
   327  	{"GET", "google.com", "/dir/..", 301, ""},
   328  	{"GET", "google.com", "/dir/..", 301, ""},
   329  	{"GET", "google.com", "/dir/./file", 301, "/dir/"},
   330  
   331  	// The /foo -> /foo/ redirect applies to CONNECT requests
   332  	// but the path canonicalization does not.
   333  	{"CONNECT", "google.com", "/dir", 301, "/dir/"},
   334  	{"CONNECT", "google.com", "/../search", 404, ""},
   335  	{"CONNECT", "google.com", "/dir/..", 200, "/dir/"},
   336  	{"CONNECT", "google.com", "/dir/..", 200, "/dir/"},
   337  	{"CONNECT", "google.com", "/dir/./file", 200, "/dir/"},
   338  }
   339  
   340  func TestServeMuxHandler(t *testing.T) {
   341  	mux := NewServeMux()
   342  	for _, e := range serveMuxRegister {
   343  		mux.Handle(e.pattern, e.h)
   344  	}
   345  
   346  	for _, tt := range serveMuxTests {
   347  		r := &Request{
   348  			Method: tt.method,
   349  			Host:   tt.host,
   350  			URL: &url.URL{
   351  				Path: tt.path,
   352  			},
   353  		}
   354  		h, pattern := mux.Handler(r)
   355  		rr := httptest.NewRecorder()
   356  		h.ServeHTTP(rr, r)
   357  		if pattern != tt.pattern || rr.Code != tt.code {
   358  			t.Errorf("%s %s %s = %d, %q, want %d, %q", tt.method, tt.host, tt.path, rr.Code, pattern, tt.code, tt.pattern)
   359  		}
   360  	}
   361  }
   362  
   363  var serveMuxTests2 = []struct {
   364  	method  string
   365  	host    string
   366  	url     string
   367  	code    int
   368  	redirOk bool
   369  }{
   370  	{"GET", "google.com", "/", 404, false},
   371  	{"GET", "example.com", "/test/?example.com/test/", 200, false},
   372  	{"GET", "example.com", "test/?example.com/test/", 200, true},
   373  }
   374  
   375  // TestServeMuxHandlerRedirects tests that automatic redirects generated by
   376  // mux.Handler() shouldn't clear the request's query string.
   377  func TestServeMuxHandlerRedirects(t *testing.T) {
   378  	mux := NewServeMux()
   379  	for _, e := range serveMuxRegister {
   380  		mux.Handle(e.pattern, e.h)
   381  	}
   382  
   383  	for _, tt := range serveMuxTests2 {
   384  		tries := 1
   385  		turl := tt.url
   386  		for tries > 0 {
   387  			u, e := url.Parse(turl)
   388  			if e != nil {
   389  				t.Fatal(e)
   390  			}
   391  			r := &Request{
   392  				Method: tt.method,
   393  				Host:   tt.host,
   394  				URL:    u,
   395  			}
   396  			h, _ := mux.Handler(r)
   397  			rr := httptest.NewRecorder()
   398  			h.ServeHTTP(rr, r)
   399  			if rr.Code != 301 {
   400  				if rr.Code != tt.code {
   401  					t.Errorf("%s %s %s = %d, want %d", tt.method, tt.host, tt.url, rr.Code, tt.code)
   402  				}
   403  				break
   404  			}
   405  			if !tt.redirOk {
   406  				t.Errorf("%s %s %s, unexpected redirect", tt.method, tt.host, tt.url)
   407  				break
   408  			}
   409  			turl = rr.HeaderMap.Get("Location")
   410  			tries--
   411  		}
   412  		if tries < 0 {
   413  			t.Errorf("%s %s %s, too many redirects", tt.method, tt.host, tt.url)
   414  		}
   415  	}
   416  }
   417  
   418  // Tests for http://code.google.com/p/go/issues/detail?id=900
   419  func TestMuxRedirectLeadingSlashes(t *testing.T) {
   420  	paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"}
   421  	for _, path := range paths {
   422  		req, err := ReadRequest(bufio.NewReader(bytes.NewBufferString("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n")))
   423  		if err != nil {
   424  			t.Errorf("%s", err)
   425  		}
   426  		mux := NewServeMux()
   427  		resp := httptest.NewRecorder()
   428  
   429  		mux.ServeHTTP(resp, req)
   430  
   431  		if loc, expected := resp.Header().Get("Location"), "/foo.txt"; loc != expected {
   432  			t.Errorf("Expected Location header set to %q; got %q", expected, loc)
   433  			return
   434  		}
   435  
   436  		if code, expected := resp.Code, StatusMovedPermanently; code != expected {
   437  			t.Errorf("Expected response code of StatusMovedPermanently; got %d", code)
   438  			return
   439  		}
   440  	}
   441  }
   442  
   443  func TestServerTimeouts(t *testing.T) {
   444  	defer afterTest(t)
   445  	reqNum := 0
   446  	ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) {
   447  		reqNum++
   448  		fmt.Fprintf(res, "req=%d", reqNum)
   449  	}))
   450  	ts.Config.ReadTimeout = 250 * time.Millisecond
   451  	ts.Config.WriteTimeout = 250 * time.Millisecond
   452  	ts.Start()
   453  	defer ts.Close()
   454  
   455  	// Hit the HTTP server successfully.
   456  	tr := &Transport{DisableKeepAlives: true} // they interfere with this test
   457  	defer tr.CloseIdleConnections()
   458  	c := &Client{Transport: tr}
   459  	r, err := c.Get(ts.URL)
   460  	if err != nil {
   461  		t.Fatalf("http Get #1: %v", err)
   462  	}
   463  	got, _ := ioutil.ReadAll(r.Body)
   464  	expected := "req=1"
   465  	if string(got) != expected {
   466  		t.Errorf("Unexpected response for request #1; got %q; expected %q",
   467  			string(got), expected)
   468  	}
   469  
   470  	// Slow client that should timeout.
   471  	t1 := time.Now()
   472  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   473  	if err != nil {
   474  		t.Fatalf("Dial: %v", err)
   475  	}
   476  	buf := make([]byte, 1)
   477  	n, err := conn.Read(buf)
   478  	latency := time.Since(t1)
   479  	if n != 0 || err != io.EOF {
   480  		t.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, io.EOF)
   481  	}
   482  	if latency < 200*time.Millisecond /* fudge from 250 ms above */ {
   483  		t.Errorf("got EOF after %s, want >= %s", latency, 200*time.Millisecond)
   484  	}
   485  
   486  	// Hit the HTTP server successfully again, verifying that the
   487  	// previous slow connection didn't run our handler.  (that we
   488  	// get "req=2", not "req=3")
   489  	r, err = Get(ts.URL)
   490  	if err != nil {
   491  		t.Fatalf("http Get #2: %v", err)
   492  	}
   493  	got, _ = ioutil.ReadAll(r.Body)
   494  	expected = "req=2"
   495  	if string(got) != expected {
   496  		t.Errorf("Get #2 got %q, want %q", string(got), expected)
   497  	}
   498  
   499  	if !testing.Short() {
   500  		conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   501  		if err != nil {
   502  			t.Fatalf("Dial: %v", err)
   503  		}
   504  		defer conn.Close()
   505  		go io.Copy(ioutil.Discard, conn)
   506  		for i := 0; i < 5; i++ {
   507  			_, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: foo\r\n\r\n"))
   508  			if err != nil {
   509  				t.Fatalf("on write %d: %v", i, err)
   510  			}
   511  			time.Sleep(ts.Config.ReadTimeout / 2)
   512  		}
   513  	}
   514  }
   515  
   516  // golang.org/issue/4741 -- setting only a write timeout that triggers
   517  // shouldn't cause a handler to block forever on reads (next HTTP
   518  // request) that will never happen.
   519  func TestOnlyWriteTimeout(t *testing.T) {
   520  	defer afterTest(t)
   521  	var conn net.Conn
   522  	var afterTimeoutErrc = make(chan error, 1)
   523  	ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, req *Request) {
   524  		buf := make([]byte, 512<<10)
   525  		_, err := w.Write(buf)
   526  		if err != nil {
   527  			t.Errorf("handler Write error: %v", err)
   528  			return
   529  		}
   530  		conn.SetWriteDeadline(time.Now().Add(-30 * time.Second))
   531  		_, err = w.Write(buf)
   532  		afterTimeoutErrc <- err
   533  	}))
   534  	ts.Listener = trackLastConnListener{ts.Listener, &conn}
   535  	ts.Start()
   536  	defer ts.Close()
   537  
   538  	tr := &Transport{DisableKeepAlives: false}
   539  	defer tr.CloseIdleConnections()
   540  	c := &Client{Transport: tr}
   541  
   542  	errc := make(chan error)
   543  	go func() {
   544  		res, err := c.Get(ts.URL)
   545  		if err != nil {
   546  			errc <- err
   547  			return
   548  		}
   549  		_, err = io.Copy(ioutil.Discard, res.Body)
   550  		errc <- err
   551  	}()
   552  	select {
   553  	case err := <-errc:
   554  		if err == nil {
   555  			t.Errorf("expected an error from Get request")
   556  		}
   557  	case <-time.After(5 * time.Second):
   558  		t.Fatal("timeout waiting for Get error")
   559  	}
   560  	if err := <-afterTimeoutErrc; err == nil {
   561  		t.Error("expected write error after timeout")
   562  	}
   563  }
   564  
   565  // trackLastConnListener tracks the last net.Conn that was accepted.
   566  type trackLastConnListener struct {
   567  	net.Listener
   568  	last *net.Conn // destination
   569  }
   570  
   571  func (l trackLastConnListener) Accept() (c net.Conn, err error) {
   572  	c, err = l.Listener.Accept()
   573  	*l.last = c
   574  	return
   575  }
   576  
   577  // TestIdentityResponse verifies that a handler can unset
   578  func TestIdentityResponse(t *testing.T) {
   579  	defer afterTest(t)
   580  	handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
   581  		rw.Header().Set("Content-Length", "3")
   582  		rw.Header().Set("Transfer-Encoding", req.FormValue("te"))
   583  		switch {
   584  		case req.FormValue("overwrite") == "1":
   585  			_, err := rw.Write([]byte("foo TOO LONG"))
   586  			if err != ErrContentLength {
   587  				t.Errorf("expected ErrContentLength; got %v", err)
   588  			}
   589  		case req.FormValue("underwrite") == "1":
   590  			rw.Header().Set("Content-Length", "500")
   591  			rw.Write([]byte("too short"))
   592  		default:
   593  			rw.Write([]byte("foo"))
   594  		}
   595  	})
   596  
   597  	ts := httptest.NewServer(handler)
   598  	defer ts.Close()
   599  
   600  	// Note: this relies on the assumption (which is true) that
   601  	// Get sends HTTP/1.1 or greater requests.  Otherwise the
   602  	// server wouldn't have the choice to send back chunked
   603  	// responses.
   604  	for _, te := range []string{"", "identity"} {
   605  		url := ts.URL + "/?te=" + te
   606  		res, err := Get(url)
   607  		if err != nil {
   608  			t.Fatalf("error with Get of %s: %v", url, err)
   609  		}
   610  		if cl, expected := res.ContentLength, int64(3); cl != expected {
   611  			t.Errorf("for %s expected res.ContentLength of %d; got %d", url, expected, cl)
   612  		}
   613  		if cl, expected := res.Header.Get("Content-Length"), "3"; cl != expected {
   614  			t.Errorf("for %s expected Content-Length header of %q; got %q", url, expected, cl)
   615  		}
   616  		if tl, expected := len(res.TransferEncoding), 0; tl != expected {
   617  			t.Errorf("for %s expected len(res.TransferEncoding) of %d; got %d (%v)",
   618  				url, expected, tl, res.TransferEncoding)
   619  		}
   620  		res.Body.Close()
   621  	}
   622  
   623  	// Verify that ErrContentLength is returned
   624  	url := ts.URL + "/?overwrite=1"
   625  	res, err := Get(url)
   626  	if err != nil {
   627  		t.Fatalf("error with Get of %s: %v", url, err)
   628  	}
   629  	res.Body.Close()
   630  
   631  	// Verify that the connection is closed when the declared Content-Length
   632  	// is larger than what the handler wrote.
   633  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   634  	if err != nil {
   635  		t.Fatalf("error dialing: %v", err)
   636  	}
   637  	_, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n\r\n"))
   638  	if err != nil {
   639  		t.Fatalf("error writing: %v", err)
   640  	}
   641  
   642  	// The ReadAll will hang for a failing test, so use a Timer to
   643  	// fail explicitly.
   644  	goTimeout(t, 2*time.Second, func() {
   645  		got, _ := ioutil.ReadAll(conn)
   646  		expectedSuffix := "\r\n\r\ntoo short"
   647  		if !strings.HasSuffix(string(got), expectedSuffix) {
   648  			t.Errorf("Expected output to end with %q; got response body %q",
   649  				expectedSuffix, string(got))
   650  		}
   651  	})
   652  }
   653  
   654  func testTCPConnectionCloses(t *testing.T, req string, h Handler) {
   655  	defer afterTest(t)
   656  	s := httptest.NewServer(h)
   657  	defer s.Close()
   658  
   659  	conn, err := net.Dial("tcp", s.Listener.Addr().String())
   660  	if err != nil {
   661  		t.Fatal("dial error:", err)
   662  	}
   663  	defer conn.Close()
   664  
   665  	_, err = fmt.Fprint(conn, req)
   666  	if err != nil {
   667  		t.Fatal("print error:", err)
   668  	}
   669  
   670  	r := bufio.NewReader(conn)
   671  	res, err := ReadResponse(r, &Request{Method: "GET"})
   672  	if err != nil {
   673  		t.Fatal("ReadResponse error:", err)
   674  	}
   675  
   676  	didReadAll := make(chan bool, 1)
   677  	go func() {
   678  		select {
   679  		case <-time.After(5 * time.Second):
   680  			t.Error("body not closed after 5s")
   681  			return
   682  		case <-didReadAll:
   683  		}
   684  	}()
   685  
   686  	_, err = ioutil.ReadAll(r)
   687  	if err != nil {
   688  		t.Fatal("read error:", err)
   689  	}
   690  	didReadAll <- true
   691  
   692  	if !res.Close {
   693  		t.Errorf("Response.Close = false; want true")
   694  	}
   695  }
   696  
   697  // TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive.
   698  func TestServeHTTP10Close(t *testing.T) {
   699  	testTCPConnectionCloses(t, "GET / HTTP/1.0\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
   700  		ServeFile(w, r, "testdata/file")
   701  	}))
   702  }
   703  
   704  // TestClientCanClose verifies that clients can also force a connection to close.
   705  func TestClientCanClose(t *testing.T) {
   706  	testTCPConnectionCloses(t, "GET / HTTP/1.1\r\nConnection: close\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
   707  		// Nothing.
   708  	}))
   709  }
   710  
   711  // TestHandlersCanSetConnectionClose verifies that handlers can force a connection to close,
   712  // even for HTTP/1.1 requests.
   713  func TestHandlersCanSetConnectionClose11(t *testing.T) {
   714  	testTCPConnectionCloses(t, "GET / HTTP/1.1\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
   715  		w.Header().Set("Connection", "close")
   716  	}))
   717  }
   718  
   719  func TestHandlersCanSetConnectionClose10(t *testing.T) {
   720  	testTCPConnectionCloses(t, "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
   721  		w.Header().Set("Connection", "close")
   722  	}))
   723  }
   724  
   725  func TestSetsRemoteAddr(t *testing.T) {
   726  	defer afterTest(t)
   727  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   728  		fmt.Fprintf(w, "%s", r.RemoteAddr)
   729  	}))
   730  	defer ts.Close()
   731  
   732  	res, err := Get(ts.URL)
   733  	if err != nil {
   734  		t.Fatalf("Get error: %v", err)
   735  	}
   736  	body, err := ioutil.ReadAll(res.Body)
   737  	if err != nil {
   738  		t.Fatalf("ReadAll error: %v", err)
   739  	}
   740  	ip := string(body)
   741  	if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") {
   742  		t.Fatalf("Expected local addr; got %q", ip)
   743  	}
   744  }
   745  
   746  func TestChunkedResponseHeaders(t *testing.T) {
   747  	defer afterTest(t)
   748  	log.SetOutput(ioutil.Discard) // is noisy otherwise
   749  	defer log.SetOutput(os.Stderr)
   750  
   751  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   752  		w.Header().Set("Content-Length", "intentional gibberish") // we check that this is deleted
   753  		w.(Flusher).Flush()
   754  		fmt.Fprintf(w, "I am a chunked response.")
   755  	}))
   756  	defer ts.Close()
   757  
   758  	res, err := Get(ts.URL)
   759  	if err != nil {
   760  		t.Fatalf("Get error: %v", err)
   761  	}
   762  	defer res.Body.Close()
   763  	if g, e := res.ContentLength, int64(-1); g != e {
   764  		t.Errorf("expected ContentLength of %d; got %d", e, g)
   765  	}
   766  	if g, e := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(g, e) {
   767  		t.Errorf("expected TransferEncoding of %v; got %v", e, g)
   768  	}
   769  	if _, haveCL := res.Header["Content-Length"]; haveCL {
   770  		t.Errorf("Unexpected Content-Length")
   771  	}
   772  }
   773  
   774  // Test304Responses verifies that 304s don't declare that they're
   775  // chunking in their response headers and aren't allowed to produce
   776  // output.
   777  func Test304Responses(t *testing.T) {
   778  	defer afterTest(t)
   779  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   780  		w.WriteHeader(StatusNotModified)
   781  		_, err := w.Write([]byte("illegal body"))
   782  		if err != ErrBodyNotAllowed {
   783  			t.Errorf("on Write, expected ErrBodyNotAllowed, got %v", err)
   784  		}
   785  	}))
   786  	defer ts.Close()
   787  	res, err := Get(ts.URL)
   788  	if err != nil {
   789  		t.Error(err)
   790  	}
   791  	if len(res.TransferEncoding) > 0 {
   792  		t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
   793  	}
   794  	body, err := ioutil.ReadAll(res.Body)
   795  	if err != nil {
   796  		t.Error(err)
   797  	}
   798  	if len(body) > 0 {
   799  		t.Errorf("got unexpected body %q", string(body))
   800  	}
   801  }
   802  
   803  // TestHeadResponses verifies that all MIME type sniffing and Content-Length
   804  // counting of GET requests also happens on HEAD requests.
   805  func TestHeadResponses(t *testing.T) {
   806  	defer afterTest(t)
   807  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   808  		_, err := w.Write([]byte("<html>"))
   809  		if err != nil {
   810  			t.Errorf("ResponseWriter.Write: %v", err)
   811  		}
   812  
   813  		// Also exercise the ReaderFrom path
   814  		_, err = io.Copy(w, strings.NewReader("789a"))
   815  		if err != nil {
   816  			t.Errorf("Copy(ResponseWriter, ...): %v", err)
   817  		}
   818  	}))
   819  	defer ts.Close()
   820  	res, err := Head(ts.URL)
   821  	if err != nil {
   822  		t.Error(err)
   823  	}
   824  	if len(res.TransferEncoding) > 0 {
   825  		t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
   826  	}
   827  	if ct := res.Header.Get("Content-Type"); ct != "text/html; charset=utf-8" {
   828  		t.Errorf("Content-Type: %q; want text/html; charset=utf-8", ct)
   829  	}
   830  	if v := res.ContentLength; v != 10 {
   831  		t.Errorf("Content-Length: %d; want 10", v)
   832  	}
   833  	body, err := ioutil.ReadAll(res.Body)
   834  	if err != nil {
   835  		t.Error(err)
   836  	}
   837  	if len(body) > 0 {
   838  		t.Errorf("got unexpected body %q", string(body))
   839  	}
   840  }
   841  
   842  func TestTLSHandshakeTimeout(t *testing.T) {
   843  	defer afterTest(t)
   844  	ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
   845  	ts.Config.ReadTimeout = 250 * time.Millisecond
   846  	ts.StartTLS()
   847  	defer ts.Close()
   848  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   849  	if err != nil {
   850  		t.Fatalf("Dial: %v", err)
   851  	}
   852  	defer conn.Close()
   853  	goTimeout(t, 10*time.Second, func() {
   854  		var buf [1]byte
   855  		n, err := conn.Read(buf[:])
   856  		if err == nil || n != 0 {
   857  			t.Errorf("Read = %d, %v; want an error and no bytes", n, err)
   858  		}
   859  	})
   860  }
   861  
   862  func TestTLSServer(t *testing.T) {
   863  	defer afterTest(t)
   864  	ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   865  		if r.TLS != nil {
   866  			w.Header().Set("X-TLS-Set", "true")
   867  			if r.TLS.HandshakeComplete {
   868  				w.Header().Set("X-TLS-HandshakeComplete", "true")
   869  			}
   870  		}
   871  	}))
   872  	defer ts.Close()
   873  
   874  	// Connect an idle TCP connection to this server before we run
   875  	// our real tests.  This idle connection used to block forever
   876  	// in the TLS handshake, preventing future connections from
   877  	// being accepted. It may prevent future accidental blocking
   878  	// in newConn.
   879  	idleConn, err := net.Dial("tcp", ts.Listener.Addr().String())
   880  	if err != nil {
   881  		t.Fatalf("Dial: %v", err)
   882  	}
   883  	defer idleConn.Close()
   884  	goTimeout(t, 10*time.Second, func() {
   885  		if !strings.HasPrefix(ts.URL, "https://") {
   886  			t.Errorf("expected test TLS server to start with https://, got %q", ts.URL)
   887  			return
   888  		}
   889  		noVerifyTransport := &Transport{
   890  			TLSClientConfig: &tls.Config{
   891  				InsecureSkipVerify: true,
   892  			},
   893  		}
   894  		client := &Client{Transport: noVerifyTransport}
   895  		res, err := client.Get(ts.URL)
   896  		if err != nil {
   897  			t.Error(err)
   898  			return
   899  		}
   900  		if res == nil {
   901  			t.Errorf("got nil Response")
   902  			return
   903  		}
   904  		defer res.Body.Close()
   905  		if res.Header.Get("X-TLS-Set") != "true" {
   906  			t.Errorf("expected X-TLS-Set response header")
   907  			return
   908  		}
   909  		if res.Header.Get("X-TLS-HandshakeComplete") != "true" {
   910  			t.Errorf("expected X-TLS-HandshakeComplete header")
   911  		}
   912  	})
   913  }
   914  
   915  type serverExpectTest struct {
   916  	contentLength    int    // of request body
   917  	expectation      string // e.g. "100-continue"
   918  	readBody         bool   // whether handler should read the body (if false, sends StatusUnauthorized)
   919  	expectedResponse string // expected substring in first line of http response
   920  }
   921  
   922  var serverExpectTests = []serverExpectTest{
   923  	// Normal 100-continues, case-insensitive.
   924  	{100, "100-continue", true, "100 Continue"},
   925  	{100, "100-cOntInUE", true, "100 Continue"},
   926  
   927  	// No 100-continue.
   928  	{100, "", true, "200 OK"},
   929  
   930  	// 100-continue but requesting client to deny us,
   931  	// so it never reads the body.
   932  	{100, "100-continue", false, "401 Unauthorized"},
   933  	// Likewise without 100-continue:
   934  	{100, "", false, "401 Unauthorized"},
   935  
   936  	// Non-standard expectations are failures
   937  	{0, "a-pony", false, "417 Expectation Failed"},
   938  
   939  	// Expect-100 requested but no body
   940  	{0, "100-continue", true, "400 Bad Request"},
   941  }
   942  
   943  // Tests that the server responds to the "Expect" request header
   944  // correctly.
   945  func TestServerExpect(t *testing.T) {
   946  	defer afterTest(t)
   947  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   948  		// Note using r.FormValue("readbody") because for POST
   949  		// requests that would read from r.Body, which we only
   950  		// conditionally want to do.
   951  		if strings.Contains(r.URL.RawQuery, "readbody=true") {
   952  			ioutil.ReadAll(r.Body)
   953  			w.Write([]byte("Hi"))
   954  		} else {
   955  			w.WriteHeader(StatusUnauthorized)
   956  		}
   957  	}))
   958  	defer ts.Close()
   959  
   960  	runTest := func(test serverExpectTest) {
   961  		conn, err := net.Dial("tcp", ts.Listener.Addr().String())
   962  		if err != nil {
   963  			t.Fatalf("Dial: %v", err)
   964  		}
   965  		defer conn.Close()
   966  
   967  		// Only send the body immediately if we're acting like an HTTP client
   968  		// that doesn't send 100-continue expectations.
   969  		writeBody := test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue"
   970  
   971  		go func() {
   972  			_, err := fmt.Fprintf(conn, "POST /?readbody=%v HTTP/1.1\r\n"+
   973  				"Connection: close\r\n"+
   974  				"Content-Length: %d\r\n"+
   975  				"Expect: %s\r\nHost: foo\r\n\r\n",
   976  				test.readBody, test.contentLength, test.expectation)
   977  			if err != nil {
   978  				t.Errorf("On test %#v, error writing request headers: %v", test, err)
   979  				return
   980  			}
   981  			if writeBody {
   982  				body := strings.Repeat("A", test.contentLength)
   983  				_, err = fmt.Fprint(conn, body)
   984  				if err != nil {
   985  					if !test.readBody {
   986  						// Server likely already hung up on us.
   987  						// See larger comment below.
   988  						t.Logf("On test %#v, acceptable error writing request body: %v", test, err)
   989  						return
   990  					}
   991  					t.Errorf("On test %#v, error writing request body: %v", test, err)
   992  				}
   993  			}
   994  		}()
   995  		bufr := bufio.NewReader(conn)
   996  		line, err := bufr.ReadString('\n')
   997  		if err != nil {
   998  			if writeBody && !test.readBody {
   999  				// This is an acceptable failure due to a possible TCP race:
  1000  				// We were still writing data and the server hung up on us. A TCP
  1001  				// implementation may send a RST if our request body data was known
  1002  				// to be lost, which may trigger our reads to fail.
  1003  				// See RFC 1122 page 88.
  1004  				t.Logf("On test %#v, acceptable error from ReadString: %v", test, err)
  1005  				return
  1006  			}
  1007  			t.Fatalf("On test %#v, ReadString: %v", test, err)
  1008  		}
  1009  		if !strings.Contains(line, test.expectedResponse) {
  1010  			t.Errorf("On test %#v, got first line = %q; want %q", test, line, test.expectedResponse)
  1011  		}
  1012  	}
  1013  
  1014  	for _, test := range serverExpectTests {
  1015  		runTest(test)
  1016  	}
  1017  }
  1018  
  1019  // Under a ~256KB (maxPostHandlerReadBytes) threshold, the server
  1020  // should consume client request bodies that a handler didn't read.
  1021  func TestServerUnreadRequestBodyLittle(t *testing.T) {
  1022  	conn := new(testConn)
  1023  	body := strings.Repeat("x", 100<<10)
  1024  	conn.readBuf.Write([]byte(fmt.Sprintf(
  1025  		"POST / HTTP/1.1\r\n"+
  1026  			"Host: test\r\n"+
  1027  			"Content-Length: %d\r\n"+
  1028  			"\r\n", len(body))))
  1029  	conn.readBuf.Write([]byte(body))
  1030  
  1031  	done := make(chan bool)
  1032  
  1033  	ls := &oneConnListener{conn}
  1034  	go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
  1035  		defer close(done)
  1036  		if conn.readBuf.Len() < len(body)/2 {
  1037  			t.Errorf("on request, read buffer length is %d; expected about 100 KB", conn.readBuf.Len())
  1038  		}
  1039  		rw.WriteHeader(200)
  1040  		rw.(Flusher).Flush()
  1041  		if g, e := conn.readBuf.Len(), 0; g != e {
  1042  			t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e)
  1043  		}
  1044  		if c := rw.Header().Get("Connection"); c != "" {
  1045  			t.Errorf(`Connection header = %q; want ""`, c)
  1046  		}
  1047  	}))
  1048  	<-done
  1049  }
  1050  
  1051  // Over a ~256KB (maxPostHandlerReadBytes) threshold, the server
  1052  // should ignore client request bodies that a handler didn't read
  1053  // and close the connection.
  1054  func TestServerUnreadRequestBodyLarge(t *testing.T) {
  1055  	conn := new(testConn)
  1056  	body := strings.Repeat("x", 1<<20)
  1057  	conn.readBuf.Write([]byte(fmt.Sprintf(
  1058  		"POST / HTTP/1.1\r\n"+
  1059  			"Host: test\r\n"+
  1060  			"Content-Length: %d\r\n"+
  1061  			"\r\n", len(body))))
  1062  	conn.readBuf.Write([]byte(body))
  1063  	conn.closec = make(chan bool, 1)
  1064  
  1065  	ls := &oneConnListener{conn}
  1066  	go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
  1067  		if conn.readBuf.Len() < len(body)/2 {
  1068  			t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len())
  1069  		}
  1070  		rw.WriteHeader(200)
  1071  		rw.(Flusher).Flush()
  1072  		if conn.readBuf.Len() < len(body)/2 {
  1073  			t.Errorf("post-WriteHeader, read buffer length is %d; expected about 1MB", conn.readBuf.Len())
  1074  		}
  1075  	}))
  1076  	<-conn.closec
  1077  
  1078  	if res := conn.writeBuf.String(); !strings.Contains(res, "Connection: close") {
  1079  		t.Errorf("Expected a Connection: close header; got response: %s", res)
  1080  	}
  1081  }
  1082  
  1083  func TestTimeoutHandler(t *testing.T) {
  1084  	defer afterTest(t)
  1085  	sendHi := make(chan bool, 1)
  1086  	writeErrors := make(chan error, 1)
  1087  	sayHi := HandlerFunc(func(w ResponseWriter, r *Request) {
  1088  		<-sendHi
  1089  		_, werr := w.Write([]byte("hi"))
  1090  		writeErrors <- werr
  1091  	})
  1092  	timeout := make(chan time.Time, 1) // write to this to force timeouts
  1093  	ts := httptest.NewServer(NewTestTimeoutHandler(sayHi, timeout))
  1094  	defer ts.Close()
  1095  
  1096  	// Succeed without timing out:
  1097  	sendHi <- true
  1098  	res, err := Get(ts.URL)
  1099  	if err != nil {
  1100  		t.Error(err)
  1101  	}
  1102  	if g, e := res.StatusCode, StatusOK; g != e {
  1103  		t.Errorf("got res.StatusCode %d; expected %d", g, e)
  1104  	}
  1105  	body, _ := ioutil.ReadAll(res.Body)
  1106  	if g, e := string(body), "hi"; g != e {
  1107  		t.Errorf("got body %q; expected %q", g, e)
  1108  	}
  1109  	if g := <-writeErrors; g != nil {
  1110  		t.Errorf("got unexpected Write error on first request: %v", g)
  1111  	}
  1112  
  1113  	// Times out:
  1114  	timeout <- time.Time{}
  1115  	res, err = Get(ts.URL)
  1116  	if err != nil {
  1117  		t.Error(err)
  1118  	}
  1119  	if g, e := res.StatusCode, StatusServiceUnavailable; g != e {
  1120  		t.Errorf("got res.StatusCode %d; expected %d", g, e)
  1121  	}
  1122  	body, _ = ioutil.ReadAll(res.Body)
  1123  	if !strings.Contains(string(body), "<title>Timeout</title>") {
  1124  		t.Errorf("expected timeout body; got %q", string(body))
  1125  	}
  1126  
  1127  	// Now make the previously-timed out handler speak again,
  1128  	// which verifies the panic is handled:
  1129  	sendHi <- true
  1130  	if g, e := <-writeErrors, ErrHandlerTimeout; g != e {
  1131  		t.Errorf("expected Write error of %v; got %v", e, g)
  1132  	}
  1133  }
  1134  
  1135  // Verifies we don't path.Clean() on the wrong parts in redirects.
  1136  func TestRedirectMunging(t *testing.T) {
  1137  	req, _ := NewRequest("GET", "http://example.com/", nil)
  1138  
  1139  	resp := httptest.NewRecorder()
  1140  	Redirect(resp, req, "/foo?next=http://bar.com/", 302)
  1141  	if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e {
  1142  		t.Errorf("Location header was %q; want %q", g, e)
  1143  	}
  1144  
  1145  	resp = httptest.NewRecorder()
  1146  	Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302)
  1147  	if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e {
  1148  		t.Errorf("Location header was %q; want %q", g, e)
  1149  	}
  1150  }
  1151  
  1152  func TestRedirectBadPath(t *testing.T) {
  1153  	// This used to crash. It's not valid input (bad path), but it
  1154  	// shouldn't crash.
  1155  	rr := httptest.NewRecorder()
  1156  	req := &Request{
  1157  		Method: "GET",
  1158  		URL: &url.URL{
  1159  			Scheme: "http",
  1160  			Path:   "not-empty-but-no-leading-slash", // bogus
  1161  		},
  1162  	}
  1163  	Redirect(rr, req, "", 304)
  1164  	if rr.Code != 304 {
  1165  		t.Errorf("Code = %d; want 304", rr.Code)
  1166  	}
  1167  }
  1168  
  1169  // TestZeroLengthPostAndResponse exercises an optimization done by the Transport:
  1170  // when there is no body (either because the method doesn't permit a body, or an
  1171  // explicit Content-Length of zero is present), then the transport can re-use the
  1172  // connection immediately. But when it re-uses the connection, it typically closes
  1173  // the previous request's body, which is not optimal for zero-lengthed bodies,
  1174  // as the client would then see http.ErrBodyReadAfterClose and not 0, io.EOF.
  1175  func TestZeroLengthPostAndResponse(t *testing.T) {
  1176  	defer afterTest(t)
  1177  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
  1178  		all, err := ioutil.ReadAll(r.Body)
  1179  		if err != nil {
  1180  			t.Fatalf("handler ReadAll: %v", err)
  1181  		}
  1182  		if len(all) != 0 {
  1183  			t.Errorf("handler got %d bytes; expected 0", len(all))
  1184  		}
  1185  		rw.Header().Set("Content-Length", "0")
  1186  	}))
  1187  	defer ts.Close()
  1188  
  1189  	req, err := NewRequest("POST", ts.URL, strings.NewReader(""))
  1190  	if err != nil {
  1191  		t.Fatal(err)
  1192  	}
  1193  	req.ContentLength = 0
  1194  
  1195  	var resp [5]*Response
  1196  	for i := range resp {
  1197  		resp[i], err = DefaultClient.Do(req)
  1198  		if err != nil {
  1199  			t.Fatalf("client post #%d: %v", i, err)
  1200  		}
  1201  	}
  1202  
  1203  	for i := range resp {
  1204  		all, err := ioutil.ReadAll(resp[i].Body)
  1205  		if err != nil {
  1206  			t.Fatalf("req #%d: client ReadAll: %v", i, err)
  1207  		}
  1208  		if len(all) != 0 {
  1209  			t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all))
  1210  		}
  1211  	}
  1212  }
  1213  
  1214  func TestHandlerPanicNil(t *testing.T) {
  1215  	testHandlerPanic(t, false, nil)
  1216  }
  1217  
  1218  func TestHandlerPanic(t *testing.T) {
  1219  	testHandlerPanic(t, false, "intentional death for testing")
  1220  }
  1221  
  1222  func TestHandlerPanicWithHijack(t *testing.T) {
  1223  	testHandlerPanic(t, true, "intentional death for testing")
  1224  }
  1225  
  1226  func testHandlerPanic(t *testing.T, withHijack bool, panicValue interface{}) {
  1227  	defer afterTest(t)
  1228  	// Unlike the other tests that set the log output to ioutil.Discard
  1229  	// to quiet the output, this test uses a pipe.  The pipe serves three
  1230  	// purposes:
  1231  	//
  1232  	//   1) The log.Print from the http server (generated by the caught
  1233  	//      panic) will go to the pipe instead of stderr, making the
  1234  	//      output quiet.
  1235  	//
  1236  	//   2) We read from the pipe to verify that the handler
  1237  	//      actually caught the panic and logged something.
  1238  	//
  1239  	//   3) The blocking Read call prevents this TestHandlerPanic
  1240  	//      function from exiting before the HTTP server handler
  1241  	//      finishes crashing. If this text function exited too
  1242  	//      early (and its defer log.SetOutput(os.Stderr) ran),
  1243  	//      then the crash output could spill into the next test.
  1244  	pr, pw := io.Pipe()
  1245  	log.SetOutput(pw)
  1246  	defer log.SetOutput(os.Stderr)
  1247  	defer pw.Close()
  1248  
  1249  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1250  		if withHijack {
  1251  			rwc, _, err := w.(Hijacker).Hijack()
  1252  			if err != nil {
  1253  				t.Logf("unexpected error: %v", err)
  1254  			}
  1255  			defer rwc.Close()
  1256  		}
  1257  		panic(panicValue)
  1258  	}))
  1259  	defer ts.Close()
  1260  
  1261  	// Do a blocking read on the log output pipe so its logging
  1262  	// doesn't bleed into the next test.  But wait only 5 seconds
  1263  	// for it.
  1264  	done := make(chan bool, 1)
  1265  	go func() {
  1266  		buf := make([]byte, 4<<10)
  1267  		_, err := pr.Read(buf)
  1268  		pr.Close()
  1269  		if err != nil && err != io.EOF {
  1270  			t.Error(err)
  1271  		}
  1272  		done <- true
  1273  	}()
  1274  
  1275  	_, err := Get(ts.URL)
  1276  	if err == nil {
  1277  		t.Logf("expected an error")
  1278  	}
  1279  
  1280  	if panicValue == nil {
  1281  		return
  1282  	}
  1283  
  1284  	select {
  1285  	case <-done:
  1286  		return
  1287  	case <-time.After(5 * time.Second):
  1288  		t.Fatal("expected server handler to log an error")
  1289  	}
  1290  }
  1291  
  1292  func TestNoDate(t *testing.T) {
  1293  	defer afterTest(t)
  1294  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1295  		w.Header()["Date"] = nil
  1296  	}))
  1297  	defer ts.Close()
  1298  	res, err := Get(ts.URL)
  1299  	if err != nil {
  1300  		t.Fatal(err)
  1301  	}
  1302  	_, present := res.Header["Date"]
  1303  	if present {
  1304  		t.Fatalf("Expected no Date header; got %v", res.Header["Date"])
  1305  	}
  1306  }
  1307  
  1308  func TestStripPrefix(t *testing.T) {
  1309  	defer afterTest(t)
  1310  	h := HandlerFunc(func(w ResponseWriter, r *Request) {
  1311  		w.Header().Set("X-Path", r.URL.Path)
  1312  	})
  1313  	ts := httptest.NewServer(StripPrefix("/foo", h))
  1314  	defer ts.Close()
  1315  
  1316  	res, err := Get(ts.URL + "/foo/bar")
  1317  	if err != nil {
  1318  		t.Fatal(err)
  1319  	}
  1320  	if g, e := res.Header.Get("X-Path"), "/bar"; g != e {
  1321  		t.Errorf("test 1: got %s, want %s", g, e)
  1322  	}
  1323  	res.Body.Close()
  1324  
  1325  	res, err = Get(ts.URL + "/bar")
  1326  	if err != nil {
  1327  		t.Fatal(err)
  1328  	}
  1329  	if g, e := res.StatusCode, 404; g != e {
  1330  		t.Errorf("test 2: got status %v, want %v", g, e)
  1331  	}
  1332  	res.Body.Close()
  1333  }
  1334  
  1335  func TestRequestLimit(t *testing.T) {
  1336  	defer afterTest(t)
  1337  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1338  		t.Fatalf("didn't expect to get request in Handler")
  1339  	}))
  1340  	defer ts.Close()
  1341  	req, _ := NewRequest("GET", ts.URL, nil)
  1342  	var bytesPerHeader = len("header12345: val12345\r\n")
  1343  	for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ {
  1344  		req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i))
  1345  	}
  1346  	res, err := DefaultClient.Do(req)
  1347  	if err != nil {
  1348  		// Some HTTP clients may fail on this undefined behavior (server replying and
  1349  		// closing the connection while the request is still being written), but
  1350  		// we do support it (at least currently), so we expect a response below.
  1351  		t.Fatalf("Do: %v", err)
  1352  	}
  1353  	defer res.Body.Close()
  1354  	if res.StatusCode != 413 {
  1355  		t.Fatalf("expected 413 response status; got: %d %s", res.StatusCode, res.Status)
  1356  	}
  1357  }
  1358  
  1359  type neverEnding byte
  1360  
  1361  func (b neverEnding) Read(p []byte) (n int, err error) {
  1362  	for i := range p {
  1363  		p[i] = byte(b)
  1364  	}
  1365  	return len(p), nil
  1366  }
  1367  
  1368  type countReader struct {
  1369  	r io.Reader
  1370  	n *int64
  1371  }
  1372  
  1373  func (cr countReader) Read(p []byte) (n int, err error) {
  1374  	n, err = cr.r.Read(p)
  1375  	atomic.AddInt64(cr.n, int64(n))
  1376  	return
  1377  }
  1378  
  1379  func TestRequestBodyLimit(t *testing.T) {
  1380  	defer afterTest(t)
  1381  	const limit = 1 << 20
  1382  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1383  		r.Body = MaxBytesReader(w, r.Body, limit)
  1384  		n, err := io.Copy(ioutil.Discard, r.Body)
  1385  		if err == nil {
  1386  			t.Errorf("expected error from io.Copy")
  1387  		}
  1388  		if n != limit {
  1389  			t.Errorf("io.Copy = %d, want %d", n, limit)
  1390  		}
  1391  	}))
  1392  	defer ts.Close()
  1393  
  1394  	nWritten := new(int64)
  1395  	req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200))
  1396  
  1397  	// Send the POST, but don't care it succeeds or not.  The
  1398  	// remote side is going to reply and then close the TCP
  1399  	// connection, and HTTP doesn't really define if that's
  1400  	// allowed or not.  Some HTTP clients will get the response
  1401  	// and some (like ours, currently) will complain that the
  1402  	// request write failed, without reading the response.
  1403  	//
  1404  	// But that's okay, since what we're really testing is that
  1405  	// the remote side hung up on us before we wrote too much.
  1406  	_, _ = DefaultClient.Do(req)
  1407  
  1408  	if atomic.LoadInt64(nWritten) > limit*100 {
  1409  		t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d",
  1410  			limit, nWritten)
  1411  	}
  1412  }
  1413  
  1414  // TestClientWriteShutdown tests that if the client shuts down the write
  1415  // side of their TCP connection, the server doesn't send a 400 Bad Request.
  1416  func TestClientWriteShutdown(t *testing.T) {
  1417  	defer afterTest(t)
  1418  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
  1419  	defer ts.Close()
  1420  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1421  	if err != nil {
  1422  		t.Fatalf("Dial: %v", err)
  1423  	}
  1424  	err = conn.(*net.TCPConn).CloseWrite()
  1425  	if err != nil {
  1426  		t.Fatalf("Dial: %v", err)
  1427  	}
  1428  	donec := make(chan bool)
  1429  	go func() {
  1430  		defer close(donec)
  1431  		bs, err := ioutil.ReadAll(conn)
  1432  		if err != nil {
  1433  			t.Fatalf("ReadAll: %v", err)
  1434  		}
  1435  		got := string(bs)
  1436  		if got != "" {
  1437  			t.Errorf("read %q from server; want nothing", got)
  1438  		}
  1439  	}()
  1440  	select {
  1441  	case <-donec:
  1442  	case <-time.After(10 * time.Second):
  1443  		t.Fatalf("timeout")
  1444  	}
  1445  }
  1446  
  1447  // Tests that chunked server responses that write 1 byte at a time are
  1448  // buffered before chunk headers are added, not after chunk headers.
  1449  func TestServerBufferedChunking(t *testing.T) {
  1450  	conn := new(testConn)
  1451  	conn.readBuf.Write([]byte("GET / HTTP/1.1\r\n\r\n"))
  1452  	conn.closec = make(chan bool, 1)
  1453  	ls := &oneConnListener{conn}
  1454  	go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
  1455  		rw.(Flusher).Flush() // force the Header to be sent, in chunking mode, not counting the length
  1456  		rw.Write([]byte{'x'})
  1457  		rw.Write([]byte{'y'})
  1458  		rw.Write([]byte{'z'})
  1459  	}))
  1460  	<-conn.closec
  1461  	if !bytes.HasSuffix(conn.writeBuf.Bytes(), []byte("\r\n\r\n3\r\nxyz\r\n0\r\n\r\n")) {
  1462  		t.Errorf("response didn't end with a single 3 byte 'xyz' chunk; got:\n%q",
  1463  			conn.writeBuf.Bytes())
  1464  	}
  1465  }
  1466  
  1467  // Tests that the server flushes its response headers out when it's
  1468  // ignoring the response body and waits a bit before forcefully
  1469  // closing the TCP connection, causing the client to get a RST.
  1470  // See http://golang.org/issue/3595
  1471  func TestServerGracefulClose(t *testing.T) {
  1472  	defer afterTest(t)
  1473  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1474  		Error(w, "bye", StatusUnauthorized)
  1475  	}))
  1476  	defer ts.Close()
  1477  
  1478  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1479  	if err != nil {
  1480  		t.Fatal(err)
  1481  	}
  1482  	defer conn.Close()
  1483  	const bodySize = 5 << 20
  1484  	req := []byte(fmt.Sprintf("POST / HTTP/1.1\r\nHost: foo.com\r\nContent-Length: %d\r\n\r\n", bodySize))
  1485  	for i := 0; i < bodySize; i++ {
  1486  		req = append(req, 'x')
  1487  	}
  1488  	writeErr := make(chan error)
  1489  	go func() {
  1490  		_, err := conn.Write(req)
  1491  		writeErr <- err
  1492  	}()
  1493  	br := bufio.NewReader(conn)
  1494  	lineNum := 0
  1495  	for {
  1496  		line, err := br.ReadString('\n')
  1497  		if err == io.EOF {
  1498  			break
  1499  		}
  1500  		if err != nil {
  1501  			t.Fatalf("ReadLine: %v", err)
  1502  		}
  1503  		lineNum++
  1504  		if lineNum == 1 && !strings.Contains(line, "401 Unauthorized") {
  1505  			t.Errorf("Response line = %q; want a 401", line)
  1506  		}
  1507  	}
  1508  	// Wait for write to finish. This is a broken pipe on both
  1509  	// Darwin and Linux, but checking this isn't the point of
  1510  	// the test.
  1511  	<-writeErr
  1512  }
  1513  
  1514  func TestCaseSensitiveMethod(t *testing.T) {
  1515  	defer afterTest(t)
  1516  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
  1517  		if r.Method != "get" {
  1518  			t.Errorf(`Got method %q; want "get"`, r.Method)
  1519  		}
  1520  	}))
  1521  	defer ts.Close()
  1522  	req, _ := NewRequest("get", ts.URL, nil)
  1523  	res, err := DefaultClient.Do(req)
  1524  	if err != nil {
  1525  		t.Error(err)
  1526  		return
  1527  	}
  1528  	res.Body.Close()
  1529  }
  1530  
  1531  // TestContentLengthZero tests that for both an HTTP/1.0 and HTTP/1.1
  1532  // request (both keep-alive), when a Handler never writes any
  1533  // response, the net/http package adds a "Content-Length: 0" response
  1534  // header.
  1535  func TestContentLengthZero(t *testing.T) {
  1536  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {}))
  1537  	defer ts.Close()
  1538  
  1539  	for _, version := range []string{"HTTP/1.0", "HTTP/1.1"} {
  1540  		conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1541  		if err != nil {
  1542  			t.Fatalf("error dialing: %v", err)
  1543  		}
  1544  		_, err = fmt.Fprintf(conn, "GET / %v\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n", version)
  1545  		if err != nil {
  1546  			t.Fatalf("error writing: %v", err)
  1547  		}
  1548  		req, _ := NewRequest("GET", "/", nil)
  1549  		res, err := ReadResponse(bufio.NewReader(conn), req)
  1550  		if err != nil {
  1551  			t.Fatalf("error reading response: %v", err)
  1552  		}
  1553  		if te := res.TransferEncoding; len(te) > 0 {
  1554  			t.Errorf("For version %q, Transfer-Encoding = %q; want none", version, te)
  1555  		}
  1556  		if cl := res.ContentLength; cl != 0 {
  1557  			t.Errorf("For version %q, Content-Length = %v; want 0", version, cl)
  1558  		}
  1559  		conn.Close()
  1560  	}
  1561  }
  1562  
  1563  func TestCloseNotifier(t *testing.T) {
  1564  	defer afterTest(t)
  1565  	gotReq := make(chan bool, 1)
  1566  	sawClose := make(chan bool, 1)
  1567  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
  1568  		gotReq <- true
  1569  		cc := rw.(CloseNotifier).CloseNotify()
  1570  		<-cc
  1571  		sawClose <- true
  1572  	}))
  1573  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1574  	if err != nil {
  1575  		t.Fatalf("error dialing: %v", err)
  1576  	}
  1577  	diec := make(chan bool)
  1578  	go func() {
  1579  		_, err = fmt.Fprintf(conn, "GET / HTTP/1.1\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n")
  1580  		if err != nil {
  1581  			t.Fatal(err)
  1582  		}
  1583  		<-diec
  1584  		conn.Close()
  1585  	}()
  1586  For:
  1587  	for {
  1588  		select {
  1589  		case <-gotReq:
  1590  			diec <- true
  1591  		case <-sawClose:
  1592  			break For
  1593  		case <-time.After(5 * time.Second):
  1594  			t.Fatal("timeout")
  1595  		}
  1596  	}
  1597  	ts.Close()
  1598  }
  1599  
  1600  func TestCloseNotifierChanLeak(t *testing.T) {
  1601  	defer afterTest(t)
  1602  	req := reqBytes("GET / HTTP/1.0\nHost: golang.org")
  1603  	for i := 0; i < 20; i++ {
  1604  		var output bytes.Buffer
  1605  		conn := &rwTestConn{
  1606  			Reader: bytes.NewReader(req),
  1607  			Writer: &output,
  1608  			closec: make(chan bool, 1),
  1609  		}
  1610  		ln := &oneConnListener{conn: conn}
  1611  		handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  1612  			// Ignore the return value and never read from
  1613  			// it, testing that we don't leak goroutines
  1614  			// on the sending side:
  1615  			_ = rw.(CloseNotifier).CloseNotify()
  1616  		})
  1617  		go Serve(ln, handler)
  1618  		<-conn.closec
  1619  	}
  1620  }
  1621  
  1622  func TestOptions(t *testing.T) {
  1623  	uric := make(chan string, 2) // only expect 1, but leave space for 2
  1624  	mux := NewServeMux()
  1625  	mux.HandleFunc("/", func(w ResponseWriter, r *Request) {
  1626  		uric <- r.RequestURI
  1627  	})
  1628  	ts := httptest.NewServer(mux)
  1629  	defer ts.Close()
  1630  
  1631  	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1632  	if err != nil {
  1633  		t.Fatal(err)
  1634  	}
  1635  	defer conn.Close()
  1636  
  1637  	// An OPTIONS * request should succeed.
  1638  	_, err = conn.Write([]byte("OPTIONS * HTTP/1.1\r\nHost: foo.com\r\n\r\n"))
  1639  	if err != nil {
  1640  		t.Fatal(err)
  1641  	}
  1642  	br := bufio.NewReader(conn)
  1643  	res, err := ReadResponse(br, &Request{Method: "OPTIONS"})
  1644  	if err != nil {
  1645  		t.Fatal(err)
  1646  	}
  1647  	if res.StatusCode != 200 {
  1648  		t.Errorf("Got non-200 response to OPTIONS *: %#v", res)
  1649  	}
  1650  
  1651  	// A GET * request on a ServeMux should fail.
  1652  	_, err = conn.Write([]byte("GET * HTTP/1.1\r\nHost: foo.com\r\n\r\n"))
  1653  	if err != nil {
  1654  		t.Fatal(err)
  1655  	}
  1656  	res, err = ReadResponse(br, &Request{Method: "GET"})
  1657  	if err != nil {
  1658  		t.Fatal(err)
  1659  	}
  1660  	if res.StatusCode != 400 {
  1661  		t.Errorf("Got non-400 response to GET *: %#v", res)
  1662  	}
  1663  
  1664  	res, err = Get(ts.URL + "/second")
  1665  	if err != nil {
  1666  		t.Fatal(err)
  1667  	}
  1668  	res.Body.Close()
  1669  	if got := <-uric; got != "/second" {
  1670  		t.Errorf("Handler saw request for %q; want /second", got)
  1671  	}
  1672  }
  1673  
  1674  // Tests regarding the ordering of Write, WriteHeader, Header, and
  1675  // Flush calls.  In Go 1.0, rw.WriteHeader immediately flushed the
  1676  // (*response).header to the wire. In Go 1.1, the actual wire flush is
  1677  // delayed, so we could maybe tack on a Content-Length and better
  1678  // Content-Type after we see more (or all) of the output. To preserve
  1679  // compatibility with Go 1, we need to be careful to track which
  1680  // headers were live at the time of WriteHeader, so we write the same
  1681  // ones, even if the handler modifies them (~erroneously) after the
  1682  // first Write.
  1683  func TestHeaderToWire(t *testing.T) {
  1684  	tests := []struct {
  1685  		name    string
  1686  		handler func(ResponseWriter, *Request)
  1687  		check   func(output string) error
  1688  	}{
  1689  		{
  1690  			name: "write without Header",
  1691  			handler: func(rw ResponseWriter, r *Request) {
  1692  				rw.Write([]byte("hello world"))
  1693  			},
  1694  			check: func(got string) error {
  1695  				if !strings.Contains(got, "Content-Length:") {
  1696  					return errors.New("no content-length")
  1697  				}
  1698  				if !strings.Contains(got, "Content-Type: text/plain") {
  1699  					return errors.New("no content-length")
  1700  				}
  1701  				return nil
  1702  			},
  1703  		},
  1704  		{
  1705  			name: "Header mutation before write",
  1706  			handler: func(rw ResponseWriter, r *Request) {
  1707  				h := rw.Header()
  1708  				h.Set("Content-Type", "some/type")
  1709  				rw.Write([]byte("hello world"))
  1710  				h.Set("Too-Late", "bogus")
  1711  			},
  1712  			check: func(got string) error {
  1713  				if !strings.Contains(got, "Content-Length:") {
  1714  					return errors.New("no content-length")
  1715  				}
  1716  				if !strings.Contains(got, "Content-Type: some/type") {
  1717  					return errors.New("wrong content-type")
  1718  				}
  1719  				if strings.Contains(got, "Too-Late") {
  1720  					return errors.New("don't want too-late header")
  1721  				}
  1722  				return nil
  1723  			},
  1724  		},
  1725  		{
  1726  			name: "write then useless Header mutation",
  1727  			handler: func(rw ResponseWriter, r *Request) {
  1728  				rw.Write([]byte("hello world"))
  1729  				rw.Header().Set("Too-Late", "Write already wrote headers")
  1730  			},
  1731  			check: func(got string) error {
  1732  				if strings.Contains(got, "Too-Late") {
  1733  					return errors.New("header appeared from after WriteHeader")
  1734  				}
  1735  				return nil
  1736  			},
  1737  		},
  1738  		{
  1739  			name: "flush then write",
  1740  			handler: func(rw ResponseWriter, r *Request) {
  1741  				rw.(Flusher).Flush()
  1742  				rw.Write([]byte("post-flush"))
  1743  				rw.Header().Set("Too-Late", "Write already wrote headers")
  1744  			},
  1745  			check: func(got string) error {
  1746  				if !strings.Contains(got, "Transfer-Encoding: chunked") {
  1747  					return errors.New("not chunked")
  1748  				}
  1749  				if strings.Contains(got, "Too-Late") {
  1750  					return errors.New("header appeared from after WriteHeader")
  1751  				}
  1752  				return nil
  1753  			},
  1754  		},
  1755  		{
  1756  			name: "header then flush",
  1757  			handler: func(rw ResponseWriter, r *Request) {
  1758  				rw.Header().Set("Content-Type", "some/type")
  1759  				rw.(Flusher).Flush()
  1760  				rw.Write([]byte("post-flush"))
  1761  				rw.Header().Set("Too-Late", "Write already wrote headers")
  1762  			},
  1763  			check: func(got string) error {
  1764  				if !strings.Contains(got, "Transfer-Encoding: chunked") {
  1765  					return errors.New("not chunked")
  1766  				}
  1767  				if strings.Contains(got, "Too-Late") {
  1768  					return errors.New("header appeared from after WriteHeader")
  1769  				}
  1770  				if !strings.Contains(got, "Content-Type: some/type") {
  1771  					return errors.New("wrong content-length")
  1772  				}
  1773  				return nil
  1774  			},
  1775  		},
  1776  		{
  1777  			name: "sniff-on-first-write content-type",
  1778  			handler: func(rw ResponseWriter, r *Request) {
  1779  				rw.Write([]byte("<html><head></head><body>some html</body></html>"))
  1780  				rw.Header().Set("Content-Type", "x/wrong")
  1781  			},
  1782  			check: func(got string) error {
  1783  				if !strings.Contains(got, "Content-Type: text/html") {
  1784  					return errors.New("wrong content-length; want html")
  1785  				}
  1786  				return nil
  1787  			},
  1788  		},
  1789  		{
  1790  			name: "explicit content-type wins",
  1791  			handler: func(rw ResponseWriter, r *Request) {
  1792  				rw.Header().Set("Content-Type", "some/type")
  1793  				rw.Write([]byte("<html><head></head><body>some html</body></html>"))
  1794  			},
  1795  			check: func(got string) error {
  1796  				if !strings.Contains(got, "Content-Type: some/type") {
  1797  					return errors.New("wrong content-length; want html")
  1798  				}
  1799  				return nil
  1800  			},
  1801  		},
  1802  		{
  1803  			name: "empty handler",
  1804  			handler: func(rw ResponseWriter, r *Request) {
  1805  			},
  1806  			check: func(got string) error {
  1807  				if !strings.Contains(got, "Content-Type: text/plain") {
  1808  					return errors.New("wrong content-length; want text/plain")
  1809  				}
  1810  				if !strings.Contains(got, "Content-Length: 0") {
  1811  					return errors.New("want 0 content-length")
  1812  				}
  1813  				return nil
  1814  			},
  1815  		},
  1816  		{
  1817  			name: "only Header, no write",
  1818  			handler: func(rw ResponseWriter, r *Request) {
  1819  				rw.Header().Set("Some-Header", "some-value")
  1820  			},
  1821  			check: func(got string) error {
  1822  				if !strings.Contains(got, "Some-Header") {
  1823  					return errors.New("didn't get header")
  1824  				}
  1825  				return nil
  1826  			},
  1827  		},
  1828  		{
  1829  			name: "WriteHeader call",
  1830  			handler: func(rw ResponseWriter, r *Request) {
  1831  				rw.WriteHeader(404)
  1832  				rw.Header().Set("Too-Late", "some-value")
  1833  			},
  1834  			check: func(got string) error {
  1835  				if !strings.Contains(got, "404") {
  1836  					return errors.New("wrong status")
  1837  				}
  1838  				if strings.Contains(got, "Some-Header") {
  1839  					return errors.New("shouldn't have seen Too-Late")
  1840  				}
  1841  				return nil
  1842  			},
  1843  		},
  1844  	}
  1845  	for _, tc := range tests {
  1846  		ht := newHandlerTest(HandlerFunc(tc.handler))
  1847  		got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org")
  1848  		if err := tc.check(got); err != nil {
  1849  			t.Errorf("%s: %v\nGot response:\n%s", tc.name, err, got)
  1850  		}
  1851  	}
  1852  }
  1853  
  1854  // goTimeout runs f, failing t if f takes more than ns to complete.
  1855  func goTimeout(t *testing.T, d time.Duration, f func()) {
  1856  	ch := make(chan bool, 2)
  1857  	timer := time.AfterFunc(d, func() {
  1858  		t.Errorf("Timeout expired after %v", d)
  1859  		ch <- true
  1860  	})
  1861  	defer timer.Stop()
  1862  	go func() {
  1863  		defer func() { ch <- true }()
  1864  		f()
  1865  	}()
  1866  	<-ch
  1867  }
  1868  
  1869  type errorListener struct {
  1870  	errs []error
  1871  }
  1872  
  1873  func (l *errorListener) Accept() (c net.Conn, err error) {
  1874  	if len(l.errs) == 0 {
  1875  		return nil, io.EOF
  1876  	}
  1877  	err = l.errs[0]
  1878  	l.errs = l.errs[1:]
  1879  	return
  1880  }
  1881  
  1882  func (l *errorListener) Close() error {
  1883  	return nil
  1884  }
  1885  
  1886  func (l *errorListener) Addr() net.Addr {
  1887  	return dummyAddr("test-address")
  1888  }
  1889  
  1890  func TestAcceptMaxFds(t *testing.T) {
  1891  	log.SetOutput(ioutil.Discard) // is noisy otherwise
  1892  	defer log.SetOutput(os.Stderr)
  1893  
  1894  	ln := &errorListener{[]error{
  1895  		&net.OpError{
  1896  			Op:  "accept",
  1897  			Err: syscall.EMFILE,
  1898  		}}}
  1899  	err := Serve(ln, HandlerFunc(HandlerFunc(func(ResponseWriter, *Request) {})))
  1900  	if err != io.EOF {
  1901  		t.Errorf("got error %v, want EOF", err)
  1902  	}
  1903  }
  1904  
  1905  func TestWriteAfterHijack(t *testing.T) {
  1906  	req := reqBytes("GET / HTTP/1.1\nHost: golang.org")
  1907  	var buf bytes.Buffer
  1908  	wrotec := make(chan bool, 1)
  1909  	conn := &rwTestConn{
  1910  		Reader: bytes.NewReader(req),
  1911  		Writer: &buf,
  1912  		closec: make(chan bool, 1),
  1913  	}
  1914  	handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  1915  		conn, bufrw, err := rw.(Hijacker).Hijack()
  1916  		if err != nil {
  1917  			t.Error(err)
  1918  			return
  1919  		}
  1920  		go func() {
  1921  			bufrw.Write([]byte("[hijack-to-bufw]"))
  1922  			bufrw.Flush()
  1923  			conn.Write([]byte("[hijack-to-conn]"))
  1924  			conn.Close()
  1925  			wrotec <- true
  1926  		}()
  1927  	})
  1928  	ln := &oneConnListener{conn: conn}
  1929  	go Serve(ln, handler)
  1930  	<-conn.closec
  1931  	<-wrotec
  1932  	if g, w := buf.String(), "[hijack-to-bufw][hijack-to-conn]"; g != w {
  1933  		t.Errorf("wrote %q; want %q", g, w)
  1934  	}
  1935  }
  1936  
  1937  // http://code.google.com/p/go/issues/detail?id=5955
  1938  // Note that this does not test the "request too large"
  1939  // exit path from the http server. This is intentional;
  1940  // not sending Connection: close is just a minor wire
  1941  // optimization and is pointless if dealing with a
  1942  // badly behaved client.
  1943  func TestHTTP10ConnectionHeader(t *testing.T) {
  1944  	defer afterTest(t)
  1945  
  1946  	mux := NewServeMux()
  1947  	mux.Handle("/", HandlerFunc(func(resp ResponseWriter, req *Request) {}))
  1948  	ts := httptest.NewServer(mux)
  1949  	defer ts.Close()
  1950  
  1951  	// net/http uses HTTP/1.1 for requests, so write requests manually
  1952  	tests := []struct {
  1953  		req    string   // raw http request
  1954  		expect []string // expected Connection header(s)
  1955  	}{
  1956  		{
  1957  			req:    "GET / HTTP/1.0\r\n\r\n",
  1958  			expect: nil,
  1959  		},
  1960  		{
  1961  			req:    "OPTIONS * HTTP/1.0\r\n\r\n",
  1962  			expect: nil,
  1963  		},
  1964  		{
  1965  			req:    "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n",
  1966  			expect: []string{"keep-alive"},
  1967  		},
  1968  	}
  1969  
  1970  	for _, tt := range tests {
  1971  		conn, err := net.Dial("tcp", ts.Listener.Addr().String())
  1972  		if err != nil {
  1973  			t.Fatal("dial err:", err)
  1974  		}
  1975  
  1976  		_, err = fmt.Fprint(conn, tt.req)
  1977  		if err != nil {
  1978  			t.Fatal("conn write err:", err)
  1979  		}
  1980  
  1981  		resp, err := ReadResponse(bufio.NewReader(conn), &Request{Method: "GET"})
  1982  		if err != nil {
  1983  			t.Fatal("ReadResponse err:", err)
  1984  		}
  1985  		conn.Close()
  1986  		resp.Body.Close()
  1987  
  1988  		got := resp.Header["Connection"]
  1989  		if !reflect.DeepEqual(got, tt.expect) {
  1990  			t.Errorf("wrong Connection headers for request %q. Got %q expect %q", got, tt.expect)
  1991  		}
  1992  	}
  1993  }
  1994  
  1995  // See golang.org/issue/5660
  1996  func TestServerReaderFromOrder(t *testing.T) {
  1997  	defer afterTest(t)
  1998  	pr, pw := io.Pipe()
  1999  	const size = 3 << 20
  2000  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
  2001  		rw.Header().Set("Content-Type", "text/plain") // prevent sniffing path
  2002  		done := make(chan bool)
  2003  		go func() {
  2004  			io.Copy(rw, pr)
  2005  			close(done)
  2006  		}()
  2007  		time.Sleep(25 * time.Millisecond) // give Copy a chance to break things
  2008  		n, err := io.Copy(ioutil.Discard, req.Body)
  2009  		if err != nil {
  2010  			t.Errorf("handler Copy: %v", err)
  2011  			return
  2012  		}
  2013  		if n != size {
  2014  			t.Errorf("handler Copy = %d; want %d", n, size)
  2015  		}
  2016  		pw.Write([]byte("hi"))
  2017  		pw.Close()
  2018  		<-done
  2019  	}))
  2020  	defer ts.Close()
  2021  
  2022  	req, err := NewRequest("POST", ts.URL, io.LimitReader(neverEnding('a'), size))
  2023  	if err != nil {
  2024  		t.Fatal(err)
  2025  	}
  2026  	res, err := DefaultClient.Do(req)
  2027  	if err != nil {
  2028  		t.Fatal(err)
  2029  	}
  2030  	all, err := ioutil.ReadAll(res.Body)
  2031  	if err != nil {
  2032  		t.Fatal(err)
  2033  	}
  2034  	res.Body.Close()
  2035  	if string(all) != "hi" {
  2036  		t.Errorf("Body = %q; want hi", all)
  2037  	}
  2038  }
  2039  
  2040  // Issue 6157
  2041  func TestNoContentTypeOnNotModified(t *testing.T) {
  2042  	ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
  2043  		if r.URL.Path == "/header" {
  2044  			w.Header().Set("Content-Length", "123")
  2045  		}
  2046  		w.WriteHeader(StatusNotModified)
  2047  		if r.URL.Path == "/more" {
  2048  			w.Write([]byte("stuff"))
  2049  		}
  2050  	}))
  2051  	for _, req := range []string{
  2052  		"GET / HTTP/1.0",
  2053  		"GET /header HTTP/1.0",
  2054  		"GET /more HTTP/1.0",
  2055  		"GET / HTTP/1.1",
  2056  		"GET /header HTTP/1.1",
  2057  		"GET /more HTTP/1.1",
  2058  	} {
  2059  		got := ht.rawResponse(req)
  2060  		if !strings.Contains(got, "304 Not Modified") {
  2061  			t.Errorf("Non-304 Not Modified for %q: %s", req, got)
  2062  		} else if strings.Contains(got, "Content-Length") {
  2063  			t.Errorf("Got a Content-Length from %q: %s", req, got)
  2064  		}
  2065  	}
  2066  }
  2067  
  2068  func TestResponseWriterWriteStringAllocs(t *testing.T) {
  2069  	ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
  2070  		if r.URL.Path == "/s" {
  2071  			io.WriteString(w, "Hello world")
  2072  		} else {
  2073  			w.Write([]byte("Hello world"))
  2074  		}
  2075  	}))
  2076  	before := testing.AllocsPerRun(25, func() { ht.rawResponse("GET / HTTP/1.0") })
  2077  	after := testing.AllocsPerRun(25, func() { ht.rawResponse("GET /s HTTP/1.0") })
  2078  	if int(after) >= int(before) {
  2079  		t.Errorf("WriteString allocs of %v >= Write allocs of %v", after, before)
  2080  	}
  2081  }
  2082  
  2083  func BenchmarkClientServer(b *testing.B) {
  2084  	b.ReportAllocs()
  2085  	b.StopTimer()
  2086  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
  2087  		fmt.Fprintf(rw, "Hello world.\n")
  2088  	}))
  2089  	defer ts.Close()
  2090  	b.StartTimer()
  2091  
  2092  	for i := 0; i < b.N; i++ {
  2093  		res, err := Get(ts.URL)
  2094  		if err != nil {
  2095  			b.Fatal("Get:", err)
  2096  		}
  2097  		all, err := ioutil.ReadAll(res.Body)
  2098  		if err != nil {
  2099  			b.Fatal("ReadAll:", err)
  2100  		}
  2101  		body := string(all)
  2102  		if body != "Hello world.\n" {
  2103  			b.Fatal("Got body:", body)
  2104  		}
  2105  	}
  2106  
  2107  	b.StopTimer()
  2108  }
  2109  
  2110  func BenchmarkClientServerParallel4(b *testing.B) {
  2111  	benchmarkClientServerParallel(b, 4)
  2112  }
  2113  
  2114  func BenchmarkClientServerParallel64(b *testing.B) {
  2115  	benchmarkClientServerParallel(b, 64)
  2116  }
  2117  
  2118  func benchmarkClientServerParallel(b *testing.B, conc int) {
  2119  	b.ReportAllocs()
  2120  	b.StopTimer()
  2121  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
  2122  		fmt.Fprintf(rw, "Hello world.\n")
  2123  	}))
  2124  	defer ts.Close()
  2125  	b.StartTimer()
  2126  
  2127  	numProcs := runtime.GOMAXPROCS(-1) * conc
  2128  	var wg sync.WaitGroup
  2129  	wg.Add(numProcs)
  2130  	n := int32(b.N)
  2131  	for p := 0; p < numProcs; p++ {
  2132  		go func() {
  2133  			for atomic.AddInt32(&n, -1) >= 0 {
  2134  				res, err := Get(ts.URL)
  2135  				if err != nil {
  2136  					b.Logf("Get: %v", err)
  2137  					continue
  2138  				}
  2139  				all, err := ioutil.ReadAll(res.Body)
  2140  				if err != nil {
  2141  					b.Logf("ReadAll: %v", err)
  2142  					continue
  2143  				}
  2144  				body := string(all)
  2145  				if body != "Hello world.\n" {
  2146  					panic("Got body: " + body)
  2147  				}
  2148  			}
  2149  			wg.Done()
  2150  		}()
  2151  	}
  2152  	wg.Wait()
  2153  }
  2154  
  2155  // A benchmark for profiling the server without the HTTP client code.
  2156  // The client code runs in a subprocess.
  2157  //
  2158  // For use like:
  2159  //   $ go test -c
  2160  //   $ ./http.test -test.run=XX -test.bench=BenchmarkServer -test.benchtime=15s -test.cpuprofile=http.prof
  2161  //   $ go tool pprof http.test http.prof
  2162  //   (pprof) web
  2163  func BenchmarkServer(b *testing.B) {
  2164  	b.ReportAllocs()
  2165  	// Child process mode;
  2166  	if url := os.Getenv("TEST_BENCH_SERVER_URL"); url != "" {
  2167  		n, err := strconv.Atoi(os.Getenv("TEST_BENCH_CLIENT_N"))
  2168  		if err != nil {
  2169  			panic(err)
  2170  		}
  2171  		for i := 0; i < n; i++ {
  2172  			res, err := Get(url)
  2173  			if err != nil {
  2174  				log.Panicf("Get: %v", err)
  2175  			}
  2176  			all, err := ioutil.ReadAll(res.Body)
  2177  			if err != nil {
  2178  				log.Panicf("ReadAll: %v", err)
  2179  			}
  2180  			body := string(all)
  2181  			if body != "Hello world.\n" {
  2182  				log.Panicf("Got body: %q", body)
  2183  			}
  2184  		}
  2185  		os.Exit(0)
  2186  		return
  2187  	}
  2188  
  2189  	var res = []byte("Hello world.\n")
  2190  	b.StopTimer()
  2191  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
  2192  		rw.Header().Set("Content-Type", "text/html; charset=utf-8")
  2193  		rw.Write(res)
  2194  	}))
  2195  	defer ts.Close()
  2196  	b.StartTimer()
  2197  
  2198  	cmd := exec.Command(os.Args[0], "-test.run=XXXX", "-test.bench=BenchmarkServer")
  2199  	cmd.Env = append([]string{
  2200  		fmt.Sprintf("TEST_BENCH_CLIENT_N=%d", b.N),
  2201  		fmt.Sprintf("TEST_BENCH_SERVER_URL=%s", ts.URL),
  2202  	}, os.Environ()...)
  2203  	out, err := cmd.CombinedOutput()
  2204  	if err != nil {
  2205  		b.Errorf("Test failure: %v, with output: %s", err, out)
  2206  	}
  2207  }
  2208  
  2209  func BenchmarkServerFakeConnNoKeepAlive(b *testing.B) {
  2210  	b.ReportAllocs()
  2211  	req := reqBytes(`GET / HTTP/1.0
  2212  Host: golang.org
  2213  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  2214  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
  2215  Accept-Encoding: gzip,deflate,sdch
  2216  Accept-Language: en-US,en;q=0.8
  2217  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  2218  `)
  2219  	res := []byte("Hello world!\n")
  2220  
  2221  	conn := &testConn{
  2222  		// testConn.Close will not push into the channel
  2223  		// if it's full.
  2224  		closec: make(chan bool, 1),
  2225  	}
  2226  	handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  2227  		rw.Header().Set("Content-Type", "text/html; charset=utf-8")
  2228  		rw.Write(res)
  2229  	})
  2230  	ln := new(oneConnListener)
  2231  	for i := 0; i < b.N; i++ {
  2232  		conn.readBuf.Reset()
  2233  		conn.writeBuf.Reset()
  2234  		conn.readBuf.Write(req)
  2235  		ln.conn = conn
  2236  		Serve(ln, handler)
  2237  		<-conn.closec
  2238  	}
  2239  }
  2240  
  2241  // repeatReader reads content count times, then EOFs.
  2242  type repeatReader struct {
  2243  	content []byte
  2244  	count   int
  2245  	off     int
  2246  }
  2247  
  2248  func (r *repeatReader) Read(p []byte) (n int, err error) {
  2249  	if r.count <= 0 {
  2250  		return 0, io.EOF
  2251  	}
  2252  	n = copy(p, r.content[r.off:])
  2253  	r.off += n
  2254  	if r.off == len(r.content) {
  2255  		r.count--
  2256  		r.off = 0
  2257  	}
  2258  	return
  2259  }
  2260  
  2261  func BenchmarkServerFakeConnWithKeepAlive(b *testing.B) {
  2262  	b.ReportAllocs()
  2263  
  2264  	req := reqBytes(`GET / HTTP/1.1
  2265  Host: golang.org
  2266  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  2267  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
  2268  Accept-Encoding: gzip,deflate,sdch
  2269  Accept-Language: en-US,en;q=0.8
  2270  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  2271  `)
  2272  	res := []byte("Hello world!\n")
  2273  
  2274  	conn := &rwTestConn{
  2275  		Reader: &repeatReader{content: req, count: b.N},
  2276  		Writer: ioutil.Discard,
  2277  		closec: make(chan bool, 1),
  2278  	}
  2279  	handled := 0
  2280  	handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  2281  		handled++
  2282  		rw.Header().Set("Content-Type", "text/html; charset=utf-8")
  2283  		rw.Write(res)
  2284  	})
  2285  	ln := &oneConnListener{conn: conn}
  2286  	go Serve(ln, handler)
  2287  	<-conn.closec
  2288  	if b.N != handled {
  2289  		b.Errorf("b.N=%d but handled %d", b.N, handled)
  2290  	}
  2291  }
  2292  
  2293  // same as above, but representing the most simple possible request
  2294  // and handler. Notably: the handler does not call rw.Header().
  2295  func BenchmarkServerFakeConnWithKeepAliveLite(b *testing.B) {
  2296  	b.ReportAllocs()
  2297  
  2298  	req := reqBytes(`GET / HTTP/1.1
  2299  Host: golang.org
  2300  `)
  2301  	res := []byte("Hello world!\n")
  2302  
  2303  	conn := &rwTestConn{
  2304  		Reader: &repeatReader{content: req, count: b.N},
  2305  		Writer: ioutil.Discard,
  2306  		closec: make(chan bool, 1),
  2307  	}
  2308  	handled := 0
  2309  	handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  2310  		handled++
  2311  		rw.Write(res)
  2312  	})
  2313  	ln := &oneConnListener{conn: conn}
  2314  	go Serve(ln, handler)
  2315  	<-conn.closec
  2316  	if b.N != handled {
  2317  		b.Errorf("b.N=%d but handled %d", b.N, handled)
  2318  	}
  2319  }
  2320  
  2321  const someResponse = "<html>some response</html>"
  2322  
  2323  // A Response that's just no bigger than 2KB, the buffer-before-chunking threshold.
  2324  var response = bytes.Repeat([]byte(someResponse), 2<<10/len(someResponse))
  2325  
  2326  // Both Content-Type and Content-Length set. Should be no buffering.
  2327  func BenchmarkServerHandlerTypeLen(b *testing.B) {
  2328  	benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
  2329  		w.Header().Set("Content-Type", "text/html")
  2330  		w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  2331  		w.Write(response)
  2332  	}))
  2333  }
  2334  
  2335  // A Content-Type is set, but no length. No sniffing, but will count the Content-Length.
  2336  func BenchmarkServerHandlerNoLen(b *testing.B) {
  2337  	benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
  2338  		w.Header().Set("Content-Type", "text/html")
  2339  		w.Write(response)
  2340  	}))
  2341  }
  2342  
  2343  // A Content-Length is set, but the Content-Type will be sniffed.
  2344  func BenchmarkServerHandlerNoType(b *testing.B) {
  2345  	benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
  2346  		w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  2347  		w.Write(response)
  2348  	}))
  2349  }
  2350  
  2351  // Neither a Content-Type or Content-Length, so sniffed and counted.
  2352  func BenchmarkServerHandlerNoHeader(b *testing.B) {
  2353  	benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
  2354  		w.Write(response)
  2355  	}))
  2356  }
  2357  
  2358  func benchmarkHandler(b *testing.B, h Handler) {
  2359  	b.ReportAllocs()
  2360  	req := reqBytes(`GET / HTTP/1.1
  2361  Host: golang.org
  2362  `)
  2363  	conn := &rwTestConn{
  2364  		Reader: &repeatReader{content: req, count: b.N},
  2365  		Writer: ioutil.Discard,
  2366  		closec: make(chan bool, 1),
  2367  	}
  2368  	handled := 0
  2369  	handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
  2370  		handled++
  2371  		h.ServeHTTP(rw, r)
  2372  	})
  2373  	ln := &oneConnListener{conn: conn}
  2374  	go Serve(ln, handler)
  2375  	<-conn.closec
  2376  	if b.N != handled {
  2377  		b.Errorf("b.N=%d but handled %d", b.N, handled)
  2378  	}
  2379  }