Repository navigation
Expand file tree
/
Copy pathstatus_test.go
More file actions
99 lines (84 loc) · 2.36 KB
/
Copy pathstatus_test.go
File metadata and controls
99 lines (84 loc) · 2.36 KB
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
package main
import (
"io"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestHealthEndpoints(t *testing.T) {
tests := []TestCase{
{
query: "GET /livez",
expectedCode: 200,
expectedBody: "OK",
expectedContentType: "text/plain; charset=utf-8",
},
{
query: "GET /readyz",
expectedCode: 200,
expectedBody: "OK",
expectedContentType: "text/plain; charset=utf-8",
},
{
query: "GET /v1/status",
expectedCode: 204,
expectedBody: "",
expectedContentType: "",
},
}
runTestCases(t, tests)
}
func TestStatusCacheHeaders(t *testing.T) {
loadFixtures(t)
req, err := newTestRequest("GET", "/v1/status", nil)
require.NoError(t, err)
res, err := app.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, 204, res.StatusCode)
assert.Equal(t, "no-cache", res.Header.Get("Cache-Control"))
}
func TestHealthEndpointsWhenDatabaseUnavailable(t *testing.T) {
unavailableDB, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
require.NoError(t, err)
pool, err := unavailableDB.DB()
require.NoError(t, err)
require.NoError(t, pool.Close())
healthApp := fiber.New()
healthApp.Use(newHealthcheck(unavailableDB))
t.Cleanup(func() { require.NoError(t, healthApp.Shutdown()) })
tests := []struct {
path string
expectedCode int
expectedBody string
expectedContentType string
}{
{
path: "/livez",
expectedCode: 200,
expectedBody: "OK",
expectedContentType: "text/plain; charset=utf-8",
},
{
path: "/readyz",
expectedCode: 503,
expectedBody: "Service Unavailable",
expectedContentType: "text/plain; charset=utf-8",
},
}
for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
req, err := newTestRequest("GET", test.path, nil)
require.NoError(t, err)
res, err := healthApp.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, test.expectedCode, res.StatusCode)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
assert.Equal(t, test.expectedBody, string(body))
assert.Equal(t, test.expectedContentType, res.Header.Get("Content-Type"))
})
}
}