-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
113 lines (93 loc) · 2.76 KB
/
main_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
package main
import (
"bytes"
"dam-webhook/webhook"
"io/ioutil"
"net/http"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
type SingleTestCase struct {
description string
route string
method string
expectedError bool
expectedCode int
checkBody bool
expectedBody string
jsonValue string
}
func TestIndexRoute(t *testing.T) {
// Define a structure for specifying input and output
// data of a single test case. This structure is then used
// to create a so called test map, which contains all test
// cases, that should be run for testing this function
tests := []SingleTestCase{
{
description: "hello world test",
route: "/",
method: "GET",
expectedError: false,
expectedCode: 200,
checkBody: true,
expectedBody: "hello world",
jsonValue: ``,
},
{
description: "webhook post",
route: "/webhook",
method: "POST",
expectedError: false,
expectedCode: 200,
checkBody: false,
expectedBody: "",
jsonValue: `{"assetId": "87c23cwqDD2111", "metadata": {"folderPath": "/Client/XXX Group Holding SE & Co. KGaA/my-image-name-jpg", "cf_approvalState_client1": "Approved", "cf_assetType": {"value": "Content Image"}}}`,
},
}
// Setup the app as it is done in the main function
app := Setup()
// Iterate through test single test cases
for _, test := range tests {
// Create a new http request with the route
// from the test case
payload := bytes.NewBuffer([]byte(test.jsonValue))
req, _ := http.NewRequest(
test.method,
test.route,
payload,
)
req.Header.Set("Content-Type", "application/json")
// Perform the request plain with the app.
// The -1 disables request latency.
res, err := app.Test(req, -1)
// verify that no error occured, that is not expected
assert.Equalf(t, test.expectedError, err != nil, test.description)
// As expected errors lead to broken responses, the next
// test case needs to be processed
if test.expectedError {
continue
}
// Verify if the status code is as expected
assert.Equalf(t, test.expectedCode, res.StatusCode, test.description)
// Read the response body
body, err := ioutil.ReadAll(res.Body)
// Reading the response body should work everytime, such that
// the err variable should be nil
assert.Nilf(t, err, test.description)
// Verify, that the reponse body equals the expected body
if test.checkBody {
assert.Equalf(t, test.expectedBody, string(body), test.description)
}
}
}
func Setup() *fiber.App {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("hello world")
})
app.Post("/webhook", webhook.CreateWebhook)
app.Post("/mock-api", webhook.MockAPI)
readConfig("testing")
return app
}