-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
196 lines (174 loc) · 3.88 KB
/
api_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
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/google/jsonapi"
)
const (
API_VER = "/api/v1"
CONTENT_TYPE_JSON = "application/json"
)
/**
* curl http://localhost:8080
**/
func TestViewPage(t *testing.T) {
requestEndpoint(
t,
http.MethodGet,
"/",
"",
"",
viewHandler,
http.StatusOK,
"JSON Formatter",
)
}
/**
* curl -d '{"data":"{}"}' http://localhost:8080/api/v1/std
**/
func TestStdEndpoint(t *testing.T) {
requestEndpoint(
t,
http.MethodPost,
API_VER+"/std",
`{"data":"{}"}`,
CONTENT_TYPE_JSON,
stdHandler,
http.StatusCreated,
"{}",
)
}
/**
* curl -d "{}" http://localhost:8080/api/v1/spec
**/
func TestSpecEndpoint(t *testing.T) {
res := requestEndpoint(
t,
http.MethodPost,
API_VER+"/spec",
"{}",
jsonapi.MediaType,
specHandler,
http.StatusCreated,
"{}",
)
// Check content type is up to JSON:API spec
result := res.Result()
if content_type := result.Header.Get("Content-type"); content_type != jsonapi.MediaType {
t.Errorf("handler returned unexpected body: got %v want %v", content_type, jsonapi.MediaType)
}
}
// curl -d '{"data":"{\"test\":\"test\"}"}' http://localhost:8080/api/v1/std
func TestStdFormatsCorrectly(t *testing.T) {
pretty_json := `{
"test": "test"
}`
requestEndpoint(
t,
http.MethodPost,
API_VER+"/std",
`{"data":"{\"test\":\"test\"}"}`,
CONTENT_TYPE_JSON,
stdHandler,
http.StatusCreated,
pretty_json,
)
}
// curl -d '{"data":{"test":"test"}}' http://localhost:8080/api/v1/spec
func TestSpecFormatsCorrectly(t *testing.T) {
pretty_json := `{
"data": {
"test": "test"
}
}`
res := requestEndpoint(
t,
http.MethodPost,
API_VER+"/spec",
`{"data":{"test":"test"}}`,
jsonapi.MediaType,
specHandler,
http.StatusCreated,
pretty_json,
)
// Check content type is up to JSON:API spec
result := res.Result()
if content_type := result.Header.Get("Content-type"); content_type != jsonapi.MediaType {
t.Errorf("handler returned unexpected body: got %v want %v", content_type, jsonapi.MediaType)
}
}
// curl -X POST http://localhost:8080/api/v1/std
func TestStdFailure(t *testing.T) {
requestEndpoint(
t,
http.MethodPost,
API_VER+"/std",
"",
CONTENT_TYPE_JSON,
stdHandler,
http.StatusUnprocessableEntity,
`{"error": "EOF"}`,
)
}
// curl -X POST http://localhost:8080/api/v1/spec
func TestSpecFailure(t *testing.T) {
res := requestEndpoint(
t,
http.MethodPost,
API_VER+"/spec",
"",
jsonapi.MediaType,
specHandler,
http.StatusUnprocessableEntity,
`{"error": "EOF"}`,
)
// Check content type is up to JSON:API spec
result := res.Result()
if content_type := result.Header.Get("Content-type"); content_type != jsonapi.MediaType {
t.Errorf("handler returned unexpected body: got %v want %v", content_type, jsonapi.MediaType)
}
}
/**
* requestEndpoint is a boilerplate function to make testing endpoints a one liner.
**/
func requestEndpoint(
t *testing.T,
method string,
url string,
body string,
content_type string,
req_handler http.HandlerFunc,
expected_status int,
expected_data string) *httptest.ResponseRecorder {
var r io.Reader
if body != "" {
var json_str = []byte(body)
r = bytes.NewBuffer(json_str)
} else {
r = strings.NewReader(body)
}
// Request valid?
req, err := http.NewRequest(method, url, r)
if err != nil {
t.Fatal(err)
}
if content_type != "" {
req.Header.Set("Content-Type", content_type)
}
// Check status code
res := httptest.NewRecorder()
handler := http.HandlerFunc(req_handler)
handler.ServeHTTP(res, req)
if status := res.Code; status != expected_status {
t.Errorf("handler returned wrong status code: got %v want %v", status, expected_status)
}
// Check the response body is what we expect.
if body := res.Body.String(); !strings.Contains(body, expected_data) {
t.Errorf("handler returned unexpected body: got %v want %v", body, expected_data)
}
return res
}