-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_test.go
119 lines (100 loc) · 2.98 KB
/
controller_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
package main
// integration tests that require a database
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestGetIndex(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
IndexHandler(w, req)
response := w.Result()
assertResponseCode(t, http.StatusOK, response.StatusCode)
body, _ := ioutil.ReadAll(response.Body)
assertString(t, `<html><body>index web page</body></html>`, string(body))
}
func TestIntegrationGetIndex(t *testing.T) {
if !isIntegrationTest() {
t.Skip()
}
// GIVEN
cleanStart(t)
// WHEN
req, _ := http.NewRequest("GET", "/", nil)
response := executeRequest(t, req)
// THEN
assertResponseCode(t, http.StatusOK, response.Code)
body, _ := ioutil.ReadAll(response.Body)
assertString(t, "<html><body>index web page</body></html>", string(body))
// CLEANUP
err := resetTable(t, testDB, NoteTable, NoteTableSequence)
exitIfError(err)
}
func TestIntegrationPostNote(t *testing.T) {
if !isIntegrationTest() {
t.Skip()
}
// GIVEN
cleanStart(t)
testNoteString := "this is a test note"
testNoteJSON := `{"note": "` + testNoteString + `"}`
payload := []byte(testNoteJSON)
req, _ := http.NewRequest("POST", "/note", bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json")
// WHEN
response := executeRequest(t, req)
// THEN
assertResponseCode(t, http.StatusOK, response.Code)
// WHEN
reqGet, _ := http.NewRequest("GET", "/note", nil)
responseGet := executeRequest(t, reqGet)
// THEN
assertResponseCode(t, http.StatusOK, responseGet.Code)
assertString(t, "application/json; charset=utf-8", responseGet.Result().Header.Get("Content-Type"))
body, _ := ioutil.ReadAll(responseGet.Body)
now := time.Now().UTC()
// If the tests runs exactly at the boundary of an hour this might fail
timestamp := fmt.Sprintf("%d-%02d-%02dT%02d:", now.Year(), now.Month(), now.Day(), now.Hour())
expectedJSON := `[
{
"id": 1,
"note": "` + testNoteString + `",
"created": "` + timestamp
assertResponseBodyStartsWith(t, expectedJSON, string(body))
// CLEANUP
err := resetTable(t, testDB, NoteTable, NoteTableSequence)
exitIfError(err)
}
func executeRequest(t *testing.T, req *http.Request) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
// TODO: is there some way to avoid this global test variable
r := router(testDB)
r.ServeHTTP(w, req)
return w
}
func assertResponseCode(t *testing.T, expected, result int) {
t.Helper()
if expected != result {
t.Errorf("Expected response code: %d ,Received: %d\n", expected, result)
}
}
func assertResponseBodyStartsWith(t *testing.T, expected, result string) {
t.Helper()
if strings.HasPrefix(result, expected) {
return
}
t.Errorf("Expected response body starts with: \n%s\nInstead received: \n%s\n", expected, result)
}
func assertString(t *testing.T, expected, result string) {
t.Helper()
if expected != result {
t.Errorf("Expected: \n%s\nInstead received: \n%s\n", expected, result)
}
}