-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdir-lookup-http-handler_test.go
96 lines (90 loc) · 2.48 KB
/
dir-lookup-http-handler_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
package bifrost_http
import (
"net/http"
"net/url"
"testing"
)
func TestMatchServeMuxPattern(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("GET /test", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("GET /test/nested", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("POST /test", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("GET /posts/{id}", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("GET /posts/latest", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("/files/{pathname...}", func(w http.ResponseWriter, r *http.Request) {})
tests := []struct {
name string
method string
url string
expectedPath string
expectedExists bool
}{
{
name: "Exact match GET",
method: "GET",
url: "/test",
expectedPath: "GET /test",
expectedExists: true,
},
{
name: "Nested match GET",
method: "GET",
url: "/test/nested",
expectedPath: "GET /test/nested",
expectedExists: true,
},
{
name: "POST method",
method: "POST",
url: "/test",
expectedPath: "POST /test",
expectedExists: true,
},
{
name: "Wildcard match",
method: "GET",
url: "/posts/123",
expectedPath: "GET /posts/{id}",
expectedExists: true,
},
{
name: "Specific path over wildcard",
method: "GET",
url: "/posts/latest",
expectedPath: "GET /posts/latest",
expectedExists: true,
},
{
name: "Wildcard with multiple segments",
method: "GET",
url: "/files/path/to/file.txt",
expectedPath: "/files/{pathname...}",
expectedExists: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parsedURL, err := url.Parse(tt.url)
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
dir := NewLookupHTTPHandler(tt.method, parsedURL, "")
handler, pattern := MatchServeMuxPattern(mux, dir)
if tt.expectedExists {
if handler == nil {
t.Fatal("Expected handler to not be nil")
}
if pattern != tt.expectedPath {
t.Fatalf("Expected pattern %s, got %s", tt.expectedPath, pattern)
}
} else {
if handler != nil {
t.Fatalf("Expected handler to be nil, got %v", handler)
}
if pattern != "" {
t.Fatalf("Expected empty pattern, got %s", pattern)
}
}
})
}
}