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.
5 // End-to-end serving tests
29 type oneConnListener struct {
33 func (l *oneConnListener) Accept() (c net.Conn, err os.Error) {
44 func (l *oneConnListener) Close() os.Error {
48 func (l *oneConnListener) Addr() net.Addr {
49 return dummyAddr("test-address")
52 func (a dummyAddr) Network() string {
56 func (a dummyAddr) String() string {
60 type testConn struct {
65 func (c *testConn) Read(b []byte) (int, os.Error) {
66 return c.readBuf.Read(b)
69 func (c *testConn) Write(b []byte) (int, os.Error) {
70 return c.writeBuf.Write(b)
73 func (c *testConn) Close() os.Error {
77 func (c *testConn) LocalAddr() net.Addr {
78 return dummyAddr("local-addr")
81 func (c *testConn) RemoteAddr() net.Addr {
82 return dummyAddr("remote-addr")
85 func (c *testConn) SetTimeout(nsec int64) os.Error {
89 func (c *testConn) SetReadTimeout(nsec int64) os.Error {
93 func (c *testConn) SetWriteTimeout(nsec int64) os.Error {
97 func TestConsumingBodyOnNextConn(t *testing.T) {
99 for i := 0; i < 2; i++ {
100 conn.readBuf.Write([]byte(
101 "POST / HTTP/1.1\r\n" +
103 "Content-Length: 11\r\n" +
109 ch := make(chan *Request)
110 servech := make(chan os.Error)
111 listener := &oneConnListener{conn}
112 handler := func(res ResponseWriter, req *Request) {
118 servech <- Serve(listener, HandlerFunc(handler))
124 t.Fatal("Got nil first request.")
126 if req.Method != "POST" {
127 t.Errorf("For request #1's method, got %q; expected %q",
133 t.Fatal("Got nil first request.")
135 if req.Method != "POST" {
136 t.Errorf("For request #2's method, got %q; expected %q",
140 if serveerr := <-servech; serveerr != os.EOF {
141 t.Errorf("Serve returned %q; expected EOF", serveerr)
145 type stringHandler string
147 func (s stringHandler) ServeHTTP(w ResponseWriter, r *Request) {
148 w.Header().Set("Result", string(s))
151 var handlers = []struct {
156 {"/someDir/", "someDir"},
157 {"someHost.com/someDir/", "someHost.com/someDir"},
160 var vtests = []struct {
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"},
171 func TestHostHandlers(t *testing.T) {
172 for _, h := range handlers {
173 Handle(h.pattern, stringHandler(h.msg))
175 ts := httptest.NewServer(nil)
178 conn, err := net.Dial("tcp", ts.Listener.Addr().String())
183 cc := NewClientConn(conn, nil)
184 for _, vt := range vtests {
187 if req.URL, err = url.Parse(vt.url); err != nil {
188 t.Errorf("cannot parse url: %v", err)
191 if err := cc.Write(&req); err != nil {
192 t.Errorf("writing request: %v", err)
195 r, err := cc.Read(&req)
197 t.Errorf("reading response: %v", err)
200 s := r.Header.Get("Result")
201 if s != vt.expected {
202 t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
207 // Tests for http://code.google.com/p/go/issues/detail?id=900
208 func 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")))
216 resp := httptest.NewRecorder()
218 mux.ServeHTTP(resp, req)
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)
225 if code, expected := resp.Code, StatusMovedPermanently; code != expected {
226 t.Errorf("Expected response code of StatusMovedPermanently; got %d", code)
232 func TestServerTimeouts(t *testing.T) {
233 // TODO(bradfitz): convert this to use httptest.Server
234 l, err := net.Listen("tcp", "127.0.0.1:0")
236 t.Fatalf("listen error: %v", err)
238 addr, _ := l.Addr().(*net.TCPAddr)
241 handler := HandlerFunc(func(res ResponseWriter, req *Request) {
243 fmt.Fprintf(res, "req=%d", reqNum)
246 const second = 1000000000 /* nanos */
247 server := &Server{Handler: handler, ReadTimeout: 0.25 * second, WriteTimeout: 0.25 * second}
250 url := fmt.Sprintf("http://%s/", addr)
252 // Hit the HTTP server successfully.
253 tr := &Transport{DisableKeepAlives: true} // they interfere with this test
254 c := &Client{Transport: tr}
257 t.Fatalf("http Get #1: %v", err)
259 got, _ := ioutil.ReadAll(r.Body)
261 if string(got) != expected {
262 t.Errorf("Unexpected response for request #1; got %q; expected %q",
263 string(got), expected)
266 // Slow client that should timeout.
267 t1 := time.Nanoseconds()
268 conn, err := net.Dial("tcp", addr.String())
270 t.Fatalf("Dial: %v", err)
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)
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)
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")
287 t.Fatalf("http Get #2: %v", err)
289 got, _ = ioutil.ReadAll(r.Body)
291 if string(got) != expected {
292 t.Errorf("Get #2 got %q, want %q", string(got), expected)
298 // TestIdentityResponse verifies that a handler can unset
299 func 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"))
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)
309 case req.FormValue("underwrite") == "1":
310 rw.Header().Set("Content-Length", "500")
311 rw.Write([]byte("too short"))
313 rw.Write([]byte("foo"))
317 ts := httptest.NewServer(handler)
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
324 for _, te := range []string{"", "identity"} {
325 url := ts.URL + "/?te=" + te
328 t.Fatalf("error with Get of %s: %v", url, err)
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)
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)
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)
343 // Verify that ErrContentLength is returned
344 url := ts.URL + "/?overwrite=1"
347 t.Fatalf("error with Get of %s: %v", url, err)
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())
353 t.Fatalf("error dialing: %v", err)
355 _, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n\r\n"))
357 t.Fatalf("error writing: %v", err)
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.")
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))
373 func testTcpConnectionCloses(t *testing.T, req string, h Handler) {
374 s := httptest.NewServer(h)
377 conn, err := net.Dial("tcp", s.Listener.Addr().String())
379 t.Fatal("dial error:", err)
383 _, err = fmt.Fprint(conn, req)
385 t.Fatal("print error:", err)
388 r := bufio.NewReader(conn)
389 _, err = ReadResponse(r, &Request{Method: "GET"})
391 t.Fatal("ReadResponse error:", err)
394 success := make(chan bool)
397 case <-time.After(5e9):
398 t.Fatal("body not closed after 5s")
403 _, err = ioutil.ReadAll(r)
405 t.Fatal("read error:", err)
411 // TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive.
412 func 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")
418 // TestHandlersCanSetConnectionClose verifies that handlers can force a connection to close,
419 // even for HTTP/1.1 requests.
420 func 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")
426 func 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")
432 func TestSetsRemoteAddr(t *testing.T) {
433 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
434 fmt.Fprintf(w, "%s", r.RemoteAddr)
438 res, err := Get(ts.URL)
440 t.Fatalf("Get error: %v", err)
442 body, err := ioutil.ReadAll(res.Body)
444 t.Fatalf("ReadAll error: %v", err)
447 if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") {
448 t.Fatalf("Expected local addr; got %q", ip)
452 func TestChunkedResponseHeaders(t *testing.T) {
453 log.SetOutput(ioutil.Discard) // is noisy otherwise
454 defer log.SetOutput(os.Stderr)
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.")
462 res, err := Get(ts.URL)
464 t.Fatalf("Get error: %v", err)
466 if g, e := res.ContentLength, int64(-1); g != e {
467 t.Errorf("expected ContentLength of %d; got %d", e, g)
469 if g, e := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(g, e) {
470 t.Errorf("expected TransferEncoding of %v; got %v", e, g)
472 if _, haveCL := res.Header["Content-Length"]; haveCL {
473 t.Errorf("Unexpected Content-Length")
477 // Test304Responses verifies that 304s don't declare that they're
478 // chunking in their response headers and aren't allowed to produce
480 func 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)
489 res, err := Get(ts.URL)
493 if len(res.TransferEncoding) > 0 {
494 t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
496 body, err := ioutil.ReadAll(res.Body)
501 t.Errorf("got unexpected body %q", string(body))
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.
508 func 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)
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)
522 res, err := Head(ts.URL)
526 if len(res.TransferEncoding) > 0 {
527 t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
529 body, err := ioutil.ReadAll(res.Body)
534 t.Errorf("got unexpected body %q", string(body))
538 func TestTLSServer(t *testing.T) {
539 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
541 w.Header().Set("X-TLS-Set", "true")
542 if r.TLS.HandshakeComplete {
543 w.Header().Set("X-TLS-HandshakeComplete", "true")
548 if !strings.HasPrefix(ts.URL, "https://") {
549 t.Fatalf("expected test TLS server to start with https://, got %q", ts.URL)
551 res, err := Get(ts.URL)
556 t.Fatalf("got nil Response")
558 defer res.Body.Close()
559 if res.Header.Get("X-TLS-Set") != "true" {
560 t.Errorf("expected X-TLS-Set response header")
562 if res.Header.Get("X-TLS-HandshakeComplete") != "true" {
563 t.Errorf("expected X-TLS-HandshakeComplete header")
567 type serverExpectTest struct {
568 contentLength int // of request body
569 expectation string // e.g. "100-continue"
570 readBody bool // whether handler should read the body (if false, sends StatusUnauthorized)
571 expectedResponse string // expected substring in first line of http response
574 var serverExpectTests = []serverExpectTest{
575 // Normal 100-continues, case-insensitive.
576 {100, "100-continue", true, "100 Continue"},
577 {100, "100-cOntInUE", true, "100 Continue"},
580 {100, "", true, "200 OK"},
582 // 100-continue but requesting client to deny us,
583 // so it never reads the body.
584 {100, "100-continue", false, "401 Unauthorized"},
585 // Likewise without 100-continue:
586 {100, "", false, "401 Unauthorized"},
588 // Non-standard expectations are failures
589 {0, "a-pony", false, "417 Expectation Failed"},
591 // Expect-100 requested but no body
592 {0, "100-continue", true, "400 Bad Request"},
595 // Tests that the server responds to the "Expect" request header
597 func TestServerExpect(t *testing.T) {
598 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
599 // Note using r.FormValue("readbody") because for POST
600 // requests that would read from r.Body, which we only
601 // conditionally want to do.
602 if strings.Contains(r.URL.RawPath, "readbody=true") {
603 ioutil.ReadAll(r.Body)
604 w.Write([]byte("Hi"))
606 w.WriteHeader(StatusUnauthorized)
611 runTest := func(test serverExpectTest) {
612 conn, err := net.Dial("tcp", ts.Listener.Addr().String())
614 t.Fatalf("Dial: %v", err)
617 sendf := func(format string, args ...interface{}) {
618 _, err := fmt.Fprintf(conn, format, args...)
620 t.Fatalf("On test %#v, error writing %q: %v", test, format, err)
624 sendf("POST /?readbody=%v HTTP/1.1\r\n"+
625 "Connection: close\r\n"+
626 "Content-Length: %d\r\n"+
627 "Expect: %s\r\nHost: foo\r\n\r\n",
628 test.readBody, test.contentLength, test.expectation)
629 if test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue" {
630 body := strings.Repeat("A", test.contentLength)
634 bufr := bufio.NewReader(conn)
635 line, err := bufr.ReadString('\n')
637 t.Fatalf("ReadString: %v", err)
639 if !strings.Contains(line, test.expectedResponse) {
640 t.Errorf("for test %#v got first line=%q", test, line)
644 for _, test := range serverExpectTests {
649 func TestServerConsumesRequestBody(t *testing.T) {
650 conn := new(testConn)
651 body := strings.Repeat("x", 1<<20)
652 conn.readBuf.Write([]byte(fmt.Sprintf(
653 "POST / HTTP/1.1\r\n"+
655 "Content-Length: %d\r\n"+
657 conn.readBuf.Write([]byte(body))
659 done := make(chan bool)
661 ls := &oneConnListener{conn}
662 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
663 if conn.readBuf.Len() < len(body)/2 {
664 t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len())
667 if g, e := conn.readBuf.Len(), 0; g != e {
668 t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e)
675 func TestTimeoutHandler(t *testing.T) {
676 sendHi := make(chan bool, 1)
677 writeErrors := make(chan os.Error, 1)
678 sayHi := HandlerFunc(func(w ResponseWriter, r *Request) {
680 _, werr := w.Write([]byte("hi"))
683 timeout := make(chan int64, 1) // write to this to force timeouts
684 ts := httptest.NewServer(NewTestTimeoutHandler(sayHi, timeout))
687 // Succeed without timing out:
689 res, err := Get(ts.URL)
693 if g, e := res.StatusCode, StatusOK; g != e {
694 t.Errorf("got res.StatusCode %d; expected %d", g, e)
696 body, _ := ioutil.ReadAll(res.Body)
697 if g, e := string(body), "hi"; g != e {
698 t.Errorf("got body %q; expected %q", g, e)
700 if g := <-writeErrors; g != nil {
701 t.Errorf("got unexpected Write error on first request: %v", g)
706 res, err = Get(ts.URL)
710 if g, e := res.StatusCode, StatusServiceUnavailable; g != e {
711 t.Errorf("got res.StatusCode %d; expected %d", g, e)
713 body, _ = ioutil.ReadAll(res.Body)
714 if !strings.Contains(string(body), "<title>Timeout</title>") {
715 t.Errorf("expected timeout body; got %q", string(body))
718 // Now make the previously-timed out handler speak again,
719 // which verifies the panic is handled:
721 if g, e := <-writeErrors, ErrHandlerTimeout; g != e {
722 t.Errorf("expected Write error of %v; got %v", e, g)
726 // Verifies we don't path.Clean() on the wrong parts in redirects.
727 func TestRedirectMunging(t *testing.T) {
728 req, _ := NewRequest("GET", "http://example.com/", nil)
730 resp := httptest.NewRecorder()
731 Redirect(resp, req, "/foo?next=http://bar.com/", 302)
732 if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e {
733 t.Errorf("Location header was %q; want %q", g, e)
736 resp = httptest.NewRecorder()
737 Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302)
738 if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e {
739 t.Errorf("Location header was %q; want %q", g, e)
743 // TestZeroLengthPostAndResponse exercises an optimization done by the Transport:
744 // when there is no body (either because the method doesn't permit a body, or an
745 // explicit Content-Length of zero is present), then the transport can re-use the
746 // connection immediately. But when it re-uses the connection, it typically closes
747 // the previous request's body, which is not optimal for zero-lengthed bodies,
748 // as the client would then see http.ErrBodyReadAfterClose and not 0, os.EOF.
749 func TestZeroLengthPostAndResponse(t *testing.T) {
750 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
751 all, err := ioutil.ReadAll(r.Body)
753 t.Fatalf("handler ReadAll: %v", err)
756 t.Errorf("handler got %d bytes; expected 0", len(all))
758 rw.Header().Set("Content-Length", "0")
762 req, err := NewRequest("POST", ts.URL, strings.NewReader(""))
766 req.ContentLength = 0
768 var resp [5]*Response
769 for i := range resp {
770 resp[i], err = DefaultClient.Do(req)
772 t.Fatalf("client post #%d: %v", i, err)
776 for i := range resp {
777 all, err := ioutil.ReadAll(resp[i].Body)
779 t.Fatalf("req #%d: client ReadAll: %v", i, err)
782 t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all))
787 func TestHandlerPanic(t *testing.T) {
788 // Unlike the other tests that set the log output to ioutil.Discard
789 // to quiet the output, this test uses a pipe. The pipe serves three
792 // 1) The log.Print from the http server (generated by the caught
793 // panic) will go to the pipe instead of stderr, making the
796 // 2) We read from the pipe to verify that the handler
797 // actually caught the panic and logged something.
799 // 3) The blocking Read call prevents this TestHandlerPanic
800 // function from exiting before the HTTP server handler
801 // finishes crashing. If this text function exited too
802 // early (and its defer log.SetOutput(os.Stderr) ran),
803 // then the crash output could spill into the next test.
806 defer log.SetOutput(os.Stderr)
808 ts := httptest.NewServer(HandlerFunc(func(ResponseWriter, *Request) {
809 panic("intentional death for testing")
812 _, err := Get(ts.URL)
814 t.Logf("expected an error")
817 // Do a blocking read on the log output pipe so its logging
818 // doesn't bleed into the next test. But wait only 5 seconds
820 done := make(chan bool)
822 buf := make([]byte, 1024)
823 _, err := pr.Read(buf)
833 case <-time.After(5e9):
834 t.Fatal("expected server handler to log an error")
838 func TestNoDate(t *testing.T) {
839 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
840 w.Header()["Date"] = nil
843 res, err := Get(ts.URL)
847 _, present := res.Header["Date"]
849 t.Fatalf("Expected no Date header; got %v", res.Header["Date"])
853 func TestStripPrefix(t *testing.T) {
854 h := HandlerFunc(func(w ResponseWriter, r *Request) {
855 w.Header().Set("X-Path", r.URL.Path)
857 ts := httptest.NewServer(StripPrefix("/foo", h))
860 res, err := Get(ts.URL + "/foo/bar")
864 if g, e := res.Header.Get("X-Path"), "/bar"; g != e {
865 t.Errorf("test 1: got %s, want %s", g, e)
868 res, err = Get(ts.URL + "/bar")
872 if g, e := res.StatusCode, 404; g != e {
873 t.Errorf("test 2: got status %v, want %v", g, e)
877 func TestRequestLimit(t *testing.T) {
878 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
879 t.Fatalf("didn't expect to get request in Handler")
882 req, _ := NewRequest("GET", ts.URL, nil)
883 var bytesPerHeader = len("header12345: val12345\r\n")
884 for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ {
885 req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i))
887 res, err := DefaultClient.Do(req)
889 // Some HTTP clients may fail on this undefined behavior (server replying and
890 // closing the connection while the request is still being written), but
891 // we do support it (at least currently), so we expect a response below.
892 t.Fatalf("Do: %v", err)
894 if res.StatusCode != 400 {
895 t.Fatalf("expected 400 response status; got: %d %s", res.StatusCode, res.Status)
899 type errorListener struct {
903 func (l *errorListener) Accept() (c net.Conn, err os.Error) {
904 if len(l.errs) == 0 {
912 func (l *errorListener) Close() os.Error {
916 func (l *errorListener) Addr() net.Addr {
917 return dummyAddr("test-address")
920 func TestAcceptMaxFds(t *testing.T) {
921 log.SetOutput(ioutil.Discard) // is noisy otherwise
922 defer log.SetOutput(os.Stderr)
924 ln := &errorListener{[]os.Error{
927 Error: os.Errno(syscall.EMFILE),
929 err := Serve(ln, HandlerFunc(HandlerFunc(func(ResponseWriter, *Request) {})))
931 t.Errorf("got error %v, want EOF", err)
935 func BenchmarkClientServer(b *testing.B) {
937 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
938 fmt.Fprintf(rw, "Hello world.\n")
943 for i := 0; i < b.N; i++ {
944 res, err := Get(ts.URL)
946 panic("Get: " + err.String())
948 all, err := ioutil.ReadAll(res.Body)
950 panic("ReadAll: " + err.String())
953 if body != "Hello world.\n" {
954 panic("Got body: " + body)