Skip to content

Commit c5ecf98

Browse files
committed
remove custom parsed types in favor of stringly typed api
1 parent f17d095 commit c5ecf98

File tree

3 files changed

+107
-115
lines changed

3 files changed

+107
-115
lines changed

rulesengine/engine_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestEngineMatches(t *testing.T) {
2020
{
2121
name: "method matches exact",
2222
rule: Rule{
23-
MethodPatterns: map[methodPattern]struct{}{methodPattern("GET"): {}},
23+
MethodPatterns: map[string]struct{}{"GET": {}},
2424
},
2525
method: "GET",
2626
url: "https://example.com/api",
@@ -29,7 +29,7 @@ func TestEngineMatches(t *testing.T) {
2929
{
3030
name: "method does not match",
3131
rule: Rule{
32-
MethodPatterns: map[methodPattern]struct{}{methodPattern("POST"): {}},
32+
MethodPatterns: map[string]struct{}{"POST": {}},
3333
},
3434
method: "GET",
3535
url: "https://example.com/api",
@@ -38,7 +38,7 @@ func TestEngineMatches(t *testing.T) {
3838
{
3939
name: "method wildcard matches any",
4040
rule: Rule{
41-
MethodPatterns: map[methodPattern]struct{}{methodPattern("*"): {}},
41+
MethodPatterns: map[string]struct{}{"*": {}},
4242
},
4343
method: "PUT",
4444
url: "https://example.com/api",
@@ -47,7 +47,7 @@ func TestEngineMatches(t *testing.T) {
4747
{
4848
name: "no method pattern allows all methods",
4949
rule: Rule{
50-
HostPattern: []labelPattern{labelPattern("example"), labelPattern("com")},
50+
HostPattern: []string{"example", "com"},
5151
},
5252
method: "DELETE",
5353
url: "https://example.com/api",
@@ -58,7 +58,7 @@ func TestEngineMatches(t *testing.T) {
5858
{
5959
name: "host matches exact",
6060
rule: Rule{
61-
HostPattern: []labelPattern{labelPattern("example"), labelPattern("com")},
61+
HostPattern: []string{"example", "com"},
6262
},
6363
method: "GET",
6464
url: "https://example.com/api",
@@ -67,7 +67,7 @@ func TestEngineMatches(t *testing.T) {
6767
{
6868
name: "host does not match",
6969
rule: Rule{
70-
HostPattern: []labelPattern{labelPattern("example"), labelPattern("org")},
70+
HostPattern: []string{"example", "org"},
7171
},
7272
method: "GET",
7373
url: "https://example.com/api",
@@ -76,7 +76,7 @@ func TestEngineMatches(t *testing.T) {
7676
{
7777
name: "subdomain matches",
7878
rule: Rule{
79-
HostPattern: []labelPattern{labelPattern("example"), labelPattern("com")},
79+
HostPattern: []string{"example", "com"},
8080
},
8181
method: "GET",
8282
url: "https://api.example.com/users",
@@ -85,7 +85,7 @@ func TestEngineMatches(t *testing.T) {
8585
{
8686
name: "host pattern too long",
8787
rule: Rule{
88-
HostPattern: []labelPattern{labelPattern("v1"), labelPattern("api"), labelPattern("example"), labelPattern("com")},
88+
HostPattern: []string{"v1", "api", "example", "com"},
8989
},
9090
method: "GET",
9191
url: "https://api.example.com/users",
@@ -94,7 +94,7 @@ func TestEngineMatches(t *testing.T) {
9494
{
9595
name: "host wildcard matches",
9696
rule: Rule{
97-
HostPattern: []labelPattern{labelPattern("*"), labelPattern("com")},
97+
HostPattern: []string{"*", "com"},
9898
},
9999
method: "GET",
100100
url: "https://test.com/api",
@@ -103,7 +103,7 @@ func TestEngineMatches(t *testing.T) {
103103
{
104104
name: "multiple host wildcards",
105105
rule: Rule{
106-
HostPattern: []labelPattern{labelPattern("*"), labelPattern("*")},
106+
HostPattern: []string{"*", "*"},
107107
},
108108
method: "GET",
109109
url: "https://api.example.com/users",
@@ -114,7 +114,7 @@ func TestEngineMatches(t *testing.T) {
114114
{
115115
name: "path matches exact",
116116
rule: Rule{
117-
PathPattern: []segmentPattern{segmentPattern("api"), segmentPattern("users")},
117+
PathPattern: []string{"api", "users"},
118118
},
119119
method: "GET",
120120
url: "https://example.com/api/users",
@@ -123,7 +123,7 @@ func TestEngineMatches(t *testing.T) {
123123
{
124124
name: "path does not match",
125125
rule: Rule{
126-
PathPattern: []segmentPattern{segmentPattern("api"), segmentPattern("posts")},
126+
PathPattern: []string{"api", "posts"},
127127
},
128128
method: "GET",
129129
url: "https://example.com/api/users",
@@ -132,7 +132,7 @@ func TestEngineMatches(t *testing.T) {
132132
{
133133
name: "subpath matches",
134134
rule: Rule{
135-
PathPattern: []segmentPattern{segmentPattern("api")},
135+
PathPattern: []string{"api"},
136136
},
137137
method: "GET",
138138
url: "https://example.com/api/users/123",
@@ -141,7 +141,7 @@ func TestEngineMatches(t *testing.T) {
141141
{
142142
name: "path pattern too long",
143143
rule: Rule{
144-
PathPattern: []segmentPattern{segmentPattern("api"), segmentPattern("v1"), segmentPattern("users"), segmentPattern("profile")},
144+
PathPattern: []string{"api", "v1", "users", "profile"},
145145
},
146146
method: "GET",
147147
url: "https://example.com/api/v1/users",
@@ -150,7 +150,7 @@ func TestEngineMatches(t *testing.T) {
150150
{
151151
name: "path wildcard matches",
152152
rule: Rule{
153-
PathPattern: []segmentPattern{segmentPattern("api"), segmentPattern("*"), segmentPattern("profile")},
153+
PathPattern: []string{"api", "*", "profile"},
154154
},
155155
method: "GET",
156156
url: "https://example.com/api/users/profile",
@@ -159,7 +159,7 @@ func TestEngineMatches(t *testing.T) {
159159
{
160160
name: "multiple path wildcards",
161161
rule: Rule{
162-
PathPattern: []segmentPattern{segmentPattern("*"), segmentPattern("*")},
162+
PathPattern: []string{"*", "*"},
163163
},
164164
method: "GET",
165165
url: "https://example.com/api/users/123",
@@ -170,9 +170,9 @@ func TestEngineMatches(t *testing.T) {
170170
{
171171
name: "all patterns match",
172172
rule: Rule{
173-
MethodPatterns: map[methodPattern]struct{}{methodPattern("POST"): {}},
174-
HostPattern: []labelPattern{labelPattern("api"), labelPattern("com")},
175-
PathPattern: []segmentPattern{segmentPattern("users")},
173+
MethodPatterns: map[string]struct{}{"POST": {}},
174+
HostPattern: []string{"api", "com"},
175+
PathPattern: []string{"users"},
176176
},
177177
method: "POST",
178178
url: "https://api.com/users",
@@ -181,9 +181,9 @@ func TestEngineMatches(t *testing.T) {
181181
{
182182
name: "method fails combined test",
183183
rule: Rule{
184-
MethodPatterns: map[methodPattern]struct{}{methodPattern("POST"): {}},
185-
HostPattern: []labelPattern{labelPattern("api"), labelPattern("com")},
186-
PathPattern: []segmentPattern{segmentPattern("users")},
184+
MethodPatterns: map[string]struct{}{"POST": {}},
185+
HostPattern: []string{"api", "com"},
186+
PathPattern: []string{"users"},
187187
},
188188
method: "GET",
189189
url: "https://api.com/users",
@@ -192,9 +192,9 @@ func TestEngineMatches(t *testing.T) {
192192
{
193193
name: "host fails combined test",
194194
rule: Rule{
195-
MethodPatterns: map[methodPattern]struct{}{methodPattern("POST"): {}},
196-
HostPattern: []labelPattern{labelPattern("api"), labelPattern("org")},
197-
PathPattern: []segmentPattern{segmentPattern("users")},
195+
MethodPatterns: map[string]struct{}{"POST": {}},
196+
HostPattern: []string{"api", "org"},
197+
PathPattern: []string{"users"},
198198
},
199199
method: "POST",
200200
url: "https://api.com/users",
@@ -203,9 +203,9 @@ func TestEngineMatches(t *testing.T) {
203203
{
204204
name: "path fails combined test",
205205
rule: Rule{
206-
MethodPatterns: map[methodPattern]struct{}{methodPattern("POST"): {}},
207-
HostPattern: []labelPattern{labelPattern("api"), labelPattern("com")},
208-
PathPattern: []segmentPattern{segmentPattern("posts")},
206+
MethodPatterns: map[string]struct{}{"POST": {}},
207+
HostPattern: []string{"api", "com"},
208+
PathPattern: []string{"posts"},
209209
},
210210
method: "POST",
211211
url: "https://api.com/users",
@@ -214,9 +214,9 @@ func TestEngineMatches(t *testing.T) {
214214
{
215215
name: "all wildcards match",
216216
rule: Rule{
217-
MethodPatterns: map[methodPattern]struct{}{methodPattern("*"): {}},
218-
HostPattern: []labelPattern{labelPattern("*"), labelPattern("*")},
219-
PathPattern: []segmentPattern{segmentPattern("*"), segmentPattern("*")},
217+
MethodPatterns: map[string]struct{}{"*": {}},
218+
HostPattern: []string{"*", "*"},
219+
PathPattern: []string{"*", "*"},
220220
},
221221
method: "PATCH",
222222
url: "https://test.example.com/api/users/123",
@@ -234,7 +234,7 @@ func TestEngineMatches(t *testing.T) {
234234
{
235235
name: "invalid URL",
236236
rule: Rule{
237-
HostPattern: []labelPattern{labelPattern("example"), labelPattern("com")},
237+
HostPattern: []string{"example", "com"},
238238
},
239239
method: "GET",
240240
url: "not-a-valid-url",
@@ -243,7 +243,7 @@ func TestEngineMatches(t *testing.T) {
243243
{
244244
name: "root path",
245245
rule: Rule{
246-
PathPattern: []segmentPattern{},
246+
PathPattern: []string{},
247247
},
248248
method: "GET",
249249
url: "https://example.com/",
@@ -252,7 +252,7 @@ func TestEngineMatches(t *testing.T) {
252252
{
253253
name: "localhost host",
254254
rule: Rule{
255-
HostPattern: []labelPattern{labelPattern("localhost")},
255+
HostPattern: []string{"localhost"},
256256
},
257257
method: "GET",
258258
url: "http://localhost:8080/api",

0 commit comments

Comments
 (0)