@@ -134,14 +134,15 @@ func reqBytes(req string) []byte {
134
134
}
135
135
136
136
type handlerTest struct {
137
+ logbuf bytes.Buffer
137
138
handler Handler
138
139
}
139
140
140
141
func newHandlerTest (h Handler ) handlerTest {
141
- return handlerTest {h }
142
+ return handlerTest {handler : h }
142
143
}
143
144
144
- func (ht handlerTest ) rawResponse (req string ) string {
145
+ func (ht * handlerTest ) rawResponse (req string ) string {
145
146
reqb := reqBytes (req )
146
147
var output bytes.Buffer
147
148
conn := & rwTestConn {
@@ -150,7 +151,11 @@ func (ht handlerTest) rawResponse(req string) string {
150
151
closec : make (chan bool , 1 ),
151
152
}
152
153
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 )
154
159
<- conn .closec
155
160
return output .String ()
156
161
}
@@ -3399,14 +3404,14 @@ func TestHeaderToWire(t *testing.T) {
3399
3404
tests := []struct {
3400
3405
name string
3401
3406
handler func (ResponseWriter , * Request )
3402
- check func (output string ) error
3407
+ check func (got , logs string ) error
3403
3408
}{
3404
3409
{
3405
3410
name : "write without Header" ,
3406
3411
handler : func (rw ResponseWriter , r * Request ) {
3407
3412
rw .Write ([]byte ("hello world" ))
3408
3413
},
3409
- check : func (got string ) error {
3414
+ check : func (got , logs string ) error {
3410
3415
if ! strings .Contains (got , "Content-Length:" ) {
3411
3416
return errors .New ("no content-length" )
3412
3417
}
@@ -3424,7 +3429,7 @@ func TestHeaderToWire(t *testing.T) {
3424
3429
rw .Write ([]byte ("hello world" ))
3425
3430
h .Set ("Too-Late" , "bogus" )
3426
3431
},
3427
- check : func (got string ) error {
3432
+ check : func (got , logs string ) error {
3428
3433
if ! strings .Contains (got , "Content-Length:" ) {
3429
3434
return errors .New ("no content-length" )
3430
3435
}
@@ -3443,7 +3448,7 @@ func TestHeaderToWire(t *testing.T) {
3443
3448
rw .Write ([]byte ("hello world" ))
3444
3449
rw .Header ().Set ("Too-Late" , "Write already wrote headers" )
3445
3450
},
3446
- check : func (got string ) error {
3451
+ check : func (got , logs string ) error {
3447
3452
if strings .Contains (got , "Too-Late" ) {
3448
3453
return errors .New ("header appeared from after WriteHeader" )
3449
3454
}
@@ -3457,7 +3462,7 @@ func TestHeaderToWire(t *testing.T) {
3457
3462
rw .Write ([]byte ("post-flush" ))
3458
3463
rw .Header ().Set ("Too-Late" , "Write already wrote headers" )
3459
3464
},
3460
- check : func (got string ) error {
3465
+ check : func (got , logs string ) error {
3461
3466
if ! strings .Contains (got , "Transfer-Encoding: chunked" ) {
3462
3467
return errors .New ("not chunked" )
3463
3468
}
@@ -3475,7 +3480,7 @@ func TestHeaderToWire(t *testing.T) {
3475
3480
rw .Write ([]byte ("post-flush" ))
3476
3481
rw .Header ().Set ("Too-Late" , "Write already wrote headers" )
3477
3482
},
3478
- check : func (got string ) error {
3483
+ check : func (got , logs string ) error {
3479
3484
if ! strings .Contains (got , "Transfer-Encoding: chunked" ) {
3480
3485
return errors .New ("not chunked" )
3481
3486
}
@@ -3494,7 +3499,7 @@ func TestHeaderToWire(t *testing.T) {
3494
3499
rw .Write ([]byte ("<html><head></head><body>some html</body></html>" ))
3495
3500
rw .Header ().Set ("Content-Type" , "x/wrong" )
3496
3501
},
3497
- check : func (got string ) error {
3502
+ check : func (got , logs string ) error {
3498
3503
if ! strings .Contains (got , "Content-Type: text/html" ) {
3499
3504
return errors .New ("wrong content-type; want html" )
3500
3505
}
@@ -3507,7 +3512,7 @@ func TestHeaderToWire(t *testing.T) {
3507
3512
rw .Header ().Set ("Content-Type" , "some/type" )
3508
3513
rw .Write ([]byte ("<html><head></head><body>some html</body></html>" ))
3509
3514
},
3510
- check : func (got string ) error {
3515
+ check : func (got , logs string ) error {
3511
3516
if ! strings .Contains (got , "Content-Type: some/type" ) {
3512
3517
return errors .New ("wrong content-type; want html" )
3513
3518
}
@@ -3518,7 +3523,7 @@ func TestHeaderToWire(t *testing.T) {
3518
3523
name : "empty handler" ,
3519
3524
handler : func (rw ResponseWriter , r * Request ) {
3520
3525
},
3521
- check : func (got string ) error {
3526
+ check : func (got , logs string ) error {
3522
3527
if ! strings .Contains (got , "Content-Length: 0" ) {
3523
3528
return errors .New ("want 0 content-length" )
3524
3529
}
@@ -3530,7 +3535,7 @@ func TestHeaderToWire(t *testing.T) {
3530
3535
handler : func (rw ResponseWriter , r * Request ) {
3531
3536
rw .Header ().Set ("Some-Header" , "some-value" )
3532
3537
},
3533
- check : func (got string ) error {
3538
+ check : func (got , logs string ) error {
3534
3539
if ! strings .Contains (got , "Some-Header" ) {
3535
3540
return errors .New ("didn't get header" )
3536
3541
}
@@ -3543,7 +3548,7 @@ func TestHeaderToWire(t *testing.T) {
3543
3548
rw .WriteHeader (404 )
3544
3549
rw .Header ().Set ("Too-Late" , "some-value" )
3545
3550
},
3546
- check : func (got string ) error {
3551
+ check : func (got , logs string ) error {
3547
3552
if ! strings .Contains (got , "404" ) {
3548
3553
return errors .New ("wrong status" )
3549
3554
}
@@ -3560,22 +3565,26 @@ func TestHeaderToWire(t *testing.T) {
3560
3565
rw .WriteHeader (200 )
3561
3566
rw .Write ([]byte ("<!doctype html>\n <html><head></head><body>some html</body></html>" ))
3562
3567
},
3563
- check : func (got string ) error {
3568
+ check : func (got , logs string ) error {
3564
3569
if ! strings .Contains (got , "Content-Type: application/octet-stream\r \n " ) {
3565
3570
return errors .New ("Output should have an innocuous content-type" )
3566
3571
}
3567
3572
if strings .Contains (got , "text/html" ) {
3568
3573
return errors .New ("Output should not have a guess" )
3569
3574
}
3575
+ if ! strings .Contains (logs , "X-Content-Type-Options:nosniff but no Content-Type" ) {
3576
+ return errors .New ("Expected log message" )
3577
+ }
3570
3578
return nil
3571
3579
},
3572
3580
},
3573
3581
}
3574
3582
for _ , tc := range tests {
3575
3583
ht := newHandlerTest (HandlerFunc (tc .handler ))
3576
3584
got := ht .rawResponse ("GET / HTTP/1.1\n Host: golang.org" )
3577
- if err := tc .check (got ); err != nil {
3578
- t .Errorf ("%s: %v\n Got 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\n Got response:\n %s\n \n %s" , tc .name , err , got , logs )
3579
3588
}
3580
3589
}
3581
3590
}
0 commit comments