|
1
|
|
|
package test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bytes" |
|
5
|
|
|
"github.com/gofiber/fiber/v2" |
|
6
|
|
|
"github.com/stretchr/testify/assert" |
|
7
|
|
|
"io/ioutil" |
|
8
|
|
|
"net/http" |
|
9
|
|
|
"testing" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
type Test struct { |
|
13
|
|
|
description string |
|
14
|
|
|
|
|
15
|
|
|
// Test input |
|
16
|
|
|
route string |
|
17
|
|
|
body []byte |
|
18
|
|
|
reqType string |
|
19
|
|
|
// Expected output |
|
20
|
|
|
expectedError bool |
|
21
|
|
|
expectedCode int |
|
22
|
|
|
expectedBody string |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
func testIndex() []Test { |
|
26
|
|
|
|
|
27
|
|
|
tests := []Test{ |
|
28
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
description: "index", |
|
31
|
|
|
route: "/", |
|
32
|
|
|
body: []byte{}, |
|
33
|
|
|
reqType: "GET", |
|
34
|
|
|
expectedCode: fiber.StatusForbidden, |
|
35
|
|
|
expectedBody: "This is not a valid route", |
|
36
|
|
|
expectedError: false, |
|
37
|
|
|
}, |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return tests |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
func TestRoute(t *testing.T) { |
|
44
|
|
|
app, _ := Setup() |
|
45
|
|
|
|
|
46
|
|
|
tests := testIndex() |
|
47
|
|
|
|
|
48
|
|
|
for _, test := range tests { |
|
49
|
|
|
req, _ := http.NewRequest(test.reqType, "/v1/"+test.route, bytes.NewBuffer(test.body)) |
|
50
|
|
|
// Perform the request plain with the app. |
|
51
|
|
|
// The -1 disables request latency. |
|
52
|
|
|
res, err := app.Test(req, -1) |
|
53
|
|
|
|
|
54
|
|
|
// verify that no error occured, that is not expected |
|
55
|
|
|
assert.Equalf(t, test.expectedError, err != nil, test.description) |
|
56
|
|
|
|
|
57
|
|
|
// As expected errors lead to broken responses, the next |
|
58
|
|
|
// test case needs to be processed |
|
59
|
|
|
if test.expectedError { |
|
60
|
|
|
continue |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Verify if the status code is as expected |
|
64
|
|
|
assert.Equalf(t, test.expectedCode, res.StatusCode, test.description) |
|
65
|
|
|
|
|
66
|
|
|
// Read the response body |
|
67
|
|
|
body, err := ioutil.ReadAll(res.Body) |
|
68
|
|
|
|
|
69
|
|
|
// Reading the response body should work everytime, such that |
|
70
|
|
|
// the err variable should be nil |
|
71
|
|
|
assert.Nilf(t, err, test.description) |
|
72
|
|
|
|
|
73
|
|
|
// Verify, that the reponse body equals the expected body |
|
74
|
|
|
assert.Equalf(t, test.expectedBody, string(body), test.description) |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|