-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
logger_test.go
51 lines (41 loc) · 1.09 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
package middleware
import (
"bufio"
"bytes"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type testLoggerWriter struct {
*httptest.ResponseRecorder
}
func (cw testLoggerWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, nil
}
func TestRequestLogger(t *testing.T) {
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := w.(http.Hijacker)
if !ok {
t.Errorf("http.Hijacker is unavailable on the writer. add the interface methods.")
}
})
r := httptest.NewRequest("GET", "/", nil)
w := testLoggerWriter{
ResponseRecorder: httptest.NewRecorder(),
}
handler := DefaultLogger(testHandler)
handler.ServeHTTP(w, r)
}
func TestRequestLoggerReadFrom(t *testing.T) {
data := []byte("file data")
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "file", time.Time{}, bytes.NewReader(data))
})
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler := DefaultLogger(testHandler)
handler.ServeHTTP(w, r)
assertEqual(t, data, w.Body.Bytes())
}