1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

net/http: remove some stderr log spam

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>
This commit is contained in:
Mike Samuel 2018-04-13 12:46:49 -04:00 committed by Brad Fitzpatrick
parent 29eca06ff0
commit d69760064f

View File

@ -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{
@ -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()
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -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")
}
@ -3560,13 +3565,16 @@ 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
},
},
@ -3574,8 +3582,9 @@ func TestHeaderToWire(t *testing.T) {
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)
}
}
}