-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathserver_test.go
More file actions
50 lines (45 loc) · 910 Bytes
/
server_test.go
File metadata and controls
50 lines (45 loc) · 910 Bytes
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
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSetupServer(t *testing.T) {
b := new(bytes.Buffer)
mux := http.NewServeMux()
wrappedMux := setupServer(mux, b)
ts := httptest.NewServer(wrappedMux)
defer ts.Close()
resp, err := http.Get(ts.URL + "/panic")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf(
"Expected response status to be: %v, Got: %v",
http.StatusInternalServerError,
resp.StatusCode,
)
}
logs := b.String()
expectedLogFragments := []string{
"path=/panic method=GET duration=",
"panic detected",
}
for _, log := range expectedLogFragments {
if !strings.Contains(logs, log) {
t.Errorf(
"Expected logs to contain: %s, Got: %s",
log, logs,
)
}
}
}