-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
route_test.go
154 lines (120 loc) · 3.81 KB
/
route_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
package mux
import (
"errors"
"net/http"
"reflect"
"regexp"
"sync"
"testing"
)
var testNewRouterMu sync.Mutex
var testHandler = http.NotFoundHandler()
func BenchmarkNewRouter(b *testing.B) {
testNewRouterMu.Lock()
defer testNewRouterMu.Unlock()
// Set the RegexpCompileFunc to the default regexp.Compile.
RegexpCompileFunc = regexp.Compile
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testNewRouter(b, testHandler)
}
}
func BenchmarkNewRouterRegexpFunc(b *testing.B) {
testNewRouterMu.Lock()
defer testNewRouterMu.Unlock()
// We preallocate the size to 8.
cache := make(map[string]*regexp.Regexp, 8)
// Override the RegexpCompileFunc to reuse compiled expressions
// from the `cache` map. Real world caches should have eviction
// policies or some sort of approach to limit memory use.
RegexpCompileFunc = func(expr string) (*regexp.Regexp, error) {
if regex, ok := cache[expr]; ok {
return regex, nil
}
regex, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
cache[expr] = regex
return regex, nil
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testNewRouter(b, testHandler)
}
}
func testNewRouter(_ testing.TB, handler http.Handler) {
r := NewRouter()
// A route with a route variable:
r.Handle("/metrics/{type}", handler)
r.Queries("orgID", "{orgID:[0-9]*?}")
r.Host("{subdomain}.domain.com")
}
func TestRouteMetadata(t *testing.T) {
router := NewRouter()
rw := NewRecorder()
expectedMap := make(map[any]any)
expectedMap["key"] = "value"
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
route := CurrentRoute(r)
metadata := route.GetMetadata()
if !reflect.DeepEqual(metadata, expectedMap) {
println(metadata)
t.Fatalf("Expected map does not equal the metadata map")
}
}).Metadata("key", "value")
router.HandleFunc("/single-value", func(w http.ResponseWriter, r *http.Request) {
route := CurrentRoute(r)
value, err := route.GetMetadataValue("key")
if err != nil {
t.Fatalf("Expected metadata value to be present, but gave error: %s", err)
}
stringValue, ok := value.(string)
if !ok {
t.Fatalf("Expected metadata value to be string, but was: %s", reflect.TypeOf(value))
}
if stringValue != "value" {
t.Fatalf("Expected metadata value to be '%s', but got '%s'", "value", stringValue)
}
_, err = route.GetMetadataValue("key2")
if err == nil {
t.Fatalf("Expected metadata key not to be present and error, but error was nil")
}
if !errors.Is(err, ErrMetadataKeyNotFound) {
t.Fatalf("Expected error to be ErrMetadataKeyNotFound but got: %s", err)
}
}).Metadata("key", "value")
router.HandleFunc("/single-value-fallback", func(w http.ResponseWriter, r *http.Request) {
route := CurrentRoute(r)
value := route.GetMetadataValueOr("key", "value-fallback")
stringValue, ok := value.(string)
if !ok {
t.Fatalf("Expected metadata value to be string, but was: %s", reflect.TypeOf(value))
}
if stringValue != "value" {
t.Fatalf("Expected metadata value to be '%s', but got '%s'", "value", stringValue)
}
fallbackValue := route.GetMetadataValueOr("key2", "value2")
fallbackStringValue, ok := fallbackValue.(string)
if !ok {
t.Fatalf("Expected metadata value to be string, but was: %s", reflect.TypeOf(value))
}
if fallbackStringValue != "value2" {
t.Fatalf("Expected metadata value to be '%s', but got '%s'", "value2", fallbackStringValue)
}
}).Metadata("key", "value")
t.Run("get metadata map", func(t *testing.T) {
req := newRequest("GET", "/")
router.ServeHTTP(rw, req)
})
t.Run("get metadata value", func(t *testing.T) {
req := newRequest("GET", "/single-value")
router.ServeHTTP(rw, req)
})
t.Run("get metadata value or fallback", func(t *testing.T) {
req := newRequest("GET", "/single-value-fallback")
router.ServeHTTP(rw, req)
})
}