-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
99 lines (83 loc) · 3.03 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
package main
import (
"testing"
"net/http"
"net/http/httptest"
"strings"
)
type TestHandler struct {
t *testing.T
// The HTTP handler function to be tested.
f func (http.ResponseWriter, *http.Request)
}
// Build an HTTP request, pass it to the HTTP handler, and return the response.
func (handler TestHandler) request(method string, path string, headers map[string]string) TestResponse {
request, err := http.NewRequest(method, path, nil)
if err != nil {
handler.t.Fatal(err)
}
// Set request headers, if any.
for header, value := range headers {
request.Header.Set(header, value)
}
response := httptest.NewRecorder()
http.HandlerFunc(handler.f).ServeHTTP(response, request)
return TestResponse{handler.t, response}
}
// Make a GET request to the HTTP handler, and return the response.
func (handler TestHandler) GET(path string, headers map[string]string) TestResponse {
return handler.request(http.MethodGet, path, headers)
}
// Make a POST request to the HTTP handler, and return the response.
func (handler TestHandler) POST(path string, headers map[string]string) TestResponse {
return handler.request(http.MethodPost, path, headers)
}
type TestResponse struct {
t *testing.T
// The HTTP response being asserted against.
r *httptest.ResponseRecorder
}
// Ensure that the response contains the expected status code.
func (response TestResponse) AssertStatusEquals(expected int) {
if response.r.Code != expected {
response.t.Errorf(
"Handler returned unexpected status code: got `%v` want `%v`",
response.r.Code, expected)
}
}
// Ensure that the response body is exactly as expected.
func (response TestResponse) AssertBodyEquals(expected string) {
if actual := response.r.Body.String(); actual != expected {
response.t.Errorf(
"Handler returned unexpected body: got `%v` want `%v`",
actual, expected)
}
}
// Ensure that the response body contains a substring.
func (response TestResponse) AssertBodyContains(substr string) {
if actual := response.r.Body.String(); !strings.Contains(actual, substr) {
response.t.Errorf(
"Handler returned unexpected body: did not find `%v` in `%v`",
substr, actual)
}
}
// Ensure that the response contains a specific header.
func (response TestResponse) AssertHeaderExists(header string) {
if _, ok := response.r.Header()[header]; !ok {
response.t.Errorf(
"Handler did not set header `%v`",
header)
}
}
func TestGetIndex(t *testing.T) {
response := TestHandler{t, IndexHandler}.GET("/", nil)
response.AssertStatusEquals(http.StatusOK)
response.AssertBodyEquals("a plaintext pastebin service")
response.AssertBodyContains("plaintext pastebin service")
}
func TestPostIndex(t *testing.T) {
response := TestHandler{t, IndexHandler}.POST("/", nil)
response.AssertStatusEquals(http.StatusFound)
response.AssertHeaderExists("Location")
response.AssertBodyEquals("")
}