-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
242 lines (213 loc) · 6 KB
/
main_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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"io"
"log"
"log/slog"
"net"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
)
// TestMain starts the server and runs all the tests.
// By doing this, you can run **actual** integration tests without starting the server.
func TestMain(m *testing.M) {
flag.Parse() // NOTE: this is needed to parse args from go test command
port := func() string { // Get a free port to run the server
listener, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return strconv.Itoa(addr.Port)
}()
ctx, cancel := context.WithCancel(context.Background())
go func() { // Start the server in a goroutine
if err := run(ctx, os.Stdout, []string{"test", "--port", port}, "vtest"); err != nil {
cancel()
log.Fatal(err)
}
}()
endpoint = "http://localhost:" + port
start := time.Now() // wait for server to be healthy before tests.
for time.Since(start) < 3*time.Second {
if res, err := http.Get(endpoint + "/health"); err == nil && res.StatusCode == http.StatusOK {
break
}
time.Sleep(250 * time.Millisecond)
}
exitCode := m.Run()
cancel()
os.Exit(exitCode)
}
// endpoint holds the server endpoint started by TestMain, not intended to be updated.
var endpoint string
// TestGetHealth tests the /health endpoint.
// Server is started by [TestMain] so that the test can make requests to it.
func TestGetHealth(t *testing.T) {
t.Parallel()
// response is repeated, but this describes intention of test better.
// For example, you can add fields only needed for testing.
type response struct {
Version string `json:"version"`
Revision string `json:"vcs.revision"`
Time time.Time `json:"vcs.time"`
// Modified bool `json:"vcs.modified"`
}
// actual http request to the server.
res, err := http.Get(endpoint + "/health")
testNil(t, err)
t.Cleanup(func() {
err = res.Body.Close()
testNil(t, err)
})
testEqual(t, http.StatusOK, res.StatusCode)
testEqual(t, "application/json", res.Header.Get("Content-Type"))
testNil(t, json.NewDecoder(res.Body).Decode(&response{}))
}
// TestGetOpenAPI tests the /openapi.yaml endpoint.
// You can add more test as needed without starting the server again.
func TestGetOpenAPI(t *testing.T) {
t.Parallel()
res, err := http.Get(endpoint + "/openapi.yaml")
testNil(t, err)
testEqual(t, http.StatusOK, res.StatusCode)
testEqual(t, "application/yaml", res.Header.Get("Content-Type"))
sb := strings.Builder{}
_, err = io.Copy(&sb, res.Body)
testNil(t, err)
t.Cleanup(func() {
err = res.Body.Close()
testNil(t, err)
})
testContains(t, "openapi: 3.1.0", sb.String())
testContains(t, "version: ", sb.String())
}
// TestAccessLogMiddleware tests accesslog middleware
func TestAccessLogMiddleware(t *testing.T) {
t.Parallel()
type record struct {
Method string `json:"method"`
Path string `json:"path"`
Query string `json:"query"`
Status int `json:"status"`
body []byte `json:"-"`
Bytes int `json:"bytes"`
}
tests := []record{
{
Method: "GET",
Path: "/test",
Query: "?key=value",
Status: http.StatusOK,
body: []byte(`{"hello":"world"}`),
},
{
Method: "POST",
Path: "/api",
Status: http.StatusCreated,
body: []byte(`{"id":1}`),
},
{
Method: "DELETE",
Path: "/users/1",
Status: http.StatusNoContent,
},
}
for _, tt := range tests {
name := strings.Join([]string{tt.Method, tt.Path, tt.Query, strconv.Itoa(tt.Status)}, " ")
t.Run(name, func(t *testing.T) {
t.Parallel()
var buffer strings.Builder
handler := accesslog(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tt.Status)
w.Write(tt.body) //nolint:errcheck
}), slog.New(slog.NewJSONHandler(&buffer, nil)))
req := httptest.NewRequest(tt.Method, tt.Path+tt.Query, bytes.NewReader(tt.body))
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
var log record
err := json.NewDecoder(strings.NewReader(buffer.String())).Decode(&log)
testNil(t, err)
testEqual(t, tt.Method, log.Method)
testEqual(t, tt.Path, log.Path)
testEqual(t, strings.TrimPrefix(tt.Query, "?"), log.Query)
testEqual(t, len(tt.body), log.Bytes)
testEqual(t, tt.Status, log.Status)
})
}
}
// TestRecoveryMiddleware tests recovery middleware
func TestRecoveryMiddleware(t *testing.T) {
t.Parallel()
tests := []struct {
name string
hf func(w http.ResponseWriter, r *http.Request)
wantCode int
wantPanic bool
}{
{
name: "no panic on normal http.Handler",
hf: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("success")) //nolint:errcheck
},
wantCode: http.StatusOK,
wantPanic: false,
},
{
name: "no panic on http.ErrAbortHandler",
hf: func(_ http.ResponseWriter, _ *http.Request) {
panic(http.ErrAbortHandler)
},
wantCode: http.StatusOK,
wantPanic: false,
},
{
name: "panic on http.Handler",
hf: func(_ http.ResponseWriter, _ *http.Request) {
panic("something went wrong")
},
wantCode: http.StatusInternalServerError,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var buffer strings.Builder
handler := recovery(http.HandlerFunc(tt.hf), slog.New(slog.NewTextHandler(&buffer, nil)))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
testEqual(t, tt.wantCode, rec.Code)
if tt.wantPanic {
testContains(t, "panic!", buffer.String())
}
})
}
}
func testEqual[T comparable](tb testing.TB, want, got T) {
tb.Helper()
if want != got {
tb.Fatalf("want: %v; got: %v", want, got)
}
}
func testNil(tb testing.TB, err error) {
tb.Helper()
testEqual(tb, nil, err)
}
func testContains(tb testing.TB, needle string, haystack string) {
tb.Helper()
if !strings.Contains(haystack, needle) {
tb.Fatalf("%q not in %q", needle, haystack)
}
}