57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRedirectHandler(t *testing.T) {
|
|
targetURL := "https://venueaxe.com"
|
|
|
|
tests := []struct {
|
|
name string
|
|
requestPath string
|
|
}{
|
|
{
|
|
name: "Root request",
|
|
requestPath: "/",
|
|
},
|
|
{
|
|
name: "Nested path request",
|
|
requestPath: "/venues/123",
|
|
},
|
|
{
|
|
name: "Query params request",
|
|
requestPath: "/search?q=concert&location=nyc",
|
|
},
|
|
{
|
|
name: "Arbitrary sub-path with query",
|
|
requestPath: "/a/b/c/d?token=xyz#hash",
|
|
},
|
|
}
|
|
|
|
handler := redirectHandler(targetURL)
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, tt.requestPath, nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler(w, req)
|
|
|
|
resp := w.Result()
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusMovedPermanently {
|
|
t.Fatalf("expected status %d, got %d", http.StatusMovedPermanently, resp.StatusCode)
|
|
}
|
|
|
|
location := resp.Header.Get("Location")
|
|
if location != targetURL {
|
|
t.Errorf("expected redirect location %q, got %q", targetURL, location)
|
|
}
|
|
})
|
|
}
|
|
}
|