-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
192 lines (171 loc) · 4.27 KB
/
handlers_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"errors"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/go-redis/redis"
)
type db_mock struct{}
func (m *db_mock) Get(key string) (string, error) {
switch key {
case "abc-1":
return "yes", nil
case "abc-2":
return "", redis.Nil
default:
return "", errors.New("Internal Error")
}
}
func (m *db_mock) Search(key string) ([]string, error) {
switch key {
case "abc*":
return []string{"abc-1", "abc-2"}, nil
case "*-1":
return []string{"abc-1", "xyz-1"}, nil
case "xyz*":
return []string{}, nil
case "*-2":
return []string{}, nil
default:
return []string{}, errors.New("Internal Error")
}
}
func (m *db_mock) Set(key string, value interface{}) error {
switch key {
case "xyz-4":
return nil
default:
return errors.New("error in setting")
}
}
func (m *db_mock) TotalKeys() int {
return 4
}
func newTestApplication(t *testing.T) *application {
return &application{
config: config{},
logger: log.New(ioutil.Discard, "", 0),
db: &db_mock{},
}
}
type testServer struct {
*httptest.Server
}
func newTestServer(t *testing.T, h http.Handler) *testServer {
ts := httptest.NewServer(h)
return &testServer{ts}
}
func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, []byte) {
rs, err := ts.Client().Get(ts.URL + urlPath)
if err != nil {
t.Fatal(err)
}
defer rs.Body.Close()
body, err := ioutil.ReadAll(rs.Body)
if err != nil {
t.Fatal(err)
}
return rs.StatusCode, rs.Header, body
}
func (ts *testServer) post(t *testing.T, urlPath string, payload string) (int, http.Header, []byte) {
rs, err := ts.Client().Post(ts.URL+urlPath, "application/json", strings.NewReader(payload))
if err != nil {
t.Fatal(err)
}
defer rs.Body.Close()
body, err := ioutil.ReadAll(rs.Body)
if err != nil {
t.Fatal(err)
}
return rs.StatusCode, rs.Header, body
}
func TestHealth(t *testing.T) {
app := newTestApplication(t)
ts := newTestServer(t, app.routes())
defer ts.Close()
code, _, _ := ts.get(t, "/healthcheck")
if code != http.StatusOK {
t.Errorf("want %d; got %d", http.StatusOK, code)
}
}
func TestGet(t *testing.T) {
app := newTestApplication(t)
ts := newTestServer(t, app.routes())
defer ts.Close()
tests := []struct {
name string
urlPath string
wantCode int
wantBody []byte
}{
{"key exists", "/get/abc-1", http.StatusOK, []byte("yes")},
{"key doesn't exist", "/get/abc-2", http.StatusNotFound, nil},
{"empty key", "/get", http.StatusNotFound, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, _, body := ts.get(t, tt.urlPath)
if code != tt.wantCode {
t.Errorf("want %d; got %d", tt.wantCode, code)
}
if !bytes.Contains(body, tt.wantBody) {
t.Errorf("want body to contain %q", tt.wantBody)
}
})
}
}
func TestSet(t *testing.T) {
app := newTestApplication(t)
ts := newTestServer(t, app.routes())
defer ts.Close()
tests := []struct {
name string
urlPath string
payload string
wantCode int
}{
{"set key", "/set", `{"key":"xyz-4", "value":"f"}`, http.StatusOK},
{"set key without payload", "/set", ``, http.StatusBadRequest},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, _, _ := ts.post(t, tt.urlPath, tt.payload)
if code != tt.wantCode {
t.Errorf("want %d; got %d", tt.wantCode, code)
}
})
}
}
func TestSearch(t *testing.T) {
app := newTestApplication(t)
ts := newTestServer(t, app.routes())
defer ts.Close()
tests := []struct {
name string
urlPath string
wantCode int
wantBody []byte
}{
{"prefix keys exists", "/search?prefix=abc", http.StatusOK, []byte(`{"keys":["abc-1","abc-2"]}`)},
{"prefix keys doesn't exist", "/search?prefix=xyz", http.StatusOK, []byte(`{"keys":[]}`)},
{"suffix keys exists", "/search?suffix=-1", http.StatusOK, []byte(`{"keys":["abc-1","xyz-1"]}`)},
{"suffix keys doesn't exist", "/search?suffix=-2", http.StatusOK, []byte(`{"keys":[]}`)},
{"empty filters", "/search", http.StatusBadRequest, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, _, body := ts.get(t, tt.urlPath)
if code != tt.wantCode {
t.Errorf("want %d; got %d", tt.wantCode, code)
}
if !bytes.Contains(body, tt.wantBody) {
t.Errorf("want body to contain %q", tt.wantBody)
}
})
}
}