-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
131 lines (106 loc) · 3.63 KB
/
function_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
package function
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/operationspark/shorty/handlers"
"github.com/operationspark/shorty/inmem"
"github.com/operationspark/shorty/shorty"
"github.com/operationspark/shorty/testutil"
)
func TestPOSTLink(t *testing.T) {
t.Run("returns the Shorty by code", func(t *testing.T) {
ogURL := "https://operationspark.org"
reqBody := bytes.NewReader([]byte(fmt.Sprintf(`{"originalUrl":%q}`, ogURL)))
request := NewRequestWithAPIKey(http.MethodPost, "/api/urls/", reqBody)
response := httptest.NewRecorder()
store := inmem.NewStore()
service := handlers.NewAPIService(handlers.ServiceConfig{
Store: store,
APIkey: "test-api-key",
})
handlers.NewServer(service).ServeHTTP(response, request)
var got shorty.Link
d := json.NewDecoder(response.Body)
d.Decode(&got)
testutil.AssertEqual(t, got.OriginalUrl, ogURL)
testutil.AssertEqual(t, len(got.Code), 10)
wantShortURL := fmt.Sprintf("https://ospk.org/%s", got.Code)
testutil.AssertEqual(t, got.ShortURL, wantShortURL)
})
}
func TestGETLink(t *testing.T) {
store := inmem.NewStore()
store.Store = map[string]shorty.Link{
"abc123": {Code: "abc123"},
}
service := handlers.NewAPIService(handlers.ServiceConfig{
Store: store,
APIkey: "test-api-key",
})
server := handlers.NewServer(service)
tests := []struct {
name string
endpoint string
wantBody string
statusCode int
}{
{
name: "GET abc123",
endpoint: "/abc123",
wantBody: `{"shortUrl":"","code":"abc123","customCode":"","originalUrl":"","totalClicks":0,"createdBy":"","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}` + "\n",
statusCode: http.StatusOK,
},
{
name: "GET not existent",
endpoint: "/not-a-valid-code",
wantBody: "Link not found: \"not-a-valid-code\"\n",
statusCode: http.StatusNotFound,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request := NewRequestWithAPIKey(http.MethodGet, fmt.Sprintf("/api/urls%s", test.endpoint), nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
testutil.AssertStatus(t, response.Code, test.statusCode)
testutil.AssertResponseBody(t, response.Body.String(), test.wantBody)
})
}
}
func TestGETLinks(t *testing.T) {
t.Run("returns all the links in the store", func(t *testing.T) {
store := inmem.NewStore()
store.Store = map[string]shorty.Link{
"abc123": {Code: "abc123"},
}
service := handlers.NewAPIService(handlers.ServiceConfig{
Store: store,
APIkey: "test-api-key",
})
server := handlers.NewServer(service)
wantBody := `[{"shortUrl":"","code":"abc123","customCode":"","originalUrl":"","totalClicks":0,"createdBy":"","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}]` + "\n"
request := NewRequestWithAPIKey(http.MethodGet, "/api/urls/", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
testutil.AssertStatus(t, response.Code, http.StatusOK)
testutil.AssertResponseBody(t, response.Body.String(), wantBody)
})
t.Run("returns empty list if there a no links in the store", func(t *testing.T) {
store := inmem.NewStore()
service := handlers.NewAPIService(handlers.ServiceConfig{
Store: store,
APIkey: "test-api-key",
})
server := handlers.NewServer(service)
wantBody := "[]\n"
request := NewRequestWithAPIKey(http.MethodGet, "/api/urls/", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
testutil.AssertStatus(t, response.Code, http.StatusOK)
testutil.AssertResponseBody(t, response.Body.String(), wantBody)
})
}