-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
143 lines (118 loc) · 3.02 KB
/
Copy pathserver_test.go
File metadata and controls
143 lines (118 loc) · 3.02 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var s *server
func TestMain(m *testing.M) {
s = newServer()
code := m.Run()
os.Exit(code)
}
func TestHealthy(t *testing.T) {
request, err := http.NewRequest("GET", "/healthy", nil)
if err != nil {
t.Fatal(err)
}
response := executeRequest(request)
checkResponseCode(t, http.StatusOK, response.Code)
}
func TestPOST(t *testing.T) {
var testPost = []struct {
name string
path string
description string
content string
code int
}{
{
"send post request",
"/",
"my description",
"my content",
http.StatusOK,
},
}
for _, testData := range testPost {
id := postRequest(t, testData.path, `{"description":"`+testData.description+`","content":"`+testData.content+`"}`, testData.name)
text, ok := s.entries[id]
if !ok {
t.Errorf("%v not found", id)
}
if text.Description != testData.description {
t.Errorf("\nDescription expected: %v\n Got: %v\n", testData.description, text.Description)
}
if text.Content != testData.content {
t.Errorf("\nContent expected: %v\n Got: %v\n", testData.content, text.Content)
}
}
}
func TestGET(t *testing.T) {
id := "any-id"
s.entries = make(map[string]textData)
s.entries[id] = textData{
Description: "any-description",
Content: "any-content",
}
var testGet = []struct {
name string
path string
code int
body string
}{
{
"read existing entry",
"/paste/" + id,
http.StatusOK,
`{"id":"` + id + `","description":"any-description","content":"any-content"}`,
},
{
"read non-existing entry",
"/paste/bad-id",
http.StatusNotFound,
``,
},
}
for _, testData := range testGet {
request, err := http.NewRequest("GET", testData.path, nil)
if err != nil {
t.Fatal(err)
}
response := executeRequest(request)
checkResponseCode(t, testData.code, response.Code, testData.name)
checkResponseBody(t, testData.body, response.Body.String(), testData.name)
}
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
s.router.ServeHTTP(rec, req)
return rec
}
func checkResponseCode(t *testing.T, expected, actual int, testName ...string) {
if expected != actual {
t.Errorf("Error in test%s: expected response code %d. Got %d\n", testName, expected, actual)
}
}
func checkResponseBody(t *testing.T, expected, actual string, testName ...string) {
if expected != actual {
t.Errorf("Error in test%s: expected response body %v. Got %v\n", testName, expected, actual)
}
}
func postRequest(t *testing.T, path, jsonStr string, testName ...string) string {
request, err := http.NewRequest("POST", path, bytes.NewBuffer([]byte(jsonStr)))
if err != nil {
t.Fatal(err)
}
request.Header.Set("Content-Type", "application/json")
response := executeRequest(request)
checkResponseCode(t, http.StatusCreated, response.Code)
var entryID textID
if err := json.Unmarshal(response.Body.Bytes(), &entryID); err != nil {
t.Fatal(err)
}
return entryID.ID
}