-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger_test.go
66 lines (58 loc) · 1.71 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"net/http"
"testing"
)
type testrw struct {
headerCalled bool
writeCalled bool
writeHeaderCalled bool
}
func (trw *testrw) Header() http.Header {
trw.headerCalled = true
return http.Header{}
}
func (trw *testrw) Write(b []byte) (int, error) {
trw.writeCalled = true
return len(b), nil
}
func (trw *testrw) WriteHeader(int) {
trw.writeHeaderCalled = true
}
func TestResponseWriterRecorderWrite(t *testing.T) {
t.Run("calls the underlying rw.Header", func(t *testing.T) {
rr := &responseWriterRecorder{ResponseWriter: &testrw{}}
rr.Header()
if !rr.ResponseWriter.(*testrw).headerCalled {
t.Error("rw.Header has not been called")
}
})
t.Run("calls the underlying rw.Write", func(t *testing.T) {
rr := &responseWriterRecorder{ResponseWriter: &testrw{}}
rr.Write([]byte("hey"))
if !rr.ResponseWriter.(*testrw).writeCalled {
t.Error("rw.Write has not been called")
}
})
t.Run("calls the underlying rw.WriteHeader", func(t *testing.T) {
rr := &responseWriterRecorder{ResponseWriter: &testrw{}}
rr.WriteHeader(418)
if !rr.ResponseWriter.(*testrw).writeHeaderCalled {
t.Error("rw.WriteHeader has not been called")
}
})
t.Run("sets the status when WriteHeader is called", func(t *testing.T) {
rr := &responseWriterRecorder{ResponseWriter: &testrw{}}
rr.WriteHeader(418)
if want, have := 418, rr.status; want != have {
t.Errorf("expected status %d, found %d", want, have)
}
})
t.Run("sets the status to 200 when Write is called first", func(t *testing.T) {
rr := &responseWriterRecorder{ResponseWriter: &testrw{}}
rr.Write([]byte(""))
if want, have := 200, rr.status; want != have {
t.Errorf("expected status %d, found %d", want, have)
}
})
}