-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
83 lines (77 loc) · 2.2 KB
/
main_test.go
File metadata and controls
83 lines (77 loc) · 2.2 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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNewMuxRegistersRoutes(t *testing.T) {
mux := newMux()
tests := []struct {
name string
target string
assertions func(*testing.T, *httptest.ResponseRecorder)
}{
{
name: "browser viewer",
target: "/",
assertions: func(t *testing.T, recorder *httptest.ResponseRecorder) {
t.Helper()
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "<!DOCTYPE html>") {
t.Fatal("expected viewer HTML in response")
}
},
},
{
name: "namespaces api",
target: "/api/namespaces",
assertions: func(t *testing.T, recorder *httptest.ResponseRecorder) {
t.Helper()
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", recorder.Code)
}
var payload map[string]any
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatalf("response is not valid JSON: %v", err)
}
if _, ok := payload["namespaces"]; !ok {
t.Fatal("expected namespaces key in response")
}
},
},
{
name: "websocket endpoint",
target: "/ws",
assertions: func(t *testing.T, recorder *httptest.ResponseRecorder) {
t.Helper()
if recorder.Code == http.StatusOK || recorder.Code == http.StatusSwitchingProtocols {
t.Fatalf("expected websocket upgrade failure for plain HTTP request, got %d", recorder.Code)
}
},
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, testCase.target, nil)
recorder := httptest.NewRecorder()
mux.ServeHTTP(recorder, request)
testCase.assertions(t, recorder)
})
}
}
func TestNewServerConfiguresReadHeaderTimeout(t *testing.T) {
server := newServer("127.0.0.1:9999")
if server.Addr != "127.0.0.1:9999" {
t.Fatalf("Addr = %q, want %q", server.Addr, "127.0.0.1:9999")
}
if server.ReadHeaderTimeout != readHeaderTimeout {
t.Fatalf("ReadHeaderTimeout = %v, want %v", server.ReadHeaderTimeout, readHeaderTimeout)
}
if server.Handler == nil {
t.Fatal("expected server handler to be configured")
}
}