-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
169 lines (144 loc) · 4.77 KB
/
handlers_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gw
import (
"bytes"
"net/http"
"net/http/httptest"
"encoding/json"
"testing"
"github.com/gorilla/mux"
"github.com/juju/errors"
)
func okHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return nil
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return &AppError{http.StatusNotFound, nil}
}
func notAllowedHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return &AppError{http.StatusMethodNotAllowed, nil}
}
func badRequestHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return &AppError{http.StatusBadRequest, errors.Errorf("Missing field")}
}
func forbiddenHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return &AppError{http.StatusForbidden, nil}
}
func serverErrorHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return &AppError{http.StatusInternalServerError, nil}
}
func testMiddleware(fn AppHandler) AppHandler {
return func(w http.ResponseWriter, r *http.Request) *AppError {
return fn(w, r)
}
}
func setupRawHandlers(m *mux.Router) {
m.Handle("/not/aproblem", testMiddleware(okHandler))
m.Handle("/not/found", testMiddleware(notFoundHandler))
m.Handle("/not/allowed", testMiddleware(notAllowedHandler))
m.Handle("/not/understood", testMiddleware(badRequestHandler))
m.Handle("/not/permitted", testMiddleware(forbiddenHandler))
m.Handle("/not/working", testMiddleware(serverErrorHandler))
}
func setupHandlers(m *mux.Router) {
c := MockSettings{}
m.Handle("/not/aproblem", Middleware(okHandler, c))
m.Handle("/not/found", Middleware(notFoundHandler, c))
m.Handle("/not/allowed", Middleware(notAllowedHandler, c))
m.Handle("/not/understood", Middleware(badRequestHandler, c))
m.Handle("/not/permitted", Middleware(forbiddenHandler, c))
m.Handle("/not/working", Middleware(serverErrorHandler, c))
}
func sendJsonHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
return SendJSON(map[string]interface{}{"foo":"bar"}, w, r)
}
type ReceiveTest struct {
Foo string `json:"foo"`
}
func receiveJsonHandler(w http.ResponseWriter, r *http.Request) (*AppError) {
var rt ReceiveTest
return ReceiveJSON(&rt, r)
}
func TestServeHTTP(t *testing.T) {
m := mux.NewRouter()
setupRawHandlers(m)
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/not/found", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusNotFound {
t.Errorf("Status not %v: %v", http.StatusNotFound, resp.Code)
}
resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/not/allowed", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusMethodNotAllowed {
t.Errorf("Status not %v: %v", http.StatusMethodNotAllowed, resp.Code)
}
resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/not/permitted", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusForbidden {
t.Errorf("Status not %v: %v", http.StatusForbidden, resp.Code)
}
resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/not/understood", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest {
t.Errorf("Status not %v: %v", http.StatusBadRequest, resp.Code)
}
var answer ErrorInfo
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&answer)
if err != nil {
t.Error("Error decoding error JSON")
}
if answer.Error != "Missing field" {
t.Error("Error in sending error JSON")
}
resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/not/working", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusInternalServerError {
t.Errorf("Status not %v: %v", http.StatusInternalServerError, resp.Code)
}
resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/not/aproblem", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Status not %v: %v", http.StatusOK, resp.Code)
}
}
// Bail, kind of hard to see inside
func TestMiddleware(t *testing.T) {
t.Skip()
}
func TestServeJSON(t *testing.T) {
m := mux.NewRouter()
m.Handle("/stuff", testMiddleware(sendJsonHandler))
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/stuff", nil)
m.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Status not %v: %v", http.StatusOK, resp.Code)
}
var answer map[string]interface{}
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&answer)
if err != nil {
t.Error("Error decoding JSON")
}
if answer["foo"].(string) != "bar" {
t.Error("Error in sending JSON")
}
}
func TestReceiveJSON(t *testing.T) {
m := mux.NewRouter()
m.Handle("/stuff", testMiddleware(receiveJsonHandler))
resp := httptest.NewRecorder()
reqJSON := []byte(`{"foo":"bar"}`)
req, _ := http.NewRequest("POST", "/stuff", bytes.NewBuffer(reqJSON))
req.Header.Add("Content-Type", "application/json")
m.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Status not %v: %v", http.StatusOK, resp.Code)
}
}