-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects_plugin_test.go
188 lines (152 loc) · 4.67 KB
/
redirects_plugin_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package redirects_traefik_middleware
import (
"context"
"fmt"
"github.com/TRIMM/redirects-traefik-middleware/internal/app"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type TestRedirectStruct struct {
name string
requestURL string
expectedRedirect string
}
func getTestCases() *[]TestRedirectStruct {
return &[]TestRedirectStruct{
{
"Exact domain match redirect",
"https://old-domain.com",
"https://new-domain/post/laptop/clothing/",
},
{
"Exact relative path match redirect",
"http://example.com/product/furniture/electronics/",
"http://example.com/category/iphone/books/",
},
}
}
func startMockRedirectsServer(idx *app.IndexedRedirects) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestBody, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
request := string(requestBody)
redirectURL, ok := idx.Match(request)
if !ok {
redirectURL = "@empty"
}
_, err = fmt.Fprintf(w, "%s", redirectURL)
if err != nil {
log.Println("Failed to write response:", err)
}
})
return httptest.NewServer(mux)
}
func getMockRedirectsPlugin(serverURL string) http.Handler {
nextHandler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
config := &Config{
RedirectsAppURL: serverURL,
}
handler, err := New(context.Background(), nextHandler, config, "traefik-app-test")
if err != nil {
panic(err)
}
return handler
}
func TestServeHTTP_Match_Redirect(t *testing.T) {
idx := app.NewIndexedRedirects()
idx.IndexRule("", "old-domain.com", "https://new-domain/post/laptop/clothing/")
idx.IndexRule("/product/furniture/electronics/", "", "/category/iphone/books/")
mockServer := startMockRedirectsServer(idx)
defer mockServer.Close()
rp := getMockRedirectsPlugin(mockServer.URL)
testCases := getTestCases()
for _, tc := range *testCases {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest("GET", tc.requestURL, strings.NewReader(tc.requestURL))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
rr := httptest.NewRecorder()
rp.ServeHTTP(rr, req)
if rr.Code != http.StatusFound {
t.Errorf("handler returned wrong status code: got %v want %v",
rr.Code, http.StatusFound)
}
location := rr.Header().Get("Location")
if location != tc.expectedRedirect {
t.Errorf("handler returned unexpected redirect URL: got %v want %v", location, tc.expectedRedirect)
}
})
}
}
func TestServeHTTP_NoMatch_Redirect(t *testing.T) {
idx := app.NewIndexedRedirects()
mockServer := startMockRedirectsServer(idx)
defer mockServer.Close()
rp := getMockRedirectsPlugin(mockServer.URL)
req, err := http.NewRequest("GET", "http://example.com/nonexistent", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
rp.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func BenchmarkMiddleware_Match_Redirect(b *testing.B) {
idx := app.NewIndexedRedirects()
idx.IndexRule("/product/furniture/electronics/", "", "/category/iphone/books/")
mockServer := startMockRedirectsServer(idx)
defer mockServer.Close()
rp := getMockRedirectsPlugin(mockServer.URL)
req, err := http.NewRequest("GET", "http://example.com/product/furniture/electronics/", nil)
if err != nil {
b.Fatalf("Failed to create request: %v", err)
}
b.ResetTimer() // Reset the timer to ignore the setup time
for i := 0; i < b.N; i++ {
rr := httptest.NewRecorder()
rp.ServeHTTP(rr, req)
if rr.Code != http.StatusFound {
b.Errorf("handler returned wrong status code: got %v want %v",
rr.Code, http.StatusFound)
}
expectedRedirectURL := "http://example.com/category/iphone/books/"
location := rr.Header().Get("Location")
if location != expectedRedirectURL {
b.Errorf("handler returned unexpected redirect URL: got %v want %v",
location, expectedRedirectURL)
}
}
}
func BenchmarkMiddleware_NoMatch_Redirect(b *testing.B) {
idx := app.NewIndexedRedirects()
mockServer := startMockRedirectsServer(idx)
defer mockServer.Close()
rp := getMockRedirectsPlugin(mockServer.URL)
req, err := http.NewRequest("GET", "http://example.com/nonexistent", nil)
if err != nil {
b.Fatalf("Failed to create request: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rr := httptest.NewRecorder()
rp.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
b.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
}