-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathstatus_test.go
54 lines (42 loc) · 1.29 KB
/
status_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
package rest
import (
"github.com/ant0ine/go-json-rest/rest/test"
"testing"
)
func TestStatusMiddleware(t *testing.T) {
api := NewApi()
// the middlewares
status := &StatusMiddleware{}
api.Use(status)
api.Use(&TimerMiddleware{})
api.Use(&RecorderMiddleware{})
// an app that return the Status
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(status.GetStatus())
}))
// wrap all
handler := api.MakeHandler()
// one request
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/1", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// another request
recorded = test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/2", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// payload
payload := map[string]interface{}{}
err := recorded.DecodeJsonPayload(&payload)
if err != nil {
t.Fatal(err)
}
if payload["Pid"] == nil {
t.Error("Expected a non nil Pid")
}
if payload["TotalCount"].(float64) != 1 {
t.Errorf("TotalCount 1 Expected, got: %f", payload["TotalCount"].(float64))
}
if payload["StatusCodeCount"].(map[string]interface{})["200"].(float64) != 1 {
t.Errorf("StatusCodeCount 200 1 Expected, got: %f", payload["StatusCodeCount"].(map[string]interface{})["200"].(float64))
}
}