-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathaccess_log_json_test.go
53 lines (44 loc) · 1.16 KB
/
access_log_json_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
package rest
import (
"bytes"
"encoding/json"
"github.com/ant0ine/go-json-rest/rest/test"
"log"
"testing"
)
func TestAccessLogJsonMiddleware(t *testing.T) {
api := NewApi()
// the middlewares stack
buffer := bytes.NewBufferString("")
api.Use(&AccessLogJsonMiddleware{
Logger: log.New(buffer, "", 0),
})
api.Use(&TimerMiddleware{})
api.Use(&RecorderMiddleware{})
// a simple app
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}))
// wrap all
handler := api.MakeHandler()
req := test.MakeSimpleRequest("GET", "http://localhost/", nil)
req.RemoteAddr = "127.0.0.1:1234"
recorded := test.RunRequest(t, handler, req)
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// log tests
decoded := &AccessLogJsonRecord{}
err := json.Unmarshal(buffer.Bytes(), decoded)
if err != nil {
t.Fatal(err)
}
if decoded.StatusCode != 200 {
t.Errorf("StatusCode 200 expected, got %d", decoded.StatusCode)
}
if decoded.RequestURI != "/" {
t.Errorf("RequestURI / expected, got %s", decoded.RequestURI)
}
if decoded.HttpMethod != "GET" {
t.Errorf("HttpMethod GET expected, got %s", decoded.HttpMethod)
}
}