-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
51 lines (43 loc) · 897 Bytes
/
middleware_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 httplog_test
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gkats/httplog"
)
type testHandler struct{}
func (h *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "{}")
}
func TestWithLogging(t *testing.T) {
w := NewTestWriter()
l := httplog.New(w)
h := httplog.WithLogging(&testHandler{}, l)
ts := httptest.NewServer(h)
defer ts.Close()
if _, err := http.Get(ts.URL + "?foo=bar"); err != nil {
t.Error(err)
}
tests := []string{
"level=I",
"time=",
"ip=",
"method=GET",
"path=",
"ua=",
"status=200",
"params={\"foo\": \"bar\"}",
"\n",
}
count := 0
for i, want := range tests {
count = strings.Count(w.Stream, want)
if count != 1 {
t.Errorf("(%v) Expected %v to contain '%v' exactly once, got %v", i, w.Stream, want, count)
}
count = 0
}
}