Skip to content

Commit

Permalink
net/http: remove some stderr log spam
Browse files Browse the repository at this point in the history
This changes rawResponse to install a logger before
Serve()ing and makes the log output available to
tests.

Updates #24831
Updates CL 89275

Change-Id: I0fb636a35b05959ca9978d5d8552f38b7cf8e8b5
Reviewed-on: https://go-review.googlesource.com/106756
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
mikesamuel authored and bradfitz committed Apr 16, 2018
1 parent 29eca06 commit d697600
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/net/http/serve_test.go
Expand Up @@ -134,14 +134,15 @@ func reqBytes(req string) []byte {
}

type handlerTest struct {
logbuf bytes.Buffer
handler Handler
}

func newHandlerTest(h Handler) handlerTest {
return handlerTest{h}
return handlerTest{handler: h}
}

func (ht handlerTest) rawResponse(req string) string {
func (ht *handlerTest) rawResponse(req string) string {
reqb := reqBytes(req)
var output bytes.Buffer
conn := &rwTestConn{
Expand All @@ -150,7 +151,11 @@ func (ht handlerTest) rawResponse(req string) string {
closec: make(chan bool, 1),
}
ln := &oneConnListener{conn: conn}
go Serve(ln, ht.handler)
srv := &Server{
ErrorLog: log.New(&ht.logbuf, "", 0),
Handler: ht.handler,
}
go srv.Serve(ln)
<-conn.closec
return output.String()
}
Expand Down Expand Up @@ -3399,14 +3404,14 @@ func TestHeaderToWire(t *testing.T) {
tests := []struct {
name string
handler func(ResponseWriter, *Request)
check func(output string) error
check func(got, logs string) error
}{
{
name: "write without Header",
handler: func(rw ResponseWriter, r *Request) {
rw.Write([]byte("hello world"))
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Length:") {
return errors.New("no content-length")
}
Expand All @@ -3424,7 +3429,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Write([]byte("hello world"))
h.Set("Too-Late", "bogus")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Length:") {
return errors.New("no content-length")
}
Expand All @@ -3443,7 +3448,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Write([]byte("hello world"))
rw.Header().Set("Too-Late", "Write already wrote headers")
},
check: func(got string) error {
check: func(got, logs string) error {
if strings.Contains(got, "Too-Late") {
return errors.New("header appeared from after WriteHeader")
}
Expand All @@ -3457,7 +3462,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Write([]byte("post-flush"))
rw.Header().Set("Too-Late", "Write already wrote headers")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Transfer-Encoding: chunked") {
return errors.New("not chunked")
}
Expand All @@ -3475,7 +3480,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Write([]byte("post-flush"))
rw.Header().Set("Too-Late", "Write already wrote headers")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Transfer-Encoding: chunked") {
return errors.New("not chunked")
}
Expand All @@ -3494,7 +3499,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Write([]byte("<html><head></head><body>some html</body></html>"))
rw.Header().Set("Content-Type", "x/wrong")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Type: text/html") {
return errors.New("wrong content-type; want html")
}
Expand All @@ -3507,7 +3512,7 @@ func TestHeaderToWire(t *testing.T) {
rw.Header().Set("Content-Type", "some/type")
rw.Write([]byte("<html><head></head><body>some html</body></html>"))
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Type: some/type") {
return errors.New("wrong content-type; want html")
}
Expand All @@ -3518,7 +3523,7 @@ func TestHeaderToWire(t *testing.T) {
name: "empty handler",
handler: func(rw ResponseWriter, r *Request) {
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Length: 0") {
return errors.New("want 0 content-length")
}
Expand All @@ -3530,7 +3535,7 @@ func TestHeaderToWire(t *testing.T) {
handler: func(rw ResponseWriter, r *Request) {
rw.Header().Set("Some-Header", "some-value")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Some-Header") {
return errors.New("didn't get header")
}
Expand All @@ -3543,7 +3548,7 @@ func TestHeaderToWire(t *testing.T) {
rw.WriteHeader(404)
rw.Header().Set("Too-Late", "some-value")
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "404") {
return errors.New("wrong status")
}
Expand All @@ -3560,22 +3565,26 @@ func TestHeaderToWire(t *testing.T) {
rw.WriteHeader(200)
rw.Write([]byte("<!doctype html>\n<html><head></head><body>some html</body></html>"))
},
check: func(got string) error {
check: func(got, logs string) error {
if !strings.Contains(got, "Content-Type: application/octet-stream\r\n") {
return errors.New("Output should have an innocuous content-type")
}
if strings.Contains(got, "text/html") {
return errors.New("Output should not have a guess")
}
if !strings.Contains(logs, "X-Content-Type-Options:nosniff but no Content-Type") {
return errors.New("Expected log message")
}
return nil
},
},
}
for _, tc := range tests {
ht := newHandlerTest(HandlerFunc(tc.handler))
got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org")
if err := tc.check(got); err != nil {
t.Errorf("%s: %v\nGot response:\n%s", tc.name, err, got)
logs := ht.logbuf.String()
if err := tc.check(got, logs); err != nil {
t.Errorf("%s: %v\nGot response:\n%s\n\n%s", tc.name, err, got, logs)
}
}
}
Expand Down

0 comments on commit d697600

Please sign in to comment.