forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsuite.go
399 lines (355 loc) Β· 12 KB
/
testsuite.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
package goyave
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
"gorm.io/gorm"
"goyave.dev/goyave/v3/database"
"goyave.dev/goyave/v3/helper/filesystem"
"github.com/stretchr/testify/assert"
testify "github.com/stretchr/testify/suite"
"goyave.dev/goyave/v3/config"
"goyave.dev/goyave/v3/lang"
)
// ITestSuite is an extension of testify's Suite for
// Goyave-specific testing.
type ITestSuite interface {
RunServer(func(*Router), func())
Timeout() time.Duration
SetTimeout(time.Duration)
Middleware(Middleware, *Request, Handler) *http.Response
Get(string, map[string]string) (*http.Response, error)
Post(string, map[string]string, io.Reader) (*http.Response, error)
Put(string, map[string]string, io.Reader) (*http.Response, error)
Patch(string, map[string]string, io.Reader) (*http.Response, error)
Delete(string, map[string]string, io.Reader) (*http.Response, error)
Request(string, string, map[string]string, io.Reader) (*http.Response, error)
T() *testing.T
SetT(*testing.T)
GetBody(*http.Response) []byte
GetJSONBody(*http.Response, interface{}) error
CreateTestFiles(paths ...string) []filesystem.File
WriteFile(*multipart.Writer, string, string, string)
WriteField(*multipart.Writer, string, string)
CreateTestRequest(*http.Request) *Request
CreateTestResponse(http.ResponseWriter) *Response
getHTTPClient() *http.Client
}
// TestSuite is an extension of testify's Suite for
// Goyave-specific testing.
type TestSuite struct {
testify.Suite
httpClient *http.Client
timeout time.Duration // Timeout for functional tests
mu sync.Mutex
}
var _ ITestSuite = (*TestSuite)(nil) // implements ITestSuite
// Use a mutex to avoid parallel goyave test suites to be run concurrently.
var mu sync.Mutex
// Timeout get the timeout for test failure when using RunServer or requests.
func (s *TestSuite) Timeout() time.Duration {
s.mu.Lock()
defer s.mu.Unlock()
return s.timeout
}
// SetTimeout set the timeout for test failure when using RunServer or requests.
func (s *TestSuite) SetTimeout(timeout time.Duration) {
s.mu.Lock()
s.timeout = timeout
s.mu.Unlock()
}
// CreateTestRequest create a "goyave.Request" from the given raw request.
// This function is aimed at making it easier to unit test Requests.
//
// If passed request is "nil", a default GET request to "/" is used.
//
// rawRequest := httptest.NewRequest("GET", "/test-route", nil)
// rawRequest.Header.Set("Content-Type", "application/json")
// request := goyave.CreateTestRequest(rawRequest)
// request.Lang = "en-US"
// request.Data = map[string]interface{}{"field": "value"}
func (s *TestSuite) CreateTestRequest(rawRequest *http.Request) *Request {
if rawRequest == nil {
rawRequest = httptest.NewRequest("GET", "/", nil)
}
return &Request{
httpRequest: rawRequest,
route: nil,
Data: nil,
Rules: nil,
Lang: "en-US",
Params: map[string]string{},
Extra: map[string]interface{}{},
}
}
// CreateTestResponse create an empty response with the given response writer.
// This function is aimed at making it easier to unit test Responses.
//
// writer := httptest.NewRecorder()
// response := suite.CreateTestResponse(writer)
// response.Status(http.StatusNoContent)
// result := writer.Result()
// fmt.Println(result.StatusCode) // 204
func (s *TestSuite) CreateTestResponse(recorder http.ResponseWriter) *Response {
return newResponse(recorder, nil)
}
// CreateTestResponseWithRequest create an empty response with the given response writer HTTP request.
// This function is aimed at making it easier to unit test Responses needing the raw request's
// information, such as redirects.
//
// writer := httptest.NewRecorder()
// rawRequest := httptest.NewRequest("POST", "/test-route", strings.NewReader("body"))
// response := suite.CreateTestResponseWithRequest(writer, rawRequest)
// response.Status(http.StatusNoContent)
// result := writer.Result()
// fmt.Println(result.StatusCode) // 204
func (s *TestSuite) CreateTestResponseWithRequest(recorder http.ResponseWriter, rawRequest *http.Request) *Response {
return newResponse(recorder, rawRequest)
}
// RunServer start the application and run the given functional test procedure.
//
// This function is the equivalent of "goyave.Start()".
// The test fails if the suite's timeout is exceeded.
// The server automatically shuts down when the function ends.
// This function is synchronized, that means that the server is properly stopped
// when the function returns.
func (s *TestSuite) RunServer(routeRegistrer func(*Router), procedure func()) {
c := make(chan bool, 1)
c2 := make(chan bool, 1)
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout())
defer cancel()
RegisterStartupHook(func() {
procedure()
if ctx.Err() == nil {
Stop()
c <- true
}
})
go func() {
if err := Start(routeRegistrer); err != nil {
s.Fail(err.Error())
c <- true
}
c2 <- true
}()
select {
case <-ctx.Done():
s.Fail("Timeout exceeded in goyave.TestSuite.RunServer")
Stop()
case sig := <-c:
s.True(sig)
}
ClearStartupHooks()
<-c2
}
// Middleware executes the given middleware and returns the HTTP response.
// Core middleware (recovery, parsing and language) is not executed.
func (s *TestSuite) Middleware(middleware Middleware, request *Request, procedure Handler) *http.Response {
cacheCriticalConfig()
recorder := httptest.NewRecorder()
response := s.CreateTestResponse(recorder)
router := NewRouter()
router.Middleware(middleware)
middleware(procedure)(response, request)
router.finalize(response, request)
return recorder.Result()
}
// Get execute a GET request on the given route.
// Headers are optional.
func (s *TestSuite) Get(route string, headers map[string]string) (*http.Response, error) {
return s.Request(http.MethodGet, route, headers, nil)
}
// Post execute a POST request on the given route.
// Headers and body are optional.
func (s *TestSuite) Post(route string, headers map[string]string, body io.Reader) (*http.Response, error) {
return s.Request(http.MethodPost, route, headers, body)
}
// Put execute a PUT request on the given route.
// Headers and body are optional.
func (s *TestSuite) Put(route string, headers map[string]string, body io.Reader) (*http.Response, error) {
return s.Request(http.MethodPut, route, headers, body)
}
// Patch execute a PATCH request on the given route.
// Headers and body are optional.
func (s *TestSuite) Patch(route string, headers map[string]string, body io.Reader) (*http.Response, error) {
return s.Request(http.MethodPatch, route, headers, body)
}
// Delete execute a DELETE request on the given route.
// Headers and body are optional.
func (s *TestSuite) Delete(route string, headers map[string]string, body io.Reader) (*http.Response, error) {
return s.Request(http.MethodDelete, route, headers, body)
}
// Request execute a request on the given route.
// Headers and body are optional.
func (s *TestSuite) Request(method, route string, headers map[string]string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, BaseURL()+route, body)
if err != nil {
return nil, err
}
req.Close = true
for k, v := range headers {
req.Header.Set(k, v)
}
return s.getHTTPClient().Do(req)
}
// GetBody read the whole body of a response.
// If read failed, test fails and return empty byte slice.
func (s *TestSuite) GetBody(response *http.Response) []byte {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
s.Fail("Couldn't read response body", err)
}
return body
}
// GetJSONBody read the whole body of a response and decode it as JSON.
// If read or decode failed, test fails.
func (s *TestSuite) GetJSONBody(response *http.Response, data interface{}) error {
err := json.NewDecoder(response.Body).Decode(data)
if err != nil {
s.Fail("Couldn't read response body as JSON", err)
return err
}
return nil
}
// CreateTestFiles create a slice of "filesystem.File" from the given paths.
// Files are passed to a temporary http request and parsed as Multipart form,
// to reproduce the way files are obtained in real scenarios.
func (s *TestSuite) CreateTestFiles(paths ...string) []filesystem.File {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, p := range paths {
s.WriteFile(writer, p, "file", filepath.Base(p))
}
err := writer.Close()
if err != nil {
panic(err)
}
req, _ := http.NewRequest("POST", "/test-route", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
if err := req.ParseMultipartForm(10 << 20); err != nil {
panic(err)
}
return filesystem.ParseMultipartFiles(req, "file")
}
// WriteFile write a file to the given writer.
// This function is handy for file upload testing.
// The test fails if an error occurred.
func (s *TestSuite) WriteFile(writer *multipart.Writer, path, fieldName, fileName string) {
file, err := os.Open(path)
if err != nil {
s.Fail(err.Error())
return
}
defer file.Close()
part, err := writer.CreateFormFile(fieldName, fileName)
if err != nil {
s.Fail(err.Error())
return
}
_, err = io.Copy(part, file)
if err != nil {
s.Fail(err.Error())
}
}
// WriteField create and write a new multipart form field.
// The test fails if the field couldn't be written.
func (s *TestSuite) WriteField(writer *multipart.Writer, fieldName, value string) {
if err := writer.WriteField(fieldName, value); err != nil {
s.Fail(err.Error())
}
}
// getHTTPClient get suite's http client or create it if it doesn't exist yet.
// The HTTP client is created with a timeout, disabled redirect and disabled TLS cert checking.
func (s *TestSuite) getHTTPClient() *http.Client {
config := &tls.Config{
InsecureSkipVerify: true,
}
if s.httpClient == nil {
s.httpClient = &http.Client{
Timeout: s.Timeout(),
Transport: &http.Transport{TLSClientConfig: config},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
return s.httpClient
}
// ClearDatabase delete all records in all tables.
// This function only clears the tables of registered models.
func (s *TestSuite) ClearDatabase() {
db := database.GetConnection()
for _, m := range database.GetRegisteredModels() {
tx := db.Unscoped().Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(m)
if tx.Error != nil {
panic(tx.Error)
}
}
}
// ClearDatabaseTables drop all tables.
// This function only clears the tables of registered models.
func (s *TestSuite) ClearDatabaseTables() {
db := database.GetConnection()
for _, m := range database.GetRegisteredModels() {
if err := db.Migrator().DropTable(m); err != nil {
panic(err)
}
}
}
// RunTest run a test suite with prior initialization of a test environment.
// The GOYAVE_ENV environment variable is automatically set to "test" and restored
// to its original value at the end of the test run.
// All tests are run using your project's root as working directory. This directory is determined
// by the presence of a "go.mod" file.
func RunTest(t *testing.T, suite ITestSuite) bool {
mu.Lock()
defer mu.Unlock()
if suite.Timeout() == 0 {
suite.SetTimeout(5 * time.Second)
}
oldEnv := os.Getenv("GOYAVE_ENV")
os.Setenv("GOYAVE_ENV", "test")
defer os.Setenv("GOYAVE_ENV", oldEnv)
setRootWorkingDirectory()
if !config.IsLoaded() {
if err := config.Load(); err != nil {
return assert.Fail(t, "Failed to load config", err)
}
}
defer config.Clear()
lang.LoadDefault()
lang.LoadAllAvailableLanguages()
if config.GetBool("database.autoMigrate") && config.GetString("database.connection") != "none" {
database.Migrate()
}
testify.Run(t, suite)
database.Close()
return !t.Failed()
}
func setRootWorkingDirectory() {
sep := string(os.PathSeparator)
_, filename, _, _ := runtime.Caller(2)
directory := path.Dir(filename) + sep
for !filesystem.FileExists(directory + sep + "go.mod") {
directory += ".." + sep
if !filesystem.IsDirectory(directory) {
panic("Couldn't find project's root directory.")
}
}
if err := os.Chdir(directory); err != nil {
panic(err)
}
}