forked from bakins/logrus-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
48 lines (38 loc) · 1014 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
package logrusmiddleware
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
h "github.com/bakins/test-helpers"
"github.com/sirupsen/logrus"
)
func newRequest(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return req
}
func TestHandler(t *testing.T) {
var buf bytes.Buffer
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
fmt.Fprint(w, "Hello World\n")
})
logger := logrus.New()
logger.Level = logrus.InfoLevel
logger.Formatter = &logrus.JSONFormatter{}
logger.Out = &buf
l := Middleware{
Name: "example",
Logger: logger,
}
lh := l.Handler(http.HandlerFunc(handler), "homepage")
http.Handle("/", lh)
lh.ServeHTTP(httptest.NewRecorder(), newRequest("GET", "/foo"))
h.Assert(t, buf.Len() > 0, "buffer should not be empty")
h.Assert(t, strings.Contains(buf.String(), `"component":"homepage"`), "buffer did not match expected result")
}