forked from revel/modules
/
test_suite.go
415 lines (359 loc) · 12.9 KB
/
test_suite.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package testsuite
// The TestSuite container is a container to relay information between the server and the caller.
import (
"bytes"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/textproto"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/revel/revel"
"github.com/revel/revel/session"
"golang.org/x/net/websocket"
)
// Start a new request, with a new session.
func NewTestSuite(t *testing.T) *TestSuite {
return NewTestSuiteEngine(revel.NewSessionCookieEngine(), t)
}
// Define a new test suite with a custom session engine.
func NewTestSuiteEngine(engine revel.SessionEngine, t *testing.T) *TestSuite {
jar, _ := cookiejar.New(nil)
ts := &TestSuite{
Client: &http.Client{Jar: jar},
Session: session.NewSession(),
SessionEngine: engine,
T: t,
ResponseChannel: make(chan bool, 1),
}
return ts
}
// TestSuite container.
type TestSuite struct {
Response *httptest.ResponseRecorder // The response recorder
ResponseChannel chan bool // The response channel
Session session.Session // The session
SessionEngine revel.SessionEngine // The session engine
Sent bool // True if sent
T *testing.T // The test to handle any errors
Client *http.Client // The client to extract the cookie data
}
// NewTestRequest returns an initialized *TestRequest. It is used for extending
// testsuite package making it possible to define own methods. Example:
// type MyTestSuite struct {
// testing.TestSuite
// }
//
// func (t *MyTestSuite) PutFormCustom(...) {
// req := http.NewRequest(...)
// ...
// return t.NewTestRequest(req)
// }
func (t *TestSuite) NewTestRequest(req *http.Request) *TestRequest {
return &TestRequest{
Request: req,
testSuite: t,
}
}
// Host returns the address and port of the server, e.g. "127.0.0.1:8557".
func (t *TestSuite) Host() string {
if revel.ServerEngineInit.Address[0] == ':' {
return "127.0.0.1" + revel.ServerEngineInit.Address
}
return revel.ServerEngineInit.Address
}
// BaseUrl returns the base http/https URL of the server, e.g. "http://127.0.0.1:8557".
// The scheme is set to https if http.ssl is set to true in the configuration file.
func (t *TestSuite) BaseUrl() string {
if revel.HTTPSsl {
return "https://" + t.Host()
}
return "http://" + t.Host()
}
// WebSocketUrl returns the base websocket URL of the server, e.g. "ws://127.0.0.1:8557"
func (t *TestSuite) WebSocketUrl() string {
return "ws://" + t.Host()
}
// Get issues a GET request to the given path and stores the result in Response
// and ResponseBody.
func (t *TestSuite) Get(path string) *TestSuite {
t.GetCustom(t.BaseUrl() + path).Send()
return t
}
// GetCustom returns a GET request to the given URI in a form of its wrapper.
func (t *TestSuite) GetCustom(uri string) *TestRequest {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
panic(err)
}
return t.NewTestRequest(req)
}
// Delete issues a DELETE request to the given path and stores the result in
// Response and ResponseBody.
func (t *TestSuite) Delete(path string) {
t.DeleteCustom(t.BaseUrl() + path).Send()
}
// DeleteCustom returns a DELETE request to the given URI in a form of its
// wrapper.
func (t *TestSuite) DeleteCustom(uri string) *TestRequest {
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
panic(err)
}
return t.NewTestRequest(req)
}
// Put issues a PUT request to the given path, sending the given Content-Type
// and data, storing the result in Response and ResponseBody. "data" may be nil.
func (t *TestSuite) Put(path string, contentType string, reader io.Reader) {
t.PutCustom(t.BaseUrl()+path, contentType, reader).Send()
}
// PutCustom returns a PUT request to the given URI with specified Content-Type
// and data in a form of wrapper. "data" may be nil.
func (t *TestSuite) PutCustom(uri string, contentType string, reader io.Reader) *TestRequest {
req, err := http.NewRequest("PUT", uri, reader)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", contentType)
return t.NewTestRequest(req)
}
// PutForm issues a PUT request to the given path as a form put of the given key
// and values, and stores the result in Response and ResponseBody.
func (t *TestSuite) PutForm(path string, data url.Values) *TestSuite {
t.PutFormCustom(t.BaseUrl()+path, data).Send()
return t
}
// PutFormCustom returns a PUT request to the given URI as a form put of the
// given key and values. The request is in a form of TestRequest wrapper.
func (t *TestSuite) PutFormCustom(uri string, data url.Values) *TestRequest {
return t.PutCustom(uri, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// Patch issues a PATCH request to the given path, sending the given
// Content-Type and data, and stores the result in Response and ResponseBody.
// "data" may be nil.
func (t *TestSuite) Patch(path string, contentType string, reader io.Reader) *TestSuite {
t.PatchCustom(t.BaseUrl()+path, contentType, reader).Send()
return t
}
// PatchCustom returns a PATCH request to the given URI with specified
// Content-Type and data in a form of wrapper. "data" may be nil.
func (t *TestSuite) PatchCustom(uri string, contentType string, reader io.Reader) *TestRequest {
req, err := http.NewRequest("PATCH", uri, reader)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", contentType)
return t.NewTestRequest(req)
}
// Post issues a POST request to the given path, sending the given Content-Type
// and data, storing the result in Response and ResponseBody. "data" may be nil.
func (t *TestSuite) Post(path string, contentType string, reader io.Reader) *TestSuite {
t.PostCustom(t.BaseUrl()+path, contentType, reader).Send()
return t
}
// PostCustom returns a POST request to the given URI with specified
// Content-Type and data in a form of wrapper. "data" may be nil.
func (t *TestSuite) PostCustom(uri string, contentType string, reader io.Reader) *TestRequest {
req, err := http.NewRequest("POST", uri, reader)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", contentType)
return t.NewTestRequest(req)
}
// PostForm issues a POST request to the given path as a form post of the given
// key and values, and stores the result in Response and ResponseBody.
func (t *TestSuite) PostForm(path string, data url.Values) *TestSuite {
t.PostFormCustom(t.BaseUrl()+path, data).Send()
return t
}
// PostFormCustom returns a POST request to the given URI as a form post of the
// given key and values. The request is in a form of TestRequest wrapper.
func (t *TestSuite) PostFormCustom(uri string, data url.Values) *TestRequest {
return t.PostCustom(uri, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// PostFile issues a multipart request to the given path sending given params
// and files, and stores the result in Response and ResponseBody.
func (t *TestSuite) PostFile(path string, params url.Values, filePaths url.Values) *TestSuite {
t.PostFileCustom(t.BaseUrl()+path, params, filePaths).Send()
return t
}
// PostFileCustom returns a multipart request to the given URI in a form of its
// wrapper with the given params and files.
func (t *TestSuite) PostFileCustom(uri string, params url.Values, filePaths url.Values) *TestRequest {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key, values := range filePaths {
for _, value := range values {
createFormFile(writer, key, value)
}
}
for key, values := range params {
for _, value := range values {
err := writer.WriteField(key, value)
t.AssertEqual(nil, err)
}
}
err := writer.Close()
t.AssertEqual(nil, err)
return t.PostCustom(uri, writer.FormDataContentType(), body)
}
// Send issues any request and reads the response. If successful, the caller may
// examine the Response and ResponseBody properties. Session data will be
// added to the request cookies for you.
func (r *TestRequest) Send() *TestRequest {
writer := httptest.NewRecorder()
context := revel.NewGoContext(nil)
context.Request.SetRequest(r.Request)
context.Response.SetResponse(writer)
controller := revel.NewController(context)
controller.Session = r.testSuite.Session
r.testSuite.SessionEngine.Encode(controller)
response := http.Response{Header: writer.Header()}
cookies := response.Cookies()
for _, c := range cookies {
r.AddCookie(c)
}
r.MakeRequest()
return r
}
// MakeRequest issues any request and read the response. If successful, the
// caller may examine the Response and ResponseBody properties. You will need to
// manage session / cookie data manually.
func (r *TestRequest) MakeRequest() *TestRequest {
revel.CurrentEngine.(*GoHttpServer).TestChannel <- r
<-r.testSuite.ResponseChannel
r.Sent = true
// Create the controller again to receive the response for processing.
context := revel.NewGoContext(nil)
// Set the request with the header from the response..
newRequest := &http.Request{URL: r.URL, Header: r.testSuite.Response.Header()}
for _, cookie := range r.testSuite.Client.Jar.Cookies(r.Request.URL) {
newRequest.AddCookie(cookie)
}
context.Request.SetRequest(newRequest)
context.Response.SetResponse(httptest.NewRecorder())
controller := revel.NewController(context)
// Decode the session data from the controller and assign it to the session
r.testSuite.SessionEngine.Decode(controller)
r.testSuite.Session = controller.Session
return r
}
// WebSocket creates a websocket connection to the given path and returns it.
func (t *TestSuite) WebSocket(path string) *websocket.Conn {
t.Assertf(true, "Web Socket Not implemented at this time")
origin := t.BaseUrl() + "/"
url := t.WebSocketUrl() + path
ws, err := websocket.Dial(url, "", origin)
if err != nil {
panic(err)
}
return ws
}
func (t *TestSuite) AssertOk() {
t.AssertStatus(http.StatusOK)
}
func (t *TestSuite) AssertNotFound() {
t.AssertStatus(http.StatusNotFound)
}
func (t *TestSuite) AssertStatus(status int) {
if t.Response.Code != status {
panic(fmt.Errorf("status: (expected) %d != %d (actual)", status, t.Response.Code))
}
}
func (t *TestSuite) AssertContentType(contentType string) {
t.AssertHeader("Content-Type", contentType)
}
func (t *TestSuite) AssertHeader(name, value string) {
actual := t.Response.Result().Header.Get(name)
if actual != value {
panic(fmt.Errorf("header %s: (expected) %s != %s (actual)", name, value, actual))
}
}
func (t *TestSuite) AssertEqual(expected, actual interface{}) {
if !revel.Equal(expected, actual) {
panic(fmt.Errorf("(expected) %v != %v (actual)", expected, actual))
}
}
func (t *TestSuite) AssertNotEqual(expected, actual interface{}) {
if revel.Equal(expected, actual) {
panic(fmt.Errorf("(expected) %v == %v (actual)", expected, actual))
}
}
func (t *TestSuite) Assert(exp bool) {
t.Assertf(exp, "Assertion failed")
}
func (t *TestSuite) Assertf(exp bool, formatStr string, args ...interface{}) {
if !exp {
panic(fmt.Errorf(formatStr, args...))
}
}
// AssertContains asserts that the response contains the given string.
func (t *TestSuite) AssertContains(s string) {
if !bytes.Contains(t.Response.Body.Bytes(), []byte(s)) {
panic(fmt.Errorf("assertion failed. Expected response to contain %s", s))
}
}
// AssertNotContains asserts that the response does not contain the given string.
func (t *TestSuite) AssertNotContains(s string) {
if bytes.Contains(t.Response.Body.Bytes(), []byte(s)) {
panic(fmt.Errorf("assertion failed. Expected response not to contain %s", s))
}
}
// AssertContainsRegex asserts that the response matches the given regular expression.
func (t *TestSuite) AssertContainsRegex(regex string) {
r := regexp.MustCompile(regex)
if !r.Match(t.Response.Body.Bytes()) {
panic(fmt.Errorf("assertion failed. Expected response to match regexp %s", regex))
}
}
func createFormFile(writer *multipart.Writer, fieldname, filename string) {
// Try to open the file.
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer func() {
_ = file.Close()
}()
// Create a new form-data header with the provided field name and file name.
// Determine Content-Type of the file by its extension.
h := textproto.MIMEHeader{}
h.Set("Content-Disposition", fmt.Sprintf(
`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname),
escapeQuotes(filepath.Base(filename)),
))
h.Set("Content-Type", "application/octet-stream")
if ct := mime.TypeByExtension(filepath.Ext(filename)); ct != "" {
h.Set("Content-Type", ct)
}
part, err := writer.CreatePart(h)
if err != nil {
panic(err)
}
// Copy the content of the file we have opened not reading the whole
// file into memory.
_, err = io.Copy(part, file)
if err != nil {
panic(err)
}
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
// This function was borrowed from mime/multipart package.
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
type TestRequest struct {
*http.Request
testSuite *TestSuite
Sent bool
}