-
Notifications
You must be signed in to change notification settings - Fork 7
/
server_test.go
83 lines (64 loc) · 2 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
83
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// generic handler - should pass GET request to health handler
func TestHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler := healthHandler("title")
handler.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := `{"status":"ok"}`
if recorder.Body.String() != expected {
t.Errorf("handler returned unexpected body\nExpected:\n%v\nGot:\n%v\n",
expected, recorder.Body.String())
}
}
func TestHealthHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler := healthHandler("title")
handler.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := `{"status":"ok"}`
if recorder.Body.String() != expected {
t.Errorf("handler returned unexpected body\nExpected:\n%v\nGot:\n%v\n",
expected, recorder.Body.String())
}
}
func TestApiHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/api", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler := apiHandler("title")
handler.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expectedFragment01 := `health endpoint`
expectedFragment02 := `metrics endpoint`
body := recorder.Body.String()
if !strings.Contains(body, expectedFragment01) || !strings.Contains(body, expectedFragment02) {
t.Errorf("handler returned unexpected body\nExpected buffer to contain both '%s' and '%s'\nGot:\n%v\n",
expectedFragment01, expectedFragment02, recorder.Body.String())
}
}