Skip to content

Commit d697600

Browse files
mikesamuelbradfitz
authored andcommitted
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 <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 29eca06 commit d697600

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/net/http/serve_test.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,15 @@ func reqBytes(req string) []byte {
134134
}
135135

136136
type handlerTest struct {
137+
logbuf bytes.Buffer
137138
handler Handler
138139
}
139140

140141
func newHandlerTest(h Handler) handlerTest {
141-
return handlerTest{h}
142+
return handlerTest{handler: h}
142143
}
143144

144-
func (ht handlerTest) rawResponse(req string) string {
145+
func (ht *handlerTest) rawResponse(req string) string {
145146
reqb := reqBytes(req)
146147
var output bytes.Buffer
147148
conn := &rwTestConn{
@@ -150,7 +151,11 @@ func (ht handlerTest) rawResponse(req string) string {
150151
closec: make(chan bool, 1),
151152
}
152153
ln := &oneConnListener{conn: conn}
153-
go Serve(ln, ht.handler)
154+
srv := &Server{
155+
ErrorLog: log.New(&ht.logbuf, "", 0),
156+
Handler: ht.handler,
157+
}
158+
go srv.Serve(ln)
154159
<-conn.closec
155160
return output.String()
156161
}
@@ -3399,14 +3404,14 @@ func TestHeaderToWire(t *testing.T) {
33993404
tests := []struct {
34003405
name string
34013406
handler func(ResponseWriter, *Request)
3402-
check func(output string) error
3407+
check func(got, logs string) error
34033408
}{
34043409
{
34053410
name: "write without Header",
34063411
handler: func(rw ResponseWriter, r *Request) {
34073412
rw.Write([]byte("hello world"))
34083413
},
3409-
check: func(got string) error {
3414+
check: func(got, logs string) error {
34103415
if !strings.Contains(got, "Content-Length:") {
34113416
return errors.New("no content-length")
34123417
}
@@ -3424,7 +3429,7 @@ func TestHeaderToWire(t *testing.T) {
34243429
rw.Write([]byte("hello world"))
34253430
h.Set("Too-Late", "bogus")
34263431
},
3427-
check: func(got string) error {
3432+
check: func(got, logs string) error {
34283433
if !strings.Contains(got, "Content-Length:") {
34293434
return errors.New("no content-length")
34303435
}
@@ -3443,7 +3448,7 @@ func TestHeaderToWire(t *testing.T) {
34433448
rw.Write([]byte("hello world"))
34443449
rw.Header().Set("Too-Late", "Write already wrote headers")
34453450
},
3446-
check: func(got string) error {
3451+
check: func(got, logs string) error {
34473452
if strings.Contains(got, "Too-Late") {
34483453
return errors.New("header appeared from after WriteHeader")
34493454
}
@@ -3457,7 +3462,7 @@ func TestHeaderToWire(t *testing.T) {
34573462
rw.Write([]byte("post-flush"))
34583463
rw.Header().Set("Too-Late", "Write already wrote headers")
34593464
},
3460-
check: func(got string) error {
3465+
check: func(got, logs string) error {
34613466
if !strings.Contains(got, "Transfer-Encoding: chunked") {
34623467
return errors.New("not chunked")
34633468
}
@@ -3475,7 +3480,7 @@ func TestHeaderToWire(t *testing.T) {
34753480
rw.Write([]byte("post-flush"))
34763481
rw.Header().Set("Too-Late", "Write already wrote headers")
34773482
},
3478-
check: func(got string) error {
3483+
check: func(got, logs string) error {
34793484
if !strings.Contains(got, "Transfer-Encoding: chunked") {
34803485
return errors.New("not chunked")
34813486
}
@@ -3494,7 +3499,7 @@ func TestHeaderToWire(t *testing.T) {
34943499
rw.Write([]byte("<html><head></head><body>some html</body></html>"))
34953500
rw.Header().Set("Content-Type", "x/wrong")
34963501
},
3497-
check: func(got string) error {
3502+
check: func(got, logs string) error {
34983503
if !strings.Contains(got, "Content-Type: text/html") {
34993504
return errors.New("wrong content-type; want html")
35003505
}
@@ -3507,7 +3512,7 @@ func TestHeaderToWire(t *testing.T) {
35073512
rw.Header().Set("Content-Type", "some/type")
35083513
rw.Write([]byte("<html><head></head><body>some html</body></html>"))
35093514
},
3510-
check: func(got string) error {
3515+
check: func(got, logs string) error {
35113516
if !strings.Contains(got, "Content-Type: some/type") {
35123517
return errors.New("wrong content-type; want html")
35133518
}
@@ -3518,7 +3523,7 @@ func TestHeaderToWire(t *testing.T) {
35183523
name: "empty handler",
35193524
handler: func(rw ResponseWriter, r *Request) {
35203525
},
3521-
check: func(got string) error {
3526+
check: func(got, logs string) error {
35223527
if !strings.Contains(got, "Content-Length: 0") {
35233528
return errors.New("want 0 content-length")
35243529
}
@@ -3530,7 +3535,7 @@ func TestHeaderToWire(t *testing.T) {
35303535
handler: func(rw ResponseWriter, r *Request) {
35313536
rw.Header().Set("Some-Header", "some-value")
35323537
},
3533-
check: func(got string) error {
3538+
check: func(got, logs string) error {
35343539
if !strings.Contains(got, "Some-Header") {
35353540
return errors.New("didn't get header")
35363541
}
@@ -3543,7 +3548,7 @@ func TestHeaderToWire(t *testing.T) {
35433548
rw.WriteHeader(404)
35443549
rw.Header().Set("Too-Late", "some-value")
35453550
},
3546-
check: func(got string) error {
3551+
check: func(got, logs string) error {
35473552
if !strings.Contains(got, "404") {
35483553
return errors.New("wrong status")
35493554
}
@@ -3560,22 +3565,26 @@ func TestHeaderToWire(t *testing.T) {
35603565
rw.WriteHeader(200)
35613566
rw.Write([]byte("<!doctype html>\n<html><head></head><body>some html</body></html>"))
35623567
},
3563-
check: func(got string) error {
3568+
check: func(got, logs string) error {
35643569
if !strings.Contains(got, "Content-Type: application/octet-stream\r\n") {
35653570
return errors.New("Output should have an innocuous content-type")
35663571
}
35673572
if strings.Contains(got, "text/html") {
35683573
return errors.New("Output should not have a guess")
35693574
}
3575+
if !strings.Contains(logs, "X-Content-Type-Options:nosniff but no Content-Type") {
3576+
return errors.New("Expected log message")
3577+
}
35703578
return nil
35713579
},
35723580
},
35733581
}
35743582
for _, tc := range tests {
35753583
ht := newHandlerTest(HandlerFunc(tc.handler))
35763584
got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org")
3577-
if err := tc.check(got); err != nil {
3578-
t.Errorf("%s: %v\nGot response:\n%s", tc.name, err, got)
3585+
logs := ht.logbuf.String()
3586+
if err := tc.check(got, logs); err != nil {
3587+
t.Errorf("%s: %v\nGot response:\n%s\n\n%s", tc.name, err, got, logs)
35793588
}
35803589
}
35813590
}

0 commit comments

Comments
 (0)