-
Notifications
You must be signed in to change notification settings - Fork 2
/
health_test.go
110 lines (92 loc) · 3.3 KB
/
health_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
package healthcheck
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
// Test_NoConfig go test -run Test_NoConfig
func Test_NoConfig(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.Status(http.StatusNoContent).SendString("test default")
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
var content []byte
content, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "resp.body(req)")
utils.AssertEqual(t, fiber.StatusNoContent, resp.StatusCode, "Status Code")
utils.AssertEqual(t, "", utils.GetString(content), "Textcontent")
}
func Test_NoHeader(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.Status(http.StatusNoContent).SendString("test default")
})
req := httptest.NewRequest("GET", "/", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
var content []byte
content, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "resp.body(req)")
utils.AssertEqual(t, fiber.StatusNoContent, resp.StatusCode, "Status Code")
utils.AssertEqual(t, "", utils.GetString(content), "Textcontent")
}
func Test_Default_Config(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.Status(http.StatusNoContent).SendString("test default")
})
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(DefaultHeaderName, DefaultHeaderValue)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
var content []byte
content, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "resp.body(req)")
utils.AssertEqual(t, DefaultResponseCode, resp.StatusCode, "Status Code")
utils.AssertEqual(t, DefaultResponseText, utils.GetString(content), "Textcontent")
}
func Test_Custom_Config(t *testing.T) {
app := fiber.New()
app.Use(New(Config{
HeaderName: "X-Custom-Header",
HeaderValue: "customValue",
ResponseCode: http.StatusTeapot,
ResponseText: "teapot",
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.Status(http.StatusNoContent).SendString("test default")
})
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("X-Custom-Header", "customValue")
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
var content []byte
content, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "resp.body(req)")
utils.AssertEqual(t, http.StatusTeapot, resp.StatusCode, "Status Code")
utils.AssertEqual(t, "teapot", utils.GetString(content), "Textcontent")
}
func Test_Custom_Config_empty(t *testing.T) {
app := fiber.New()
app.Use(New(Config{}))
app.Get("/", func(c *fiber.Ctx) error {
return c.Status(http.StatusNoContent).SendString("test default")
})
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(DefaultHeaderName, DefaultHeaderValue)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
var content []byte
content, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "resp.body(req)")
utils.AssertEqual(t, DefaultResponseCode, resp.StatusCode, "Status Code")
utils.AssertEqual(t, DefaultResponseText, utils.GetString(content), "Textcontent")
}