diff options
Diffstat (limited to 'src/pkg/http/serve_test.go')
-rw-r--r-- | src/pkg/http/serve_test.go | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/src/pkg/http/serve_test.go b/src/pkg/http/serve_test.go index 55a9cbf70..ac0403345 100644 --- a/src/pkg/http/serve_test.go +++ b/src/pkg/http/serve_test.go @@ -22,6 +22,7 @@ import ( "syscall" "testing" "time" + "url" ) type dummyAddr string @@ -110,7 +111,6 @@ func TestConsumingBodyOnNextConn(t *testing.T) { listener := &oneConnListener{conn} handler := func(res ResponseWriter, req *Request) { reqNum++ - t.Logf("Got request #%d: %v", reqNum, req) ch <- req } @@ -119,7 +119,6 @@ func TestConsumingBodyOnNextConn(t *testing.T) { }() var req *Request - t.Log("Waiting for first request.") req = <-ch if req == nil { t.Fatal("Got nil first request.") @@ -129,7 +128,6 @@ func TestConsumingBodyOnNextConn(t *testing.T) { req.Method, "POST") } - t.Log("Waiting for second request.") req = <-ch if req == nil { t.Fatal("Got nil first request.") @@ -139,7 +137,6 @@ func TestConsumingBodyOnNextConn(t *testing.T) { req.Method, "POST") } - t.Log("Waiting for EOF.") if serveerr := <-servech; serveerr != os.EOF { t.Errorf("Serve returned %q; expected EOF", serveerr) } @@ -187,7 +184,7 @@ func TestHostHandlers(t *testing.T) { for _, vt := range vtests { var r *Response var req Request - if req.URL, err = ParseURL(vt.url); err != nil { + if req.URL, err = url.Parse(vt.url); err != nil { t.Errorf("cannot parse url: %v", err) continue } @@ -788,7 +785,24 @@ func TestZeroLengthPostAndResponse(t *testing.T) { } func TestHandlerPanic(t *testing.T) { - log.SetOutput(ioutil.Discard) // is noisy otherwise + // Unlike the other tests that set the log output to ioutil.Discard + // to quiet the output, this test uses a pipe. The pipe serves three + // purposes: + // + // 1) The log.Print from the http server (generated by the caught + // panic) will go to the pipe instead of stderr, making the + // output quiet. + // + // 2) We read from the pipe to verify that the handler + // actually caught the panic and logged something. + // + // 3) The blocking Read call prevents this TestHandlerPanic + // function from exiting before the HTTP server handler + // finishes crashing. If this text function exited too + // early (and its defer log.SetOutput(os.Stderr) ran), + // then the crash output could spill into the next test. + pr, pw := io.Pipe() + log.SetOutput(pw) defer log.SetOutput(os.Stderr) ts := httptest.NewServer(HandlerFunc(func(ResponseWriter, *Request) { @@ -799,6 +813,26 @@ func TestHandlerPanic(t *testing.T) { if err == nil { t.Logf("expected an error") } + + // Do a blocking read on the log output pipe so its logging + // doesn't bleed into the next test. But wait only 5 seconds + // for it. + done := make(chan bool) + go func() { + buf := make([]byte, 1024) + _, err := pr.Read(buf) + pr.Close() + if err != nil { + t.Fatal(err) + } + done <- true + }() + select { + case <-done: + return + case <-time.After(5e9): + t.Fatal("expected server handler to log an error") + } } func TestNoDate(t *testing.T) { @@ -840,6 +874,28 @@ func TestStripPrefix(t *testing.T) { } } +func TestRequestLimit(t *testing.T) { + ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { + t.Fatalf("didn't expect to get request in Handler") + })) + defer ts.Close() + req, _ := NewRequest("GET", ts.URL, nil) + var bytesPerHeader = len("header12345: val12345\r\n") + for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ { + req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i)) + } + res, err := DefaultClient.Do(req) + if err != nil { + // Some HTTP clients may fail on this undefined behavior (server replying and + // closing the connection while the request is still being written), but + // we do support it (at least currently), so we expect a response below. + t.Fatalf("Do: %v", err) + } + if res.StatusCode != 400 { + t.Fatalf("expected 400 response status; got: %d %s", res.StatusCode, res.Status) + } +} + type errorListener struct { errs []os.Error } |