@@ -46,26 +46,32 @@ func TestLogRequest(t *testing.T) {
4646}
4747
4848func testHandler (w http.ResponseWriter , r * http.Request ) {
49+ if r .URL != nil && r .URL .Path == "/panicbefore" {
50+ panic ("some test handler panic before response" )
51+ }
4952 if r .URL != nil && r .URL .Path == "/tea" {
5053 w .WriteHeader (http .StatusTeapot )
5154 }
5255 w .Write ([]byte ("hello" ))
5356 time .Sleep (100 * time .Millisecond )
57+ if r .URL != nil && r .URL .Path == "/panicafter" {
58+ panic ("some test handler panic after response" )
59+ }
5460}
5561
5662type NullHTTPWriter struct {
5763 doErr bool
58- doPanic bool
64+ headers http. Header
5965}
6066
6167func (n * NullHTTPWriter ) Header () http.Header {
62- return nil
68+ if n .headers == nil {
69+ n .headers = make (http.Header )
70+ }
71+ return n .headers
6372}
6473
6574func (n * NullHTTPWriter ) Write (b []byte ) (int , error ) {
66- if n .doPanic {
67- panic ("some fake http write panic" )
68- }
6975 if n .doErr {
7076 return 0 , errors .New ("some fake http write error" )
7177 }
@@ -100,7 +106,7 @@ func TestLogAndCall(t *testing.T) {
100106 if ! strings .HasPrefix (actual , expectedPrefix ) {
101107 t .Errorf ("unexpected:\n %s\n vs should start with:\n %s\n " , actual , expectedPrefix )
102108 }
103- if hw .Header () != nil {
109+ if len ( hw .Header ()) != 0 {
104110 t .Errorf ("unexpected non nil header: %v" , hw .Header ())
105111 }
106112 hr .URL = & url.URL {Path : "/tea" }
@@ -123,20 +129,33 @@ func TestLogAndCall(t *testing.T) {
123129 if ! strings .Contains (actual , expectedFragment ) {
124130 t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , expectedFragment )
125131 }
126- n . doPanic = true
132+ hr . URL = & url. URL { Path : "/panicafter" }
127133 n .doErr = false
128134 SetLogLevelQuiet (Verbose )
129135 b .Reset ()
130136 LogAndCall ("test-log-and-call4" , testHandler ).ServeHTTP (hw , hr )
131137 w .Flush ()
132138 actual = b .String ()
133- expectedFragment = `,"size":0,`
139+ expectedFragment = `"status":-500,`
140+ Config .GoroutineID = false
141+ if ! strings .Contains (actual , expectedFragment ) {
142+ t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , expectedFragment )
143+ }
144+ if ! strings .Contains (actual , `{"level":"crit","msg":"panic in handler","error":"some test handler panic after response"}` ) {
145+ t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , "some test handler panic after response" )
146+ }
147+ hr .URL = & url.URL {Path : "/panicbefore" }
148+ b .Reset ()
149+ LogAndCall ("test-log-and-call5" , testHandler ).ServeHTTP (hw , hr )
150+ w .Flush ()
151+ actual = b .String ()
152+ expectedFragment = `"status":-500,`
134153 Config .GoroutineID = false
135154 if ! strings .Contains (actual , expectedFragment ) {
136155 t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , expectedFragment )
137156 }
138- if ! strings .Contains (actual , `{"level":"crit","msg":"panic in handler","error":"some fake http write panic" ` ) {
139- t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , "some fake http write panic" )
157+ if ! strings .Contains (actual , `{"level":"crit","msg":"panic in handler","error":"some test handler panic before response"} ` ) {
158+ t .Errorf ("unexpected:\n %s\n vs should contain error:\n %s\n " , actual , "some test handler panic before response " )
140159 }
141160 // restore for other tests
142161 Config .GoroutineID = true
0 commit comments