-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_test.go
69 lines (60 loc) · 2.12 KB
/
mux_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
package security_test
import (
"context"
"embed"
"net/http/httptest"
"testing"
"github.com/andygeiss/cloud-native-utils/assert"
"github.com/andygeiss/cloud-native-utils/security"
)
//go:embed testdata
var efs embed.FS
func TestServeMux_Is_Not_Nil(t *testing.T) {
ctx := context.Background()
mux, _ := security.NewServeMux(ctx, efs)
assert.That(t, "mux must not be nil", mux != nil, true)
}
func TestServeMux_Has_Health_Check(t *testing.T) {
ctx := context.Background()
mux, _ := security.NewServeMux(ctx, efs)
req := httptest.NewRequest("GET", "/liveness", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.That(t, "status code must be 200", w.Code, 200)
assert.That(t, "body must be OK", w.Body.String(), "OK")
}
func TestServeMux_Has_Readiness_Check_When_Context_Active(t *testing.T) {
ctx := context.Background()
mux, _ := security.NewServeMux(ctx, efs)
req := httptest.NewRequest("GET", "/readiness", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.That(t, "status code must be 200", w.Code, 200)
// The body is empty in this example, but you can also check it if needed.
}
func TestServeMux_Has_Readiness_Check_When_Context_Canceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // Immediately cancel the context.
mux, _ := security.NewServeMux(ctx, efs)
req := httptest.NewRequest("GET", "/readiness", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.That(t, "status code must be 503", w.Code, 503)
}
func TestServeMux_Unknown_Route(t *testing.T) {
ctx := context.Background()
mux, _ := security.NewServeMux(ctx, efs)
req := httptest.NewRequest("GET", "/unknown", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.That(t, "status code must be 404", w.Code, 404)
}
func TestServeMux_Has_Static_Assets(t *testing.T) {
ctx := context.Background()
mux, _ := security.NewServeMux(ctx, efs)
req := httptest.NewRequest("GET", "/testdata/server.crt", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.That(t, "status code must be 200", w.Code, 200)
assert.That(t, "body length must be correct", len(w.Body.String()), 1598)
}