This repository has been archived by the owner on May 24, 2020. It is now read-only.
forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
102 lines (91 loc) · 2.94 KB
/
handler_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package rest
import (
"github.com/ant0ine/go-json-rest/test"
"io/ioutil"
"log"
"testing"
)
func TestHandler(t *testing.T) {
handler := ResourceHandler{
DisableJsonIndent: true,
}
handler.SetRoutes(
Route{"GET", "/r/:id",
func(w *ResponseWriter, r *Request) {
id := r.PathParam("id")
w.WriteJson(map[string]string{"Id": id})
},
},
Route{"POST", "/r/:id",
func(w *ResponseWriter, r *Request) {
// JSON echo
data := map[string]string{}
err := r.DecodeJsonPayload(&data)
if err != nil {
t.Fatal(err)
}
w.WriteJson(data)
},
},
Route{"GET", "/auto-fails",
func(w *ResponseWriter, r *Request) {
a := []int{}
_ = a[0]
},
},
Route{"GET", "/user-error",
func(w *ResponseWriter, r *Request) {
Error(w, "My error", 500)
},
},
Route{"GET", "/user-notfound",
func(w *ResponseWriter, r *Request) {
NotFound(w, r)
},
},
)
// valid get resource
recorded := test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/r/123", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Id":"123"}`)
// valid post resource
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest(
"POST", "http://1.2.3.4/r/123", &map[string]string{"Test": "Test"}))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Test":"Test"}`)
// broken Content-Type post resource
request := test.MakeSimpleRequest("POST", "http://1.2.3.4/r/123", &map[string]string{"Test": "Test"})
request.Header.Set("Content-Type", "text/html")
recorded = test.RunRequest(t, &handler, request)
recorded.CodeIs(415)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Error":"Bad Content-Type, expected 'application/json'"}`)
// auto 405 on undefined route (wrong method)
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("DELETE", "http://1.2.3.4/r/123", nil))
recorded.CodeIs(405)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Error":"Method not allowed"}`)
// auto 404 on undefined route (wrong path)
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/s/123", nil))
recorded.CodeIs(404)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Error":"Resource not found"}`)
// auto 500 on unhandled userecorder error
origLogger := handler.Logger
handler.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/auto-fails", nil))
handler.Logger = origLogger
recorded.CodeIs(500)
// userecorder error
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/user-error", nil))
recorded.CodeIs(500)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Error":"My error"}`)
// userecorder notfound
recorded = test.RunRequest(t, &handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/user-notfound", nil))
recorded.CodeIs(404)
recorded.ContentTypeIsJson()
recorded.BodyIs(`{"Error":"Resource not found"}`)
}