-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlogger_test.go
79 lines (67 loc) · 2.5 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
67
68
69
70
71
72
73
74
75
76
77
78
79
package golf
import (
"bytes"
"regexp"
"testing"
)
func assertContains(t *testing.T, content string, query string) {
re := regexp.MustCompile(query)
if re.FindString(content) == "" {
t.Errorf("Not contain: %v in %v", query, content)
}
}
func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer)
app := New()
app.Use(LoggingMiddleware(buffer))
app.Get("/example", func(c *Context) {})
app.Post("/example", func(c *Context) {})
app.Put("/example", func(c *Context) {})
app.Delete("/example", func(c *Context) {})
app.Patch("/example", func(c *Context) {})
app.Head("/example", func(c *Context) {})
app.Options("/example", func(c *Context) {})
_, _, r, w := makeTestContext("GET", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "GET")
assertContains(t, buffer.String(), "/example")
// I wrote these first (extending the above) but then realized they are more
// like integration tests because they test the whole logging process rather
// than individual functions. Im not sure where these should go.
_, _, r, w = makeTestContext("POST", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "POST")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("PUT", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "PUT")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("DELETE", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "DELETE")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("PATCH", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "PATCH")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("HEAD", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "HEAD")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("OPTIONS", "/example")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "200")
assertContains(t, buffer.String(), "OPTIONS")
assertContains(t, buffer.String(), "/example")
_, _, r, w = makeTestContext("GET", "/notfound")
app.ServeHTTP(w, r)
assertContains(t, buffer.String(), "404")
assertContains(t, buffer.String(), "GET")
assertContains(t, buffer.String(), "/notfound")
}