-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreator_test.go
245 lines (205 loc) · 5.67 KB
/
creator_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package tyrgin
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
type respTest struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
Uploaded string `json:"uploaded"`
Count int `json:"count"`
}
type reqTest struct {
Upload string `json:"uploaded" binding:"required"`
Count int `json:"count" binding:"required"`
}
var uploaded = ""
var count = 0
func testGetFunc(c *gin.Context) {
count++
c.JSON(
http.StatusOK,
gin.H{
"status_code": http.StatusOK,
"message": "Hello World!",
"uploaded": uploaded,
"count": count,
},
)
}
func testDeleteFunc(c *gin.Context) {
uploaded = ""
count = 0
c.JSON(
http.StatusResetContent,
gin.H{
"status_code": http.StatusResetContent,
"message": "Hello Deleted",
"uploaded": uploaded,
"count": count,
},
)
}
func testPatchFunc(c *gin.Context) {
var json reqTest
err := c.ShouldBindJSON(&json)
ErrorLogger(err, "Failed to bind json for testPatchFunc.")
if json.Upload != "" {
uploaded = json.Upload
}
count++
c.JSON(
http.StatusCreated,
gin.H{
"status_code": http.StatusCreated,
"message": "Hello Patch",
"uploaded": uploaded,
"count": count,
},
)
}
func testPostFunc(c *gin.Context) {
var rb reqTest
c.ShouldBindJSON(&rb)
uploaded = rb.Upload
count = rb.Count
c.JSON(
http.StatusCreated,
gin.H{
"status_code": http.StatusCreated,
"message": "Hello Post",
"uploaded": uploaded,
"count": count,
},
)
}
func testPutFunc(c *gin.Context) {
var json reqTest
err := c.ShouldBindJSON(&json)
ErrorLogger(err, "Failed to bind json for testPutFunc.")
uploaded = json.Upload
count = json.Count
c.JSON(
http.StatusCreated,
gin.H{
"status_code": http.StatusCreated,
"message": "Hello Put",
"uploaded": uploaded,
"count": count,
},
)
}
var (
testGet = NewRoute(testGetFunc, "hello", GET)
testDelete = NewRoute(testDeleteFunc, "hello", DELETE)
testPatch = NewRoute(testPatchFunc, "hello", PATCH)
testPost = NewRoute(testPostFunc, "hello", POST)
testPut = NewRoute(testPutFunc, "hello", PUT)
)
var testEndpoints = []APIAction{
testGet,
testDelete,
testPatch,
testPost,
testPut,
}
func performRequest(r http.Handler, method, path string, body []byte) *httptest.ResponseRecorder {
var req *http.Request
if method == "GET" {
req, _ = http.NewRequest(method, path, nil)
} else {
bb := new(bytes.Buffer)
json.NewEncoder(bb).Encode(body)
req, _ = http.NewRequest(method, path, bytes.NewBuffer(body))
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
return resp
}
func TestNotFound(t *testing.T) {
body := gin.H{
"statusCode": http.StatusNotFound,
"message": NotFoundError,
}
router := SetupRouter()
router.NoRoute(NotFound)
resp := performRequest(router, "GET", "/", nil)
assert.Equal(t, http.StatusNotFound, resp.Code)
var response respTest
err := json.Unmarshal([]byte(resp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, body["statusCode"], response.StatusCode)
assert.Equal(t, body["message"], response.Message)
}
func TestCreatorRoutes(t *testing.T) {
router := SetupRouter()
AddRoutes(router, false, nil, "1", "tester", testEndpoints)
var response respTest
// Test Get
gresp := performRequest(router, "GET", "/api/v1/tester/hello", nil)
assert.Equal(t, http.StatusOK, gresp.Code)
err := json.Unmarshal([]byte(gresp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "Hello World!", response.Message)
assert.Equal(t, "", response.Uploaded)
assert.Equal(t, 1, response.Count)
var request reqTest
// Test Post
request = reqTest{
Upload: "New Hello",
Count: 100,
}
jsb, _ := json.Marshal(request)
poresp := performRequest(router, "POST", "/api/v1/tester/hello", jsb)
assert.Equal(t, http.StatusCreated, poresp.Code)
err = json.Unmarshal([]byte(poresp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Hello Post", response.Message)
assert.Equal(t, "New Hello", response.Uploaded)
assert.Equal(t, 100, response.Count)
// Test Put
request = reqTest{
Upload: "New New Hello",
Count: 1000,
}
jsb, _ = json.Marshal(request)
puresp := performRequest(router, "PUT", "/api/v1/tester/hello", jsb)
assert.Equal(t, http.StatusCreated, puresp.Code)
err = json.Unmarshal([]byte(puresp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Hello Put", response.Message)
assert.Equal(t, "New New Hello", response.Uploaded)
assert.Equal(t, 1000, response.Count)
// Test Patch
request = reqTest{
Upload: "New New New Hello",
}
jsb, _ = json.Marshal(request)
paresp := performRequest(router, "PATCH", "/api/v1/tester/hello", jsb)
assert.Equal(t, http.StatusCreated, paresp.Code)
err = json.Unmarshal([]byte(paresp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Hello Patch", response.Message)
assert.Equal(t, "New New New Hello", response.Uploaded)
assert.Equal(t, 1001, response.Count)
// Test Delete
dresp := performRequest(router, "DELETE", "/api/v1/tester/hello", nil)
assert.Equal(t, http.StatusResetContent, dresp.Code)
err = json.Unmarshal([]byte(dresp.Body.String()), &response)
assert.Nil(t, err)
assert.Equal(t, 205, response.StatusCode)
assert.Equal(t, "Hello Deleted", response.Message)
assert.Equal(t, "", response.Uploaded)
assert.Equal(t, 0, response.Count)
}