forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_test.go
49 lines (39 loc) · 1.16 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
package rest
import (
"github.com/ant0ine/go-json-rest/test"
"testing"
)
func TestStatus(t *testing.T) {
handler := ResourceHandler{
EnableStatusService: true,
}
handler.SetRoutes(
Route{"GET", "/r",
func(w *ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
},
},
)
// one request to the API
recorded := test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// check the status
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/.status", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
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))
}
}