PageRenderTime 110ms CodeModel.GetById 19ms app.highlight 75ms RepoModel.GetById 2ms app.codeStats 0ms

/http/serve_test.go

http://github.com/petar/GoHTTP
Go | 1082 lines | 872 code | 118 blank | 92 comment | 223 complexity | 6186ebc0852a3a6dfaf4d39058267825 MD5 | raw file
   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
   7package http_test
   8
   9import (
  10	"bufio"
  11	"bytes"
  12	"fmt"
  13	. "http"
  14	"http/httptest"
  15	"io"
  16	"io/ioutil"
  17	"log"
  18	"os"
  19	"net"
  20	"reflect"
  21	"strings"
  22	"syscall"
  23	"testing"
  24	"time"
  25	"url"
  26)
  27
  28type dummyAddr string
  29type oneConnListener struct {
  30	conn net.Conn
  31}
  32
  33func (l *oneConnListener) Accept() (c net.Conn, err os.Error) {
  34	c = l.conn
  35	if c == nil {
  36		err = os.EOF
  37		return
  38	}
  39	err = nil
  40	l.conn = nil
  41	return
  42}
  43
  44func (l *oneConnListener) Close() os.Error {
  45	return nil
  46}
  47
  48func (l *oneConnListener) Addr() net.Addr {
  49	return dummyAddr("test-address")
  50}
  51
  52func (a dummyAddr) Network() string {
  53	return string(a)
  54}
  55
  56func (a dummyAddr) String() string {
  57	return string(a)
  58}
  59
  60type testConn struct {
  61	readBuf  bytes.Buffer
  62	writeBuf bytes.Buffer
  63}
  64
  65func (c *testConn) Read(b []byte) (int, os.Error) {
  66	return c.readBuf.Read(b)
  67}
  68
  69func (c *testConn) Write(b []byte) (int, os.Error) {
  70	return c.writeBuf.Write(b)
  71}
  72
  73func (c *testConn) Close() os.Error {
  74	return nil
  75}
  76
  77func (c *testConn) LocalAddr() net.Addr {
  78	return dummyAddr("local-addr")
  79}
  80
  81func (c *testConn) RemoteAddr() net.Addr {
  82	return dummyAddr("remote-addr")
  83}
  84
  85func (c *testConn) SetTimeout(nsec int64) os.Error {
  86	return nil
  87}
  88
  89func (c *testConn) SetReadTimeout(nsec int64) os.Error {
  90	return nil
  91}
  92
  93func (c *testConn) SetWriteTimeout(nsec int64) os.Error {
  94	return nil
  95}
  96
  97func TestConsumingBodyOnNextConn(t *testing.T) {
  98	conn := new(testConn)
  99	for i := 0; i < 2; i++ {
 100		conn.readBuf.Write([]byte(
 101			"POST / HTTP/1.1\r\n" +
 102				"Host: test\r\n" +
 103				"Content-Length: 11\r\n" +
 104				"\r\n" +
 105				"foo=1&bar=1"))
 106	}
 107
 108	reqNum := 0
 109	ch := make(chan *Request)
 110	servech := make(chan os.Error)
 111	listener := &oneConnListener{conn}
 112	handler := func(res ResponseWriter, req *Request) {
 113		reqNum++
 114		ch <- req
 115	}
 116
 117	go func() {
 118		servech <- Serve(listener, HandlerFunc(handler))
 119	}()
 120
 121	var req *Request
 122	req = <-ch
 123	if req == nil {
 124		t.Fatal("Got nil first request.")
 125	}
 126	if req.Method != "POST" {
 127		t.Errorf("For request #1's method, got %q; expected %q",
 128			req.Method, "POST")
 129	}
 130
 131	req = <-ch
 132	if req == nil {
 133		t.Fatal("Got nil first request.")
 134	}
 135	if req.Method != "POST" {
 136		t.Errorf("For request #2's method, got %q; expected %q",
 137			req.Method, "POST")
 138	}
 139
 140	if serveerr := <-servech; serveerr != os.EOF {
 141		t.Errorf("Serve returned %q; expected EOF", serveerr)
 142	}
 143}
 144
 145type stringHandler string
 146
 147func (s stringHandler) ServeHTTP(w ResponseWriter, r *Request) {
 148	w.Header().Set("Result", string(s))
 149}
 150
 151var handlers = []struct {
 152	pattern string
 153	msg     string
 154}{
 155	{"/", "Default"},
 156	{"/someDir/", "someDir"},
 157	{"someHost.com/someDir/", "someHost.com/someDir"},
 158}
 159
 160var vtests = []struct {
 161	url      string
 162	expected string
 163}{
 164	{"http://localhost/someDir/apage", "someDir"},
 165	{"http://localhost/otherDir/apage", "Default"},
 166	{"http://someHost.com/someDir/apage", "someHost.com/someDir"},
 167	{"http://otherHost.com/someDir/apage", "someDir"},
 168	{"http://otherHost.com/aDir/apage", "Default"},
 169}
 170
 171func TestHostHandlers(t *testing.T) {
 172	for _, h := range handlers {
 173		Handle(h.pattern, stringHandler(h.msg))
 174	}
 175	ts := httptest.NewServer(nil)
 176	defer ts.Close()
 177
 178	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
 179	if err != nil {
 180		t.Fatal(err)
 181	}
 182	defer conn.Close()
 183	cc := NewClientConn(conn, nil)
 184	for _, vt := range vtests {
 185		var r *Response
 186		var req Request
 187		if req.URL, err = url.Parse(vt.url); err != nil {
 188			t.Errorf("cannot parse url: %v", err)
 189			continue
 190		}
 191		if err := cc.Write(&req); err != nil {
 192			t.Errorf("writing request: %v", err)
 193			continue
 194		}
 195		r, err := cc.Read(&req)
 196		if err != nil {
 197			t.Errorf("reading response: %v", err)
 198			continue
 199		}
 200		s := r.Header.Get("Result")
 201		if s != vt.expected {
 202			t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
 203		}
 204	}
 205}
 206
 207// Tests for http://code.google.com/p/go/issues/detail?id=900
 208func TestMuxRedirectLeadingSlashes(t *testing.T) {
 209	paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"}
 210	for _, path := range paths {
 211		req, err := ReadRequest(bufio.NewReader(bytes.NewBufferString("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n")))
 212		if err != nil {
 213			t.Errorf("%s", err)
 214		}
 215		mux := NewServeMux()
 216		resp := httptest.NewRecorder()
 217
 218		mux.ServeHTTP(resp, req)
 219
 220		if loc, expected := resp.Header().Get("Location"), "/foo.txt"; loc != expected {
 221			t.Errorf("Expected Location header set to %q; got %q", expected, loc)
 222			return
 223		}
 224
 225		if code, expected := resp.Code, StatusMovedPermanently; code != expected {
 226			t.Errorf("Expected response code of StatusMovedPermanently; got %d", code)
 227			return
 228		}
 229	}
 230}
 231
 232func TestServerTimeouts(t *testing.T) {
 233	// TODO(bradfitz): convert this to use httptest.Server
 234	l, err := net.Listen("tcp", "127.0.0.1:0")
 235	if err != nil {
 236		t.Fatalf("listen error: %v", err)
 237	}
 238	addr, _ := l.Addr().(*net.TCPAddr)
 239
 240	reqNum := 0
 241	handler := HandlerFunc(func(res ResponseWriter, req *Request) {
 242		reqNum++
 243		fmt.Fprintf(res, "req=%d", reqNum)
 244	})
 245
 246	const second = 1000000000 /* nanos */
 247	server := &Server{Handler: handler, ReadTimeout: 0.25 * second, WriteTimeout: 0.25 * second}
 248	go server.Serve(l)
 249
 250	url := fmt.Sprintf("http://%s/", addr)
 251
 252	// Hit the HTTP server successfully.
 253	tr := &Transport{DisableKeepAlives: true} // they interfere with this test
 254	c := &Client{Transport: tr}
 255	r, err := c.Get(url)
 256	if err != nil {
 257		t.Fatalf("http Get #1: %v", err)
 258	}
 259	got, _ := ioutil.ReadAll(r.Body)
 260	expected := "req=1"
 261	if string(got) != expected {
 262		t.Errorf("Unexpected response for request #1; got %q; expected %q",
 263			string(got), expected)
 264	}
 265
 266	// Slow client that should timeout.
 267	t1 := time.Nanoseconds()
 268	conn, err := net.Dial("tcp", addr.String())
 269	if err != nil {
 270		t.Fatalf("Dial: %v", err)
 271	}
 272	buf := make([]byte, 1)
 273	n, err := conn.Read(buf)
 274	latency := time.Nanoseconds() - t1
 275	if n != 0 || err != os.EOF {
 276		t.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, os.EOF)
 277	}
 278	if latency < second*0.20 /* fudge from 0.25 above */ {
 279		t.Errorf("got EOF after %d ns, want >= %d", latency, second*0.20)
 280	}
 281
 282	// Hit the HTTP server successfully again, verifying that the
 283	// previous slow connection didn't run our handler.  (that we
 284	// get "req=2", not "req=3")
 285	r, err = Get(url)
 286	if err != nil {
 287		t.Fatalf("http Get #2: %v", err)
 288	}
 289	got, _ = ioutil.ReadAll(r.Body)
 290	expected = "req=2"
 291	if string(got) != expected {
 292		t.Errorf("Get #2 got %q, want %q", string(got), expected)
 293	}
 294
 295	l.Close()
 296}
 297
 298// TestIdentityResponse verifies that a handler can unset 
 299func TestIdentityResponse(t *testing.T) {
 300	handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
 301		rw.Header().Set("Content-Length", "3")
 302		rw.Header().Set("Transfer-Encoding", req.FormValue("te"))
 303		switch {
 304		case req.FormValue("overwrite") == "1":
 305			_, err := rw.Write([]byte("foo TOO LONG"))
 306			if err != ErrContentLength {
 307				t.Errorf("expected ErrContentLength; got %v", err)
 308			}
 309		case req.FormValue("underwrite") == "1":
 310			rw.Header().Set("Content-Length", "500")
 311			rw.Write([]byte("too short"))
 312		default:
 313			rw.Write([]byte("foo"))
 314		}
 315	})
 316
 317	ts := httptest.NewServer(handler)
 318	defer ts.Close()
 319
 320	// Note: this relies on the assumption (which is true) that
 321	// Get sends HTTP/1.1 or greater requests.  Otherwise the
 322	// server wouldn't have the choice to send back chunked
 323	// responses.
 324	for _, te := range []string{"", "identity"} {
 325		url := ts.URL + "/?te=" + te
 326		res, err := Get(url)
 327		if err != nil {
 328			t.Fatalf("error with Get of %s: %v", url, err)
 329		}
 330		if cl, expected := res.ContentLength, int64(3); cl != expected {
 331			t.Errorf("for %s expected res.ContentLength of %d; got %d", url, expected, cl)
 332		}
 333		if cl, expected := res.Header.Get("Content-Length"), "3"; cl != expected {
 334			t.Errorf("for %s expected Content-Length header of %q; got %q", url, expected, cl)
 335		}
 336		if tl, expected := len(res.TransferEncoding), 0; tl != expected {
 337			t.Errorf("for %s expected len(res.TransferEncoding) of %d; got %d (%v)",
 338				url, expected, tl, res.TransferEncoding)
 339		}
 340		res.Body.Close()
 341	}
 342
 343	// Verify that ErrContentLength is returned
 344	url := ts.URL + "/?overwrite=1"
 345	_, err := Get(url)
 346	if err != nil {
 347		t.Fatalf("error with Get of %s: %v", url, err)
 348	}
 349	// Verify that the connection is closed when the declared Content-Length
 350	// is larger than what the handler wrote.
 351	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
 352	if err != nil {
 353		t.Fatalf("error dialing: %v", err)
 354	}
 355	_, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n\r\n"))
 356	if err != nil {
 357		t.Fatalf("error writing: %v", err)
 358	}
 359	// The next ReadAll will hang for a failing test, so use a Timer instead
 360	// to fail more traditionally
 361	timer := time.AfterFunc(2e9, func() {
 362		t.Fatalf("Timeout expired in ReadAll.")
 363	})
 364	defer timer.Stop()
 365	got, _ := ioutil.ReadAll(conn)
 366	expectedSuffix := "\r\n\r\ntoo short"
 367	if !strings.HasSuffix(string(got), expectedSuffix) {
 368		t.Fatalf("Expected output to end with %q; got response body %q",
 369			expectedSuffix, string(got))
 370	}
 371}
 372
 373func testTcpConnectionCloses(t *testing.T, req string, h Handler) {
 374	s := httptest.NewServer(h)
 375	defer s.Close()
 376
 377	conn, err := net.Dial("tcp", s.Listener.Addr().String())
 378	if err != nil {
 379		t.Fatal("dial error:", err)
 380	}
 381	defer conn.Close()
 382
 383	_, err = fmt.Fprint(conn, req)
 384	if err != nil {
 385		t.Fatal("print error:", err)
 386	}
 387
 388	r := bufio.NewReader(conn)
 389	_, err = ReadResponse(r, &Request{Method: "GET"})
 390	if err != nil {
 391		t.Fatal("ReadResponse error:", err)
 392	}
 393
 394	success := make(chan bool)
 395	go func() {
 396		select {
 397		case <-time.After(5e9):
 398			t.Fatal("body not closed after 5s")
 399		case <-success:
 400		}
 401	}()
 402
 403	_, err = ioutil.ReadAll(r)
 404	if err != nil {
 405		t.Fatal("read error:", err)
 406	}
 407
 408	success <- true
 409}
 410
 411// TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive.
 412func TestServeHTTP10Close(t *testing.T) {
 413	testTcpConnectionCloses(t, "GET / HTTP/1.0\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
 414		ServeFile(w, r, "testdata/file")
 415	}))
 416}
 417
 418// TestHandlersCanSetConnectionClose verifies that handlers can force a connection to close,
 419// even for HTTP/1.1 requests.
 420func TestHandlersCanSetConnectionClose11(t *testing.T) {
 421	testTcpConnectionCloses(t, "GET / HTTP/1.1\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
 422		w.Header().Set("Connection", "close")
 423	}))
 424}
 425
 426func TestHandlersCanSetConnectionClose10(t *testing.T) {
 427	testTcpConnectionCloses(t, "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) {
 428		w.Header().Set("Connection", "close")
 429	}))
 430}
 431
 432func TestSetsRemoteAddr(t *testing.T) {
 433	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 434		fmt.Fprintf(w, "%s", r.RemoteAddr)
 435	}))
 436	defer ts.Close()
 437
 438	res, err := Get(ts.URL)
 439	if err != nil {
 440		t.Fatalf("Get error: %v", err)
 441	}
 442	body, err := ioutil.ReadAll(res.Body)
 443	if err != nil {
 444		t.Fatalf("ReadAll error: %v", err)
 445	}
 446	ip := string(body)
 447	if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") {
 448		t.Fatalf("Expected local addr; got %q", ip)
 449	}
 450}
 451
 452func TestChunkedResponseHeaders(t *testing.T) {
 453	log.SetOutput(ioutil.Discard) // is noisy otherwise
 454	defer log.SetOutput(os.Stderr)
 455
 456	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 457		w.Header().Set("Content-Length", "intentional gibberish") // we check that this is deleted
 458		fmt.Fprintf(w, "I am a chunked response.")
 459	}))
 460	defer ts.Close()
 461
 462	res, err := Get(ts.URL)
 463	if err != nil {
 464		t.Fatalf("Get error: %v", err)
 465	}
 466	if g, e := res.ContentLength, int64(-1); g != e {
 467		t.Errorf("expected ContentLength of %d; got %d", e, g)
 468	}
 469	if g, e := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(g, e) {
 470		t.Errorf("expected TransferEncoding of %v; got %v", e, g)
 471	}
 472	if _, haveCL := res.Header["Content-Length"]; haveCL {
 473		t.Errorf("Unexpected Content-Length")
 474	}
 475}
 476
 477// Test304Responses verifies that 304s don't declare that they're
 478// chunking in their response headers and aren't allowed to produce
 479// output.
 480func Test304Responses(t *testing.T) {
 481	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 482		w.WriteHeader(StatusNotModified)
 483		_, err := w.Write([]byte("illegal body"))
 484		if err != ErrBodyNotAllowed {
 485			t.Errorf("on Write, expected ErrBodyNotAllowed, got %v", err)
 486		}
 487	}))
 488	defer ts.Close()
 489	res, err := Get(ts.URL)
 490	if err != nil {
 491		t.Error(err)
 492	}
 493	if len(res.TransferEncoding) > 0 {
 494		t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
 495	}
 496	body, err := ioutil.ReadAll(res.Body)
 497	if err != nil {
 498		t.Error(err)
 499	}
 500	if len(body) > 0 {
 501		t.Errorf("got unexpected body %q", string(body))
 502	}
 503}
 504
 505// TestHeadResponses verifies that responses to HEAD requests don't
 506// declare that they're chunking in their response headers and aren't
 507// allowed to produce output.
 508func TestHeadResponses(t *testing.T) {
 509	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 510		_, err := w.Write([]byte("Ignored body"))
 511		if err != ErrBodyNotAllowed {
 512			t.Errorf("on Write, expected ErrBodyNotAllowed, got %v", err)
 513		}
 514
 515		// Also exercise the ReaderFrom path
 516		_, err = io.Copy(w, strings.NewReader("Ignored body"))
 517		if err != ErrBodyNotAllowed {
 518			t.Errorf("on Copy, expected ErrBodyNotAllowed, got %v", err)
 519		}
 520	}))
 521	defer ts.Close()
 522	res, err := Head(ts.URL)
 523	if err != nil {
 524		t.Error(err)
 525	}
 526	if len(res.TransferEncoding) > 0 {
 527		t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
 528	}
 529	body, err := ioutil.ReadAll(res.Body)
 530	if err != nil {
 531		t.Error(err)
 532	}
 533	if len(body) > 0 {
 534		t.Errorf("got unexpected body %q", string(body))
 535	}
 536}
 537
 538func TestTLSHandshakeTimeout(t *testing.T) {
 539	if true {
 540		t.Logf("Skipping broken test; issue 2281")
 541		return
 542	}
 543	ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
 544	ts.Config.ReadTimeout = 250e6
 545	ts.StartTLS()
 546	defer ts.Close()
 547	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
 548	if err != nil {
 549		t.Fatalf("Dial: %v", err)
 550	}
 551	defer conn.Close()
 552	timer := time.AfterFunc(10e9, func() { t.Fatalf("Timeout") })
 553	defer timer.Stop()
 554
 555	var buf [1]byte
 556	n, err := conn.Read(buf[:])
 557	if err == nil || n != 0 {
 558		t.Errorf("Read = %d, %v; want an error and no bytes", n, err)
 559	}
 560}
 561
 562func TestTLSServer(t *testing.T) {
 563	ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 564		if r.TLS != nil {
 565			w.Header().Set("X-TLS-Set", "true")
 566			if r.TLS.HandshakeComplete {
 567				w.Header().Set("X-TLS-HandshakeComplete", "true")
 568			}
 569		}
 570	}))
 571	defer ts.Close()
 572
 573	// Connect an idle TCP connection to this server before we run
 574	// our real tests.  This idle connection used to block forever
 575	// in the TLS handshake, preventing future connections from
 576	// being accepted. It may prevent future accidental blocking
 577	// in newConn.
 578	idleConn, err := net.Dial("tcp", ts.Listener.Addr().String())
 579	if err != nil {
 580		t.Fatalf("Dial: %v", err)
 581	}
 582	defer idleConn.Close()
 583	time.AfterFunc(10e9, func() { t.Fatalf("Timeout") })
 584
 585	if !strings.HasPrefix(ts.URL, "https://") {
 586		t.Fatalf("expected test TLS server to start with https://, got %q", ts.URL)
 587	}
 588	res, err := Get(ts.URL)
 589	if err != nil {
 590		t.Fatal(err)
 591	}
 592	if res == nil {
 593		t.Fatalf("got nil Response")
 594	}
 595	defer res.Body.Close()
 596	if res.Header.Get("X-TLS-Set") != "true" {
 597		t.Errorf("expected X-TLS-Set response header")
 598	}
 599	if res.Header.Get("X-TLS-HandshakeComplete") != "true" {
 600		t.Errorf("expected X-TLS-HandshakeComplete header")
 601	}
 602}
 603
 604type serverExpectTest struct {
 605	contentLength    int    // of request body
 606	expectation      string // e.g. "100-continue"
 607	readBody         bool   // whether handler should read the body (if false, sends StatusUnauthorized)
 608	expectedResponse string // expected substring in first line of http response
 609}
 610
 611var serverExpectTests = []serverExpectTest{
 612	// Normal 100-continues, case-insensitive.
 613	{100, "100-continue", true, "100 Continue"},
 614	{100, "100-cOntInUE", true, "100 Continue"},
 615
 616	// No 100-continue.
 617	{100, "", true, "200 OK"},
 618
 619	// 100-continue but requesting client to deny us,
 620	// so it never reads the body.
 621	{100, "100-continue", false, "401 Unauthorized"},
 622	// Likewise without 100-continue:
 623	{100, "", false, "401 Unauthorized"},
 624
 625	// Non-standard expectations are failures
 626	{0, "a-pony", false, "417 Expectation Failed"},
 627
 628	// Expect-100 requested but no body
 629	{0, "100-continue", true, "400 Bad Request"},
 630}
 631
 632// Tests that the server responds to the "Expect" request header
 633// correctly.
 634func TestServerExpect(t *testing.T) {
 635	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 636		// Note using r.FormValue("readbody") because for POST
 637		// requests that would read from r.Body, which we only
 638		// conditionally want to do.
 639		if strings.Contains(r.URL.RawPath, "readbody=true") {
 640			ioutil.ReadAll(r.Body)
 641			w.Write([]byte("Hi"))
 642		} else {
 643			w.WriteHeader(StatusUnauthorized)
 644		}
 645	}))
 646	defer ts.Close()
 647
 648	runTest := func(test serverExpectTest) {
 649		conn, err := net.Dial("tcp", ts.Listener.Addr().String())
 650		if err != nil {
 651			t.Fatalf("Dial: %v", err)
 652		}
 653		defer conn.Close()
 654		sendf := func(format string, args ...interface{}) {
 655			_, err := fmt.Fprintf(conn, format, args...)
 656			if err != nil {
 657				t.Fatalf("On test %#v, error writing %q: %v", test, format, err)
 658			}
 659		}
 660		go func() {
 661			sendf("POST /?readbody=%v HTTP/1.1\r\n"+
 662				"Connection: close\r\n"+
 663				"Content-Length: %d\r\n"+
 664				"Expect: %s\r\nHost: foo\r\n\r\n",
 665				test.readBody, test.contentLength, test.expectation)
 666			if test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue" {
 667				body := strings.Repeat("A", test.contentLength)
 668				sendf(body)
 669			}
 670		}()
 671		bufr := bufio.NewReader(conn)
 672		line, err := bufr.ReadString('\n')
 673		if err != nil {
 674			t.Fatalf("ReadString: %v", err)
 675		}
 676		if !strings.Contains(line, test.expectedResponse) {
 677			t.Errorf("for test %#v got first line=%q", test, line)
 678		}
 679	}
 680
 681	for _, test := range serverExpectTests {
 682		runTest(test)
 683	}
 684}
 685
 686func TestServerConsumesRequestBody(t *testing.T) {
 687	conn := new(testConn)
 688	body := strings.Repeat("x", 1<<20)
 689	conn.readBuf.Write([]byte(fmt.Sprintf(
 690		"POST / HTTP/1.1\r\n"+
 691			"Host: test\r\n"+
 692			"Content-Length: %d\r\n"+
 693			"\r\n", len(body))))
 694	conn.readBuf.Write([]byte(body))
 695
 696	done := make(chan bool)
 697
 698	ls := &oneConnListener{conn}
 699	go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
 700		if conn.readBuf.Len() < len(body)/2 {
 701			t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len())
 702		}
 703		rw.WriteHeader(200)
 704		if g, e := conn.readBuf.Len(), 0; g != e {
 705			t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e)
 706		}
 707		done <- true
 708	}))
 709	<-done
 710}
 711
 712func TestTimeoutHandler(t *testing.T) {
 713	sendHi := make(chan bool, 1)
 714	writeErrors := make(chan os.Error, 1)
 715	sayHi := HandlerFunc(func(w ResponseWriter, r *Request) {
 716		<-sendHi
 717		_, werr := w.Write([]byte("hi"))
 718		writeErrors <- werr
 719	})
 720	timeout := make(chan int64, 1) // write to this to force timeouts
 721	ts := httptest.NewServer(NewTestTimeoutHandler(sayHi, timeout))
 722	defer ts.Close()
 723
 724	// Succeed without timing out:
 725	sendHi <- true
 726	res, err := Get(ts.URL)
 727	if err != nil {
 728		t.Error(err)
 729	}
 730	if g, e := res.StatusCode, StatusOK; g != e {
 731		t.Errorf("got res.StatusCode %d; expected %d", g, e)
 732	}
 733	body, _ := ioutil.ReadAll(res.Body)
 734	if g, e := string(body), "hi"; g != e {
 735		t.Errorf("got body %q; expected %q", g, e)
 736	}
 737	if g := <-writeErrors; g != nil {
 738		t.Errorf("got unexpected Write error on first request: %v", g)
 739	}
 740
 741	// Times out:
 742	timeout <- 1
 743	res, err = Get(ts.URL)
 744	if err != nil {
 745		t.Error(err)
 746	}
 747	if g, e := res.StatusCode, StatusServiceUnavailable; g != e {
 748		t.Errorf("got res.StatusCode %d; expected %d", g, e)
 749	}
 750	body, _ = ioutil.ReadAll(res.Body)
 751	if !strings.Contains(string(body), "<title>Timeout</title>") {
 752		t.Errorf("expected timeout body; got %q", string(body))
 753	}
 754
 755	// Now make the previously-timed out handler speak again,
 756	// which verifies the panic is handled:
 757	sendHi <- true
 758	if g, e := <-writeErrors, ErrHandlerTimeout; g != e {
 759		t.Errorf("expected Write error of %v; got %v", e, g)
 760	}
 761}
 762
 763// Verifies we don't path.Clean() on the wrong parts in redirects.
 764func TestRedirectMunging(t *testing.T) {
 765	req, _ := NewRequest("GET", "http://example.com/", nil)
 766
 767	resp := httptest.NewRecorder()
 768	Redirect(resp, req, "/foo?next=http://bar.com/", 302)
 769	if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e {
 770		t.Errorf("Location header was %q; want %q", g, e)
 771	}
 772
 773	resp = httptest.NewRecorder()
 774	Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302)
 775	if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e {
 776		t.Errorf("Location header was %q; want %q", g, e)
 777	}
 778}
 779
 780// TestZeroLengthPostAndResponse exercises an optimization done by the Transport:
 781// when there is no body (either because the method doesn't permit a body, or an
 782// explicit Content-Length of zero is present), then the transport can re-use the
 783// connection immediately. But when it re-uses the connection, it typically closes
 784// the previous request's body, which is not optimal for zero-lengthed bodies,
 785// as the client would then see http.ErrBodyReadAfterClose and not 0, os.EOF.
 786func TestZeroLengthPostAndResponse(t *testing.T) {
 787	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
 788		all, err := ioutil.ReadAll(r.Body)
 789		if err != nil {
 790			t.Fatalf("handler ReadAll: %v", err)
 791		}
 792		if len(all) != 0 {
 793			t.Errorf("handler got %d bytes; expected 0", len(all))
 794		}
 795		rw.Header().Set("Content-Length", "0")
 796	}))
 797	defer ts.Close()
 798
 799	req, err := NewRequest("POST", ts.URL, strings.NewReader(""))
 800	if err != nil {
 801		t.Fatal(err)
 802	}
 803	req.ContentLength = 0
 804
 805	var resp [5]*Response
 806	for i := range resp {
 807		resp[i], err = DefaultClient.Do(req)
 808		if err != nil {
 809			t.Fatalf("client post #%d: %v", i, err)
 810		}
 811	}
 812
 813	for i := range resp {
 814		all, err := ioutil.ReadAll(resp[i].Body)
 815		if err != nil {
 816			t.Fatalf("req #%d: client ReadAll: %v", i, err)
 817		}
 818		if len(all) != 0 {
 819			t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all))
 820		}
 821	}
 822}
 823
 824func TestHandlerPanic(t *testing.T) {
 825	// Unlike the other tests that set the log output to ioutil.Discard
 826	// to quiet the output, this test uses a pipe.  The pipe serves three
 827	// purposes:
 828	//
 829	//   1) The log.Print from the http server (generated by the caught
 830	//      panic) will go to the pipe instead of stderr, making the
 831	//      output quiet.
 832	//
 833	//   2) We read from the pipe to verify that the handler
 834	//      actually caught the panic and logged something.
 835	//
 836	//   3) The blocking Read call prevents this TestHandlerPanic
 837	//      function from exiting before the HTTP server handler
 838	//      finishes crashing. If this text function exited too
 839	//      early (and its defer log.SetOutput(os.Stderr) ran),
 840	//      then the crash output could spill into the next test.
 841	pr, pw := io.Pipe()
 842	log.SetOutput(pw)
 843	defer log.SetOutput(os.Stderr)
 844
 845	ts := httptest.NewServer(HandlerFunc(func(ResponseWriter, *Request) {
 846		panic("intentional death for testing")
 847	}))
 848	defer ts.Close()
 849	_, err := Get(ts.URL)
 850	if err == nil {
 851		t.Logf("expected an error")
 852	}
 853
 854	// Do a blocking read on the log output pipe so its logging
 855	// doesn't bleed into the next test.  But wait only 5 seconds
 856	// for it.
 857	done := make(chan bool)
 858	go func() {
 859		buf := make([]byte, 1024)
 860		_, err := pr.Read(buf)
 861		pr.Close()
 862		if err != nil {
 863			t.Fatal(err)
 864		}
 865		done <- true
 866	}()
 867	select {
 868	case <-done:
 869		return
 870	case <-time.After(5e9):
 871		t.Fatal("expected server handler to log an error")
 872	}
 873}
 874
 875func TestNoDate(t *testing.T) {
 876	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 877		w.Header()["Date"] = nil
 878	}))
 879	defer ts.Close()
 880	res, err := Get(ts.URL)
 881	if err != nil {
 882		t.Fatal(err)
 883	}
 884	_, present := res.Header["Date"]
 885	if present {
 886		t.Fatalf("Expected no Date header; got %v", res.Header["Date"])
 887	}
 888}
 889
 890func TestStripPrefix(t *testing.T) {
 891	h := HandlerFunc(func(w ResponseWriter, r *Request) {
 892		w.Header().Set("X-Path", r.URL.Path)
 893	})
 894	ts := httptest.NewServer(StripPrefix("/foo", h))
 895	defer ts.Close()
 896
 897	res, err := Get(ts.URL + "/foo/bar")
 898	if err != nil {
 899		t.Fatal(err)
 900	}
 901	if g, e := res.Header.Get("X-Path"), "/bar"; g != e {
 902		t.Errorf("test 1: got %s, want %s", g, e)
 903	}
 904
 905	res, err = Get(ts.URL + "/bar")
 906	if err != nil {
 907		t.Fatal(err)
 908	}
 909	if g, e := res.StatusCode, 404; g != e {
 910		t.Errorf("test 2: got status %v, want %v", g, e)
 911	}
 912}
 913
 914func TestRequestLimit(t *testing.T) {
 915	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 916		t.Fatalf("didn't expect to get request in Handler")
 917	}))
 918	defer ts.Close()
 919	req, _ := NewRequest("GET", ts.URL, nil)
 920	var bytesPerHeader = len("header12345: val12345\r\n")
 921	for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ {
 922		req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i))
 923	}
 924	res, err := DefaultClient.Do(req)
 925	if err != nil {
 926		// Some HTTP clients may fail on this undefined behavior (server replying and
 927		// closing the connection while the request is still being written), but
 928		// we do support it (at least currently), so we expect a response below.
 929		t.Fatalf("Do: %v", err)
 930	}
 931	if res.StatusCode != 413 {
 932		t.Fatalf("expected 413 response status; got: %d %s", res.StatusCode, res.Status)
 933	}
 934}
 935
 936type neverEnding byte
 937
 938func (b neverEnding) Read(p []byte) (n int, err os.Error) {
 939	for i := range p {
 940		p[i] = byte(b)
 941	}
 942	return len(p), nil
 943}
 944
 945type countReader struct {
 946	r io.Reader
 947	n *int64
 948}
 949
 950func (cr countReader) Read(p []byte) (n int, err os.Error) {
 951	n, err = cr.r.Read(p)
 952	*cr.n += int64(n)
 953	return
 954}
 955
 956func TestRequestBodyLimit(t *testing.T) {
 957	const limit = 1 << 20
 958	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
 959		r.Body = MaxBytesReader(w, r.Body, limit)
 960		n, err := io.Copy(ioutil.Discard, r.Body)
 961		if err == nil {
 962			t.Errorf("expected error from io.Copy")
 963		}
 964		if n != limit {
 965			t.Errorf("io.Copy = %d, want %d", n, limit)
 966		}
 967	}))
 968	defer ts.Close()
 969
 970	nWritten := int64(0)
 971	req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), &nWritten}, limit*200))
 972
 973	// Send the POST, but don't care it succeeds or not.  The
 974	// remote side is going to reply and then close the TCP
 975	// connection, and HTTP doesn't really define if that's
 976	// allowed or not.  Some HTTP clients will get the response
 977	// and some (like ours, currently) will complain that the
 978	// request write failed, without reading the response.
 979	//
 980	// But that's okay, since what we're really testing is that
 981	// the remote side hung up on us before we wrote too much.
 982	_, _ = DefaultClient.Do(req)
 983
 984	if nWritten > limit*100 {
 985		t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d",
 986			limit, nWritten)
 987	}
 988}
 989
 990// TestClientWriteShutdown tests that if the client shuts down the write
 991// side of their TCP connection, the server doesn't send a 400 Bad Request.
 992func TestClientWriteShutdown(t *testing.T) {
 993	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
 994	defer ts.Close()
 995	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
 996	if err != nil {
 997		t.Fatalf("Dial: %v", err)
 998	}
 999	err = conn.(*net.TCPConn).CloseWrite()
1000	if err != nil {
1001		t.Fatalf("Dial: %v", err)
1002	}
1003	donec := make(chan bool)
1004	go func() {
1005		defer close(donec)
1006		bs, err := ioutil.ReadAll(conn)
1007		if err != nil {
1008			t.Fatalf("ReadAll: %v", err)
1009		}
1010		got := string(bs)
1011		if got != "" {
1012			t.Errorf("read %q from server; want nothing", got)
1013		}
1014	}()
1015	select {
1016	case <-donec:
1017	case <-time.After(10e9):
1018		t.Fatalf("timeout")
1019	}
1020}
1021
1022type errorListener struct {
1023	errs []os.Error
1024}
1025
1026func (l *errorListener) Accept() (c net.Conn, err os.Error) {
1027	if len(l.errs) == 0 {
1028		return nil, os.EOF
1029	}
1030	err = l.errs[0]
1031	l.errs = l.errs[1:]
1032	return
1033}
1034
1035func (l *errorListener) Close() os.Error {
1036	return nil
1037}
1038
1039func (l *errorListener) Addr() net.Addr {
1040	return dummyAddr("test-address")
1041}
1042
1043func TestAcceptMaxFds(t *testing.T) {
1044	log.SetOutput(ioutil.Discard) // is noisy otherwise
1045	defer log.SetOutput(os.Stderr)
1046
1047	ln := &errorListener{[]os.Error{
1048		&net.OpError{
1049			Op:    "accept",
1050			Error: os.Errno(syscall.EMFILE),
1051		}}}
1052	err := Serve(ln, HandlerFunc(HandlerFunc(func(ResponseWriter, *Request) {})))
1053	if err != os.EOF {
1054		t.Errorf("got error %v, want EOF", err)
1055	}
1056}
1057
1058func BenchmarkClientServer(b *testing.B) {
1059	b.StopTimer()
1060	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
1061		fmt.Fprintf(rw, "Hello world.\n")
1062	}))
1063	defer ts.Close()
1064	b.StartTimer()
1065
1066	for i := 0; i < b.N; i++ {
1067		res, err := Get(ts.URL)
1068		if err != nil {
1069			panic("Get: " + err.String())
1070		}
1071		all, err := ioutil.ReadAll(res.Body)
1072		if err != nil {
1073			panic("ReadAll: " + err.String())
1074		}
1075		body := string(all)
1076		if body != "Hello world.\n" {
1077			panic("Got body: " + body)
1078		}
1079	}
1080
1081	b.StopTimer()
1082}