forked from gocraft/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_middleware_test.go
40 lines (33 loc) · 955 Bytes
/
logger_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
package web
import (
"bytes"
"log"
"regexp"
"testing"
)
func TestLoggerMiddleware(t *testing.T) {
var buf bytes.Buffer
Logger = log.New(&buf, "", 0)
router := New(Context{})
router.Middleware(LoggerMiddleware)
router.Get("/action", (*Context).A)
// Hit an action:
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "context-A", 200)
// Make sure our buf has something good:
logRegexp := regexp.MustCompile("\\[\\d+ .{2}\\] 200 '/action'")
if !logRegexp.MatchString(buf.String()) {
t.Error("Got invalid log entry: ", buf.String())
}
// Do a 404:
buf.Reset()
rw, req = newTestRequest("GET", "/wat")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Not Found", 404)
// Make sure our buf has something good:
logRegexpNotFound := regexp.MustCompile("\\[\\d+ .{2}\\] 404 '/wat'")
if !logRegexpNotFound.MatchString(buf.String()) {
t.Error("Got invalid log entry: ", buf.String())
}
}