-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathresponse_test.go
68 lines (53 loc) · 1.65 KB
/
response_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package rest
import (
"testing"
"github.com/ant0ine/go-json-rest/rest/test"
)
func TestResponseNotIndent(t *testing.T) {
writer := responseWriter{
nil,
false,
}
got, err := writer.EncodeJson(map[string]bool{"test": true})
if err != nil {
t.Error(err.Error())
}
gotStr := string(got)
expected := "{\"test\":true}"
if gotStr != expected {
t.Error(expected + " was the expected, but instead got " + gotStr)
}
}
// The following tests could instantiate only the reponseWriter,
// but using the Api object allows to use the rest/test utilities,
// and make the tests easier to write.
func TestWriteJsonResponse(t *testing.T) {
api := NewApi()
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}))
recorded := test.RunRequest(t, api.MakeHandler(), test.MakeSimpleRequest("GET", "http://localhost/", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.BodyIs("{\"Id\":\"123\"}")
}
func TestErrorResponse(t *testing.T) {
api := NewApi()
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
Error(w, "test", 500)
}))
recorded := test.RunRequest(t, api.MakeHandler(), test.MakeSimpleRequest("GET", "http://localhost/", nil))
recorded.CodeIs(500)
recorded.ContentTypeIsJson()
recorded.BodyIs("{\"Error\":\"test\"}")
}
func TestNotFoundResponse(t *testing.T) {
api := NewApi()
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
NotFound(w, r)
}))
recorded := test.RunRequest(t, api.MakeHandler(), test.MakeSimpleRequest("GET", "http://localhost/", nil))
recorded.CodeIs(404)
recorded.ContentTypeIsJson()
recorded.BodyIs("{\"Error\":\"Resource not found\"}")
}