-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils_test.go
More file actions
136 lines (117 loc) · 3.15 KB
/
utils_test.go
File metadata and controls
136 lines (117 loc) · 3.15 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
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
package limen
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGenerateRandomString_Length(t *testing.T) {
t.Parallel()
tests := []struct {
name string
length int
}{
{"zero", 0},
{"one", 1},
{"short", 8},
{"long", 64},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateRandomString(tt.length)
assert.Len(t, result, tt.length)
})
}
}
func TestGenerateRandomString_Alphanumeric(t *testing.T) {
t.Parallel()
result := GenerateRandomString(1000, CharSetAlphanumeric)
for _, c := range result {
assert.True(t,
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'),
"unexpected character: %c", c)
}
}
func TestGenerateRandomString_Numeric(t *testing.T) {
t.Parallel()
result := GenerateRandomString(1000, CharSetNumeric)
for _, c := range result {
assert.True(t, c >= '0' && c <= '9', "expected digit, got: %c", c)
}
}
func TestGetFromMap(t *testing.T) {
t.Parallel()
tests := []struct {
name string
m map[string]any
key string
expected string
}{
{"found", map[string]any{"name": "Alice"}, "name", "Alice"},
{"missing key", map[string]any{"name": "Alice"}, "missing", ""},
{"type mismatch", map[string]any{"age": 42}, "age", ""},
{"nil map", nil, "key", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, GetFromMap[string](tt.m, tt.key))
})
}
}
func TestSortRulesBySpecificity(t *testing.T) {
t.Parallel()
rules := []*RateLimitRule{
NewRateLimitRule("/api/*", 10, time.Minute),
NewRateLimitRule("/api/users", 20, time.Minute),
NewRateLimitRule("/api/users/:id", 30, time.Minute),
NewRateLimitRule("**", 5, time.Minute),
}
sortRulesBySpecificity(rules)
assert.Equal(t, "/api/users", rules[0].path, "exact match should be first")
assert.Equal(t, "/api/users/:id", rules[1].path, "parameterized path should be next")
assert.Equal(t, "/api/*", rules[2].path, "single wildcard path")
assert.Equal(t, "**", rules[3].path, "double wildcard should be last")
}
func TestNormalizePluginPath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
basePath string
pluginPath string
override *PluginHTTPOverride
expected string
}{
{
name: "basic join",
basePath: "/api/auth",
pluginPath: "/credential-password",
expected: "/api/auth/credential-password",
},
{
name: "override replaces plugin path",
basePath: "/api/auth",
pluginPath: "/credential-password",
override: &PluginHTTPOverride{BasePath: "/custom"},
expected: "/api/auth/custom",
},
{
name: "empty override base path keeps plugin path",
basePath: "/api/auth",
pluginPath: "/oauth",
override: &PluginHTTPOverride{BasePath: ""},
expected: "/api/auth/oauth",
},
{
name: "nil override uses plugin path",
basePath: "/api",
pluginPath: "/session",
override: nil,
expected: "/api/session",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := normalizePluginPath(tt.basePath, tt.pluginPath, tt.override)
assert.Equal(t, tt.expected, result)
})
}
}