-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirectrules_test.go
More file actions
49 lines (41 loc) · 1.51 KB
/
redirectrules_test.go
File metadata and controls
49 lines (41 loc) · 1.51 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
package zeaburcaddyextension_test
import (
"fmt"
"testing"
zeaburcaddyextension "github.com/zeabur/caddy-extension"
)
func TestParseRedirects(t *testing.T) {
content := `
# Example of redirects
/ /home 302
/home /index 301
/about /about-us 302 Country=US
# This is a comment
/old-path /new-path 302 Country=UK Language=en
/invalid path 302
/http https://www.google.com 302
`
expectedResults := []zeaburcaddyextension.RedirectRule{
{SourcePath: "/", TargetPath: "/home", StatusCode: 302, Conditions: ""},
{SourcePath: "/home", TargetPath: "/index", StatusCode: 301, Conditions: ""},
{SourcePath: "/about", TargetPath: "/about-us", StatusCode: 302, Conditions: "Country=US"},
{SourcePath: "/old-path", TargetPath: "/new-path", StatusCode: 302, Conditions: "Country=UK Language=en"},
{SourcePath: "/http", TargetPath: "https://www.google.com", StatusCode: 302, Conditions: ""},
}
rules, err := zeaburcaddyextension.ParseRedirects(content)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Printf("Rules: %+v\n", rules)
if len(rules) != len(expectedResults) {
t.Errorf("Expected %d rules, got %d", len(expectedResults), len(rules))
}
for i, rule := range rules {
if rule.SourcePath != expectedResults[i].SourcePath ||
rule.TargetPath != expectedResults[i].TargetPath ||
rule.StatusCode != expectedResults[i].StatusCode ||
rule.Conditions != expectedResults[i].Conditions {
t.Errorf("Expected %+v, got %+v", expectedResults[i], rule)
}
}
}