-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
46 lines (41 loc) · 849 Bytes
/
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
// Testing units by https://dev.to/koddr/go-fiber-by-examples-testing-the-application-1ldf
package main
import (
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
func Test_main(t *testing.T) {
tests := []struct {
name string
route string
code int
}{
// First test
{
name: "get main route",
route: "/",
code: 200,
},
// Second test
{
name: "get http 404",
route: "/not-found",
code: 404,
},
}
// Define fiber
app := fiber.New()
// Route for first test
app.Get("/", func(c *fiber.Ctx) error {
// return simple response
return c.SendString("Hello!")
})
// main test
for _, test := range tests {
req := httptest.NewRequest("GET", test.route, nil)
resp, _ := app.Test(req, 1)
assert.Equalf(t, test.code, resp.StatusCode, test.name)
}
}