/
rest_test.go
199 lines (161 loc) · 4.27 KB
/
rest_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package adhocrest
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func testMethod(method string, callback func(w *httptest.ResponseRecorder)) {
req, err := http.NewRequest(method, "http://example.com/foo", nil)
if err != nil {
log.Fatal(err)
}
w := httptest.NewRecorder()
handler := NewHandler("")
handler.ServeHTTP(w, req)
callback(w)
}
func TestInvalidMethod(t *testing.T) {
testMethod("fisk", func(w *httptest.ResponseRecorder) {
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("Expected status %d, got %d\n", http.StatusMethodNotAllowed, w.Code)
}
})
}
func TestPostBadData(t *testing.T) {
handler := NewHandler("")
req, _ := http.NewRequest("POST", "/", strings.NewReader("[]"))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if http.StatusInternalServerError != w.Code {
t.Errorf(
"Expected internal server error on non-\"dict\" post, got response\n%s",
w.Body.String(),
)
}
req, _ = http.NewRequest("POST", "/", strings.NewReader("500"))
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if http.StatusInternalServerError != w.Code {
t.Errorf(
"Expected internal server error on non-\"dict\" post, got response\n%s",
w.Body.String(),
)
}
req, _ = http.NewRequest("POST", "/", strings.NewReader(`{"from":"reader"}`))
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if http.StatusCreated != w.Code {
t.Errorf("Expected %d on valid \"dict\" data, got %d", http.StatusCreated, w.Code)
}
}
func TestPostNewResource(t *testing.T) {
handler := NewHandler("")
// Create a resource
data := map[string]string{"from": "httptest.Recorder"}
jsonData, err := json.Marshal(data)
if nil != err {
t.Error(err)
}
req, err := http.NewRequest("POST", "/fish", strings.NewReader(string(jsonData)))
if nil != err {
t.Error(err)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
// Create another
req, err = http.NewRequest("POST", "/fish", strings.NewReader(string(jsonData)))
if nil != err {
t.Error(err)
}
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if nil != err {
t.Error(err)
}
if w.Code != http.StatusCreated {
t.Errorf("Expected status code %d, got %d", http.StatusCreated, w.Code)
}
headers := w.Header()
_, ok := headers["Location"]
var locationHeader string
if ok {
locationHeader = headers.Get("location")
expectedHeaderPrefix := "/fish/"
if !strings.HasPrefix(locationHeader, expectedHeaderPrefix) || len(locationHeader) < len(expectedHeaderPrefix) {
t.Errorf("Expected location to match /fish/<string:id>, got %s", locationHeader)
}
} else {
t.Errorf("Expected location header on create, got:\n%s", headers)
}
// Get the same info again
req, err = http.NewRequest("GET", locationHeader, nil)
if nil != err {
t.Error(err)
}
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
responseBodyStr := w.Body.String()
if w.Code != http.StatusOK {
t.Errorf("Could not get resource from %s, status: %d", locationHeader, w.Code)
}
// Unmarshal recieved json data, and remove id
responseData := make(map[string]string)
err = json.Unmarshal(w.Body.Bytes(), &responseData)
if nil != err {
t.Error(err)
}
delete(responseData, "id")
responseDataStr, err := json.Marshal(responseData)
if nil != err {
t.Error(err)
}
if 0 != bytes.Compare(jsonData, responseDataStr) {
t.Errorf(
`Expected to match posted data plus id:
-----
%s
-----
got
-----
%s
-----`,
jsonData,
responseBodyStr,
)
}
// Get a list
req, err = http.NewRequest("GET", "/fish", nil)
if nil != err {
t.Error(err)
}
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200 on resource listing, got %d", w.Code)
}
// Delete it
req, err = http.NewRequest("DELETE", locationHeader, nil)
if nil != err {
t.Error(err)
}
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected http.StatusOK on DELETE, got %d", w.Code)
}
// Get it again (not)
req, err = http.NewRequest("GET", locationHeader, nil)
if nil != err {
t.Error(err)
}
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Errorf("Expected http status 404, Not Found, on %s, got %d. Was it not deleted?",
locationHeader, w.Code)
}
}