/
haruka_test.go
238 lines (217 loc) · 5.76 KB
/
haruka_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
package haruka
import (
"crypto/tls"
"encoding/xml"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"
)
func testRequest(t *testing.T, url string, expected string) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
assert.NoError(t, err)
defer resp.Body.Close()
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, expected, string(body), "resp body should match")
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
}
func testMakePOSTRequest(t *testing.T, url string) ([]byte, *http.Response) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
resp, err := client.Post(url, "application/json", nil)
assert.NoError(t, err)
defer resp.Body.Close()
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
return body, resp
}
func testMakeGETRequest(t *testing.T, url string) ([]byte, *http.Response) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
assert.NoError(t, err)
defer resp.Body.Close()
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
return body, resp
}
func testMakePUTRequest(t *testing.T, url string) ([]byte, *http.Response) {
client := &http.Client{}
request, err := http.NewRequest("PUT", url, nil)
if err != nil {
t.Error(err)
}
response, err := client.Do(request)
body, ioerr := ioutil.ReadAll(response.Body)
assert.NoError(t, ioerr)
return body, response
}
func testMakePATCHRequest(t *testing.T, url string) ([]byte, *http.Response) {
client := &http.Client{}
request, err := http.NewRequest("PATCH", url, nil)
if err != nil {
t.Error(err)
}
response, err := client.Do(request)
body, ioerr := ioutil.ReadAll(response.Body)
assert.NoError(t, ioerr)
return body, response
}
func testMakeDELETERequest(t *testing.T, url string) ([]byte, *http.Response) {
client := &http.Client{}
request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
t.Error(err)
}
response, err := client.Do(request)
body, ioerr := ioutil.ReadAll(response.Body)
assert.NoError(t, ioerr)
return body, response
}
func TestRunAndListen(t *testing.T) {
e := NewEngine()
e.Router.AddHandler("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
testRequest(t, "http://localhost:8090/ping", "{\"say\":\"pong\"}")
}
type XMLBody struct {
XMLName xml.Name `xml:"test"`
Say string `xml:"say"`
}
func TestXML(t *testing.T) {
e := NewEngine()
e.Router.AddHandler("/ping", func(context *Context) {
err := context.XML(XMLBody{Say: "Hello"})
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
testRequest(t, "http://localhost:8090/ping", "<test><say>Hello</say></test>")
}
func TestRouter_GET(t *testing.T) {
e := NewEngine()
e.Router.GET("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
_, resp := testMakePOSTRequest(t, "http://localhost:8090/ping")
testRequest(t, "http://localhost:8090/ping", "{\"say\":\"pong\"}")
// other methods 405 response
assert.Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
}
func TestRouter_POST(t *testing.T) {
e := NewEngine()
e.Router.POST("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
body, _ := testMakePOSTRequest(t, "http://localhost:8090/ping")
assert.Equal(t, "{\"say\":\"pong\"}", string(body))
// other methods 405 response
_, postResponse := testMakeGETRequest(t, "http://localhost:8090/ping")
assert.Equal(t, postResponse.StatusCode, http.StatusMethodNotAllowed)
}
func TestRouter_PUT(t *testing.T) {
e := NewEngine()
e.Router.PUT("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
body, _ := testMakePUTRequest(t, "http://localhost:8090/ping")
assert.Equal(t, "{\"say\":\"pong\"}", string(body))
// other methods 405 response
_, postResponse := testMakePOSTRequest(t, "http://localhost:8090/ping")
assert.Equal(t, postResponse.StatusCode, http.StatusMethodNotAllowed)
}
func TestRouter_PATCH(t *testing.T) {
e := NewEngine()
e.Router.PATCH("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
body, _ := testMakePATCHRequest(t, "http://localhost:8090/ping")
assert.Equal(t, "{\"say\":\"pong\"}", string(body))
// other methods 405 response
_, postResponse := testMakePOSTRequest(t, "http://localhost:8090/ping")
assert.Equal(t, postResponse.StatusCode, http.StatusMethodNotAllowed)
}
func TestRouter_DELETE(t *testing.T) {
e := NewEngine()
e.Router.DELETE("/ping", func(context *Context) {
data := map[string]interface{}{
"say": "pong",
}
err := context.JSON(data)
if err != nil {
t.Error(e)
}
})
go func() {
e.RunAndListen(":8090")
}()
body, _ := testMakeDELETERequest(t, "http://localhost:8090/ping")
assert.Equal(t, "{\"say\":\"pong\"}", string(body))
// other methods 405 response
_, postResponse := testMakePOSTRequest(t, "http://localhost:8090/ping")
assert.Equal(t, postResponse.StatusCode, http.StatusMethodNotAllowed)
}