-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
82 lines (73 loc) · 1.84 KB
/
server_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
type testLogger struct {
t *testing.T
}
func newTestLogger(t *testing.T) *testLogger {
return &testLogger{t: t}
}
func (l *testLogger) Criticalf(format string, args ...interface{}) {
l.t.Logf("CRITICAL: "+format, args...)
}
func (l *testLogger) Debugf(format string, args ...interface{}) {
l.t.Logf("DEBUG: "+format, args...)
}
func (l *testLogger) Errorf(format string, args ...interface{}) {
l.t.Logf("ERROR: "+format, args...)
}
func (l *testLogger) Infof(format string, args ...interface{}) {
l.t.Logf("INFO: "+format, args...)
}
func (l *testLogger) Warningf(format string, args ...interface{}) {
l.t.Logf("WARNING: "+format, args...)
}
var _ logger = (*testLogger)(nil)
func useTestDefaults(t *testing.T) func(*server) error {
return func(s *server) error {
s.log = newTestLogger(t)
s.mux = &http.ServeMux{}
s.client = http.DefaultClient
return nil
}
}
var (
testServer http.Handler
addr string
once sync.Once
)
func startServer(t *testing.T) {
once.Do(func() {
s, err := newServer(useTestDefaults(t))
if err != nil {
t.Fatalf("newServer(): %v", err)
}
testServer := httptest.NewServer(s)
addr = testServer.Listener.Addr().String()
})
}
func TestHealthz(t *testing.T) {
startServer(t)
resp, err := http.Get("http://" + addr + "/healthz")
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status code to be %d, got %d", http.StatusOK, resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("expected no error reading response body, got %v", err)
}
expected := "ok"
if strings.TrimSpace(string(b)) != expected {
t.Errorf("expected response body to be %q, got %q", expected, b)
}
}