-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
170 lines (158 loc) · 4.84 KB
/
handler_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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
type linkServiceStub struct {
get func(slug string) (string, error)
create func(url string) (Link, error)
}
func (stub linkServiceStub) Get(slug string) (string, error) {
return stub.get(slug)
}
func (stub linkServiceStub) Create(url string) (Link, error) {
return stub.create(url)
}
func TestRedirect(t *testing.T) {
url := "http://example.com"
service := linkServiceStub{
get: func(slug string) (string, error) {
return url, nil
},
}
handler := RedirectHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/xyz", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusPermanentRedirect {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusPermanentRedirect, res.StatusCode)
}
if loc := res.Header.Get("Location"); loc != url {
msg := "unexpected location: wanted %d, got %d instead"
t.Errorf(msg, url, loc)
}
}
func TestRedirectMalformedSlug(t *testing.T) {
service := linkServiceStub{
get: func(slug string) (string, error) {
return "", ErrDecodeFailure
},
}
handler := RedirectHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/x!z", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
expected := http.StatusBadRequest
if actual := res.StatusCode; actual != expected {
msg := "unexpected status code: wanted %d, got %d instead"
t.Errorf(msg, expected, actual)
}
}
func TestRedirectMissingLink(t *testing.T) {
service := linkServiceStub{
get: func(slug string) (string, error) {
return "", ErrNotFound
},
}
handler := RedirectHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/xyz", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
expected := http.StatusNotFound
if actual := res.StatusCode; actual != expected {
msg := "unexpected status code: wanted %d, got %d instead"
t.Errorf(msg, expected, actual)
}
}
func TestCreate(t *testing.T) {
url := "http://example.com"
slug := "xyz"
service := linkServiceStub{
create: func(url string) (Link, error) {
return Link{url, &slug}, nil
},
}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
payload, _ := json.Marshal(Link{url, nil})
buffer := bytes.NewBuffer(payload)
req, _ := http.NewRequest(http.MethodPost, "/api/links", buffer)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusCreated {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusCreated, res.StatusCode)
}
link := Link{}
body, _ := io.ReadAll(res.Body)
json.Unmarshal(body, &link)
if *link.Slug != slug {
t.Fail()
}
}
func TestCreateNoBody(t *testing.T) {
service := linkServiceStub{}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/api/links", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusBadRequest {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusBadRequest, res.StatusCode)
}
}
func TestCreateEmptyBody(t *testing.T) {
service := linkServiceStub{}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
buffer := bytes.NewBuffer([]byte{})
req, _ := http.NewRequest(http.MethodPost, "/api/links", buffer)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusBadRequest {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusBadRequest, res.StatusCode)
}
}
func TestCreateMalformedPayload(t *testing.T) {
service := linkServiceStub{}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
buffer := bytes.NewBufferString(`{"url": "http://example.com"`)
req, _ := http.NewRequest(http.MethodPost, "/api/links", buffer)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
expected := http.StatusBadRequest
if actual := res.StatusCode; actual != expected {
msg := "unexpected status code: wanted %d, got %d instead"
t.Errorf(msg, expected, actual)
}
}
func TestCreateServerError(t *testing.T) {
service := linkServiceStub{
create: func(url string) (Link, error) {
return Link{}, errors.New("db error")
},
}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
buffer := bytes.NewBufferString(`{"url": "http://example.com"}`)
req, _ := http.NewRequest(http.MethodPost, "/api/links", buffer)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
expected := http.StatusInternalServerError
if actual := res.StatusCode; actual != expected {
msg := "unexpected status code: wanted %d, got %d instead"
t.Errorf(msg, expected, actual)
}
}