-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
209 lines (165 loc) · 6.22 KB
/
Copy pathhandler_test.go
File metadata and controls
209 lines (165 loc) · 6.22 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
AI GENERATED
COPILOIT GENERATED FILE FOR TEST CASE GENERATION PURPOSES
TESTING COMPLETE FUNCTIONALITY OF THE CODE. DO NOT EDIT.
Package handler provides HTTP handlers for the URL shortener service.
This file contains unit tests for the handler package, covering the following scenarios:
- TestShortenURL_Success: Tests successful shortening of a valid URL, ensuring the response contains the expected short and long URLs.
- TestShortenURL_EmptyURL: Tests handling of an empty URL in the request, expecting a 400 Bad Request response.
- TestShortenURL_InvalidURL: Tests handling of an invalid URL format, expecting a 400 Bad Request response with an error message.
- TestShortenURL_InvalidJSON: Tests handling of invalid JSON in the request body, expecting a 400 Bad Request response.
- TestRedirectURL_Success: Tests successful redirection from a valid short code to the original long URL, expecting a 302 Found response with the correct Location header.
- TestRedirectURL_NotFound: Tests redirection with a non-existent short code, expecting a 404 Not Found response.
- TestShortenURL_Idempotency: Tests that shortening the same URL multiple times returns the same short URL, ensuring idempotency.
The tests use an in-memory storage implementation for isolation and do not require external dependencies.
*/
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"URL_Shortener_Ruckus_Networks/internals/handler"
"URL_Shortener_Ruckus_Networks/internals/service"
"URL_Shortener_Ruckus_Networks/internals/storage"
"github.com/gorilla/mux"
)
func setupHandler() *handler.Handler {
store := storage.NewMemoryStorage()
svc := service.NewURLService(store, "http://localhost:8080")
return handler.NewHandler(svc)
}
func TestShortenURL_Success(t *testing.T) {
h := setupHandler()
reqBody := handler.ShortenRequest{
URL: "https://www.example.com/test",
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
h.ShortenURL(w, req)
if w.Code != http.StatusOK {
t.Fatalf("Expected status 200, got %d", w.Code)
}
var resp handler.ShortenResponse
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
if resp.ShortURL == "" {
t.Fatal("Expected short URL in response")
}
if resp.LongURL != reqBody.URL {
t.Fatalf("Expected long URL %s, got %s", reqBody.URL, resp.LongURL)
}
}
func TestShortenURL_EmptyURL(t *testing.T) {
h := setupHandler()
reqBody := handler.ShortenRequest{
URL: "",
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
h.ShortenURL(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("Expected status 400, got %d", w.Code)
}
}
func TestShortenURL_InvalidURL(t *testing.T) {
h := setupHandler()
reqBody := handler.ShortenRequest{
URL: "not-a-valid-url",
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
h.ShortenURL(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("Expected status 400, got %d", w.Code)
}
var errResp handler.ErrorResponse
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
t.Fatalf("Failed to decode error response: %v", err)
}
if errResp.Error == "" {
t.Fatal("Expected error message in response")
}
}
func TestShortenURL_InvalidJSON(t *testing.T) {
h := setupHandler()
req := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
h.ShortenURL(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("Expected status 400, got %d", w.Code)
}
}
func TestRedirectURL_Success(t *testing.T) {
h := setupHandler()
longURL := "https://www.example.com/test"
reqBody := handler.ShortenRequest{URL: longURL}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
h.ShortenURL(w, req)
var shortenResp handler.ShortenResponse
json.NewDecoder(w.Body).Decode(&shortenResp)
// Extract short code from response
shortCode := shortenResp.ShortURL[len("http://localhost:8080/"):]
// Now test redirect
req = httptest.NewRequest("GET", "/"+shortCode, nil)
w = httptest.NewRecorder()
// Use mux to parse URL parameters
router := mux.NewRouter()
router.HandleFunc("/{shortCode}", h.RedirectURL)
router.ServeHTTP(w, req)
if w.Code != http.StatusFound {
t.Fatalf("Expected status 302, got %d", w.Code)
}
location := w.Header().Get("Location")
if location != longURL {
t.Fatalf("Expected redirect to %s, got %s", longURL, location)
}
}
func TestRedirectURL_NotFound(t *testing.T) {
h := setupHandler()
req := httptest.NewRequest("GET", "/nonexistent", nil)
w := httptest.NewRecorder()
// Use mux to parse URL parameters
router := mux.NewRouter()
router.HandleFunc("/{shortCode}", h.RedirectURL)
router.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("Expected status 404, got %d", w.Code)
}
}
func TestShortenURL_Idempotency(t *testing.T) {
h := setupHandler()
longURL := "https://www.example.com/idempotent-test"
reqBody := handler.ShortenRequest{URL: longURL}
body, _ := json.Marshal(reqBody)
// First request
req1 := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req1.Header.Set("Content-Type", "application/json")
w1 := httptest.NewRecorder()
h.ShortenURL(w1, req1)
var resp1 handler.ShortenResponse
json.NewDecoder(w1.Body).Decode(&resp1)
// Second request with same URL
body, _ = json.Marshal(reqBody)
req2 := httptest.NewRequest("POST", "/api/shorten", bytes.NewBuffer(body))
req2.Header.Set("Content-Type", "application/json")
w2 := httptest.NewRecorder()
h.ShortenURL(w2, req2)
var resp2 handler.ShortenResponse
json.NewDecoder(w2.Body).Decode(&resp2)
if resp1.ShortURL != resp2.ShortURL {
t.Fatalf("Expected same short URL, got %s and %s", resp1.ShortURL, resp2.ShortURL)
}
}