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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package server
import (
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"codeberg.org/mihna123/openstore/internal/config"
)
func TestMain(m *testing.M) {
slog.SetDefault(slog.New(slog.DiscardHandler))
os.Exit(m.Run())
}
func newTestConfig(t *testing.T) *config.Config {
t.Helper()
return &config.Config{
Serve: config.Serve{
Addr: ":3000",
},
Instance: config.Instance{
Name: "TestStore",
ContactEmail: "test@email.com",
BaseURL: "https://example.com",
},
}
}
func newTestServer(t *testing.T) *Server {
t.Helper()
return New(newTestConfig(t))
}
func TestRoutes(t *testing.T) {
s := newTestServer(t)
tests := []struct {
name string
method string
path string
want int
}{
{"index", http.MethodGet, "/", http.StatusOK},
{"index style", http.MethodGet, "/css/style.css", http.StatusOK},
{"unknown path", http.MethodGet, "/nonsense", http.StatusNotFound},
{"wrong method", http.MethodPost, "/", http.StatusMethodNotAllowed},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
s.mux.ServeHTTP(rec, httptest.NewRequest(tt.method, tt.path, nil))
if rec.Code != tt.want {
t.Errorf("%s %s: got status %d, want %d", tt.method, tt.path, rec.Code, tt.want)
}
})
}
}
func TestIndexRendersTemplate(t *testing.T) {
cfg := newTestConfig(t)
cfg.Instance.Name = "testname123"
s := New(cfg)
rec := httptest.NewRecorder()
s.mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
body := rec.Body.String()
if !strings.Contains(body, "testname123") {
t.Errorf("index body doesn't contain server name, got:\n%s", body)
}
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
t.Errorf("got Content-Type %q, want text/html", ct)
}
}
func TestStatusRecorder(t *testing.T) {
t.Run("records and forwards WriteHeader", func(t *testing.T) {
rec := httptest.NewRecorder()
sr := &statusRecorder{ResponseWriter: rec, status: http.StatusOK}
sr.WriteHeader(http.StatusTeapot)
if sr.status != http.StatusTeapot {
t.Errorf("recorder kept status %d, want %d", sr.status, http.StatusTeapot)
}
if rec.Code != http.StatusTeapot {
t.Errorf("underlying writer got status %d, want %d", rec.Code, http.StatusTeapot)
}
})
t.Run("defaults to 200 when handler never calls WriteHeader", func(t *testing.T) {
rec := httptest.NewRecorder()
sr := &statusRecorder{ResponseWriter: rec, status: http.StatusOK}
if _, err := sr.Write([]byte("hello")); err != nil {
t.Fatalf("Write: %v", err)
}
if sr.status != http.StatusOK {
t.Errorf("recorder kept status %d, want %d", sr.status, http.StatusOK)
}
})
}
// TestLogRequests checks the middleware passes requests through untouched,
// on both the Write path (200) and the WriteHeader path (404).
func TestLogRequests(t *testing.T) {
s := newTestServer(t)
handler := logRequests(s.mux)
tests := []struct {
name string
path string
want int
}{
{"success passes through", "/", http.StatusOK},
{"not found passes through", "/nonsense", http.StatusNotFound},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, tt.path, nil))
if rec.Code != tt.want {
t.Errorf("got status %d, want %d", rec.Code, tt.want)
}
})
}
}
|