-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
149 lines (131 loc) · 3.95 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
package urlshortener
import (
"bytes"
"encoding/json"
"github.com/oliverpauffley/urlshortener/database"
"github.com/oliverpauffley/urlshortener/hashing"
"net/http"
"net/http/httptest"
"testing"
)
func TestUrlHandler(t *testing.T) {
// set up mockdb and env for testing
db := database.Mockdb{Urls: map[int]database.UrlModel{}}
db.Urls[1] = database.UrlModel{LongUrl: "http://bbc.com", ID: 1, Hash: hashing.NewHashId(1)}
env := Env{db, http.NewServeMux()}
var tt = []struct {
name string // name of test
inputUrl string // long url to shorten
wantCode int // http status code expected
wantUrl string // shortened url
}{
{
name: "returns bad request when sent an invalid url",
inputUrl: "http://",
wantCode: http.StatusBadRequest,
wantUrl: "",
},
{
name: "returns correct short url from valid long url",
inputUrl: "http://google.com",
wantCode: http.StatusOK,
wantUrl: "/" + hashing.NewHashId(0),
},
{
name: "returns short url when given a url already in the db",
inputUrl: "http://bbc.com",
wantCode: http.StatusOK,
wantUrl: "/" + hashing.NewHashId(1),
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
// format url as json
requestPayload := struct {
LongUrl string `json:"long_url"`
}{
LongUrl: test.inputUrl,
}
b, err := json.Marshal(requestPayload)
if err != nil {
t.Fatalf("error encoding json, %v", err)
}
request, err := http.NewRequest("POST", "/url", bytes.NewBuffer(b))
if err != nil {
t.Fatalf("error creating new request, %v", err)
}
response := httptest.NewRecorder()
handler := http.HandlerFunc(env.urlHandler())
handler.ServeHTTP(response, request)
gotCode := response.Code
if gotCode != test.wantCode {
t.Errorf("Got incorrect response code, wanted %v, got %v", test.wantCode, gotCode)
}
// decode json response if the test expects a url to be returned
if test.wantUrl != "" {
type responsePayload struct {
ShortUrl string `json:"short_url"`
}
gotJson := &responsePayload{}
err := json.NewDecoder(response.Body).Decode(gotJson)
if err != nil {
t.Errorf("error decoding json response, %v", err)
}
if gotJson.ShortUrl != test.wantUrl {
t.Errorf("server returned the incorrect shortened url, wanted %v, got %v",
test.wantUrl, gotJson.ShortUrl)
}
}
})
}
}
func TestRedirectHandler(t *testing.T) {
// set up mockdb and env for testing
db := database.Mockdb{Urls: map[int]database.UrlModel{}}
db.Urls[1] = database.UrlModel{LongUrl: "http://bbc.com", ID: 1, Hash: hashing.NewHashId(1)}
env := Env{db, http.NewServeMux()}
var tt = []struct {
name string // name of test
inputUrl string // short url to redirect
wantCode int // http status code expected
wantUrl string // redirect url
}{
{
name: "returns a redirect to a url when given a short url from the db",
inputUrl: db.Urls[1].Hash,
wantCode: http.StatusSeeOther,
wantUrl: "http://bbc.com",
},
{
name: "returns an error when trying to use a short url not in the db",
inputUrl: "this isn't in the db",
wantCode: http.StatusNotFound,
wantUrl: "",
},
{
name: "returns an error when no url hash is entered",
inputUrl: "",
wantCode: http.StatusNotFound,
wantUrl: "",
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
request, _ := http.NewRequest("GET", "/"+test.inputUrl, nil)
response := httptest.NewRecorder()
handler := http.HandlerFunc(env.RedirectHandler())
handler.ServeHTTP(response, request)
gotCode := response.Code
if gotCode != test.wantCode {
t.Errorf("Got incorrect response code, wanted %v, got %v", test.wantCode, gotCode)
}
if test.wantUrl != "" {
redirectUrl := response.Header().Get("Location")
if redirectUrl != test.wantUrl {
t.Errorf("server returned the incorrect shortened url, wanted %v, got %v",
test.wantUrl, redirectUrl)
}
}
})
}
}