-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern_test.go
591 lines (554 loc) · 15.9 KB
/
pattern_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package muxpatterns
import (
"fmt"
"net/http"
"regexp"
"strings"
"testing"
"golang.org/x/exp/slices"
)
func TestParse(t *testing.T) {
lit := func(name string) segment {
return segment{s: name}
}
wild := func(name string) segment {
return segment{s: name, wild: true}
}
multi := func(name string) segment {
s := wild(name)
s.multi = true
return s
}
for _, test := range []struct {
in string
want Pattern
}{
{"/", Pattern{segments: []segment{multi("")}}},
{"/a", Pattern{segments: []segment{lit("a")}}},
{
"/a/",
Pattern{segments: []segment{lit("a"), multi("")}},
},
{"/path/to/something", Pattern{segments: []segment{
lit("path"), lit("to"), lit("something"),
}}},
{
"/{w1}/lit/{w2}",
Pattern{
segments: []segment{wild("w1"), lit("lit"), wild("w2")},
},
},
{
"/{w1}/lit/{w2}/",
Pattern{
segments: []segment{wild("w1"), lit("lit"), wild("w2"), multi("")},
},
},
{
"example.com/",
Pattern{host: "example.com", segments: []segment{multi("")}},
},
{
"GET /",
Pattern{method: "GET", segments: []segment{multi("")}},
},
{
"POST example.com/foo/{w}",
Pattern{
method: "POST",
host: "example.com",
segments: []segment{lit("foo"), wild("w")},
},
},
{
"/{$}",
Pattern{segments: []segment{lit("/")}},
},
{
"DELETE example.com/a/{foo12}/{$}",
Pattern{method: "DELETE", host: "example.com", segments: []segment{lit("a"), wild("foo12"), lit("/")}},
},
{
"/foo/{$}",
Pattern{segments: []segment{lit("foo"), lit("/")}},
},
{
"/{a}/foo/{rest...}",
Pattern{segments: []segment{wild("a"), lit("foo"), multi("rest")}},
},
{
"//",
Pattern{segments: []segment{lit(""), multi("")}},
},
{
"/foo///./../bar",
Pattern{segments: []segment{lit("foo"), lit(""), lit(""), lit("."), lit(".."), lit("bar")}},
},
{
"a.com/foo//",
Pattern{host: "a.com", segments: []segment{lit("foo"), lit(""), multi("")}},
},
} {
got := mustParse(t, test.in)
if !got.equal(&test.want) {
t.Errorf("%q:\ngot %#v\nwant %#v", test.in, got, &test.want)
}
}
}
func TestParseError(t *testing.T) {
for _, test := range []struct {
in string
contains string
}{
{"", "empty pattern"},
{"A=B /", "bad method"},
{" ", "missing /"},
{"/{w}x", "bad wildcard segment"},
{"/x{w}", "bad wildcard segment"},
{"/{wx", "bad wildcard segment"},
{"/{a$}", "bad wildcard name"},
{"/{}", "empty wildcard"},
{"/{...}", "empty wildcard"},
{"/{$...}", "bad wildcard"},
{"/{$}/", "{$} not at end"},
{"/{$}/x", "{$} not at end"},
{"/{a...}/", "not at end"},
{"/{a...}/x", "not at end"},
{"{a}/b", "missing initial '/'"},
{"/a/{x}/b/{x...}", "duplicate wildcard name"},
{"GET //", "unclean path"},
} {
_, err := Parse(test.in)
if err == nil || !strings.Contains(err.Error(), test.contains) {
t.Errorf("%q:\ngot %v, want error containing %q", test.in, err, test.contains)
}
}
}
func (p1 *Pattern) equal(p2 *Pattern) bool {
return p1.method == p2.method && p1.host == p2.host && slices.Equal(p1.segments, p2.segments)
}
func TestIsValidHTTPToken(t *testing.T) {
for _, test := range []struct {
in string
want bool
}{
{"", false},
{"GET", true},
{"get", true},
{"white space", false},
{"#!~", true},
{"a-b1_2", true},
{"notok)", false},
} {
got := isValidHTTPToken(test.in)
if g, w := got, test.want; g != w {
t.Errorf("%q: got %t, want %t", test.in, g, w)
}
}
}
func TestCompareMethods(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want relationship
}{
{"/", "/", equivalent},
{"GET /", "GET /", equivalent},
{"HEAD /", "HEAD /", equivalent},
{"POST /", "POST /", equivalent},
{"GET /", "POST /", disjoint},
{"GET /", "/", moreSpecific},
{"HEAD /", "/", moreSpecific},
{"GET /", "HEAD /", moreGeneral},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
got := pat1.compareMethods(pat2)
if got != test.want {
t.Errorf("%s vs %s: got %s, want %s", test.p1, test.p2, got, test.want)
}
got2 := pat2.compareMethods(pat1)
want2 := inverseRelationship(test.want)
if got2 != want2 {
t.Errorf("%s vs %s: got %s, want %s", test.p2, test.p1, got2, want2)
}
}
}
func TestComparePaths(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want relationship
}{
// TODO: verify we hit all these case below in our systematic list.
{"/a/b/{x...}", "/a/b/c/d/{y...}", moreGeneral},
{"/a/{x...}", "/a/b/{x...}", moreGeneral},
{"/a/{$}", "/a/b/{x...}", disjoint},
{"/a/b/{$}", "/a/b/{x...}", moreSpecific},
{"/a/{x}/b/{y...}", "/{x}/c/{y...}", overlaps},
{"/a/{x}/b/", "/{x}/c/{y...}", overlaps},
{"/a/{x}/b/{$}", "/{x}/c/{y...}", overlaps},
{"/a/{x...}", "/b/{y...}", disjoint},
{"/a/{x...}", "/a/{y...}", equivalent},
{"/a/{z}/{x...}", "/a/b/{y...}", moreGeneral},
{"/a/{z}/{x...}", "/{z}/b/{y...}", overlaps},
{"/a/{x...}", "/a/{x}/{y...}", moreGeneral},
// A non-final pattern segment can have one of two values: literal or
// single wildcard. A final pattern segment can have one of 5: empty
// (trailing slash), literal, dollar, single wildcard, or multi
// wildcard. Trailing slash and multi wildcard are the same.
// A literal should be more specific than anything it overlaps, except itself.
{"/a", "/a", equivalent},
{"/a", "/b", disjoint},
{"/a", "/", moreSpecific},
{"/a", "/{$}", disjoint},
{"/a", "/{x}", moreSpecific},
{"/a", "/{x...}", moreSpecific},
// Adding a segment doesn't change that.
{"/b/a", "/b/a", equivalent},
{"/b/a", "/b/b", disjoint},
{"/b/a", "/b/", moreSpecific},
{"/b/a", "/b/{$}", disjoint},
{"/b/a", "/b/{x}", moreSpecific},
{"/b/a", "/b/{x...}", moreSpecific},
{"/{z}/a", "/{z}/a", equivalent},
{"/{z}/a", "/{z}/b", disjoint},
{"/{z}/a", "/{z}/", moreSpecific},
{"/{z}/a", "/{z}/{$}", disjoint},
{"/{z}/a", "/{z}/{x}", moreSpecific},
{"/{z}/a", "/{z}/{x...}", moreSpecific},
// Single wildcard on left.
{"/{z}", "/a", moreGeneral},
{"/{z}", "/a/b", disjoint},
{"/{z}", "/{$}", disjoint},
{"/{z}", "/{x}", equivalent},
{"/{z}", "/", moreSpecific},
{"/{z}", "/{x...}", moreSpecific},
{"/b/{z}", "/b/a", moreGeneral},
{"/b/{z}", "/b/a/b", disjoint},
{"/b/{z}", "/b/{$}", disjoint},
{"/b/{z}", "/b/{x}", equivalent},
{"/b/{z}", "/b/", moreSpecific},
{"/b/{z}", "/b/{x...}", moreSpecific},
// Trailing slash on left.
{"/", "/a", moreGeneral},
{"/", "/a/b", moreGeneral},
{"/", "/{$}", moreGeneral},
{"/", "/{x}", moreGeneral},
{"/", "/", equivalent},
{"/", "/{x...}", equivalent},
{"/b/", "/b/a", moreGeneral},
{"/b/", "/b/a/b", moreGeneral},
{"/b/", "/b/{$}", moreGeneral},
{"/b/", "/b/{x}", moreGeneral},
{"/b/", "/b/", equivalent},
{"/b/", "/b/{x...}", equivalent},
{"/{z}/", "/{z}/a", moreGeneral},
{"/{z}/", "/{z}/a/b", moreGeneral},
{"/{z}/", "/{z}/{$}", moreGeneral},
{"/{z}/", "/{z}/{x}", moreGeneral},
{"/{z}/", "/{z}/", equivalent},
{"/{z}/", "/a/", moreGeneral},
{"/{z}/", "/{z}/{x...}", equivalent},
{"/{z}/", "/a/{x...}", moreGeneral},
{"/a/{z}/", "/{z}/a/", overlaps},
// Multi wildcard on left.
{"/{m...}", "/a", moreGeneral},
{"/{m...}", "/a/b", moreGeneral},
{"/{m...}", "/{$}", moreGeneral},
{"/{m...}", "/{x}", moreGeneral},
{"/{m...}", "/", equivalent},
{"/{m...}", "/{x...}", equivalent},
{"/b/{m...}", "/b/a", moreGeneral},
{"/b/{m...}", "/b/a/b", moreGeneral},
{"/b/{m...}", "/b/{$}", moreGeneral},
{"/b/{m...}", "/b/{x}", moreGeneral},
{"/b/{m...}", "/b/", equivalent},
{"/b/{m...}", "/b/{x...}", equivalent},
{"/{z}/{m...}", "/{z}/a", moreGeneral},
{"/{z}/{m...}", "/{z}/a/b", moreGeneral},
{"/{z}/{m...}", "/{z}/{$}", moreGeneral},
{"/{z}/{m...}", "/{z}/{x}", moreGeneral},
{"/{z}/{m...}", "/{w}/", equivalent},
{"/{z}/{m...}", "/a/", moreGeneral},
{"/{z}/{m...}", "/{z}/{x...}", equivalent},
{"/{z}/{m...}", "/a/{x...}", moreGeneral},
{"/a/{z}/{m...}", "/{z}/a/", overlaps},
// Dollar on left.
{"/{$}", "/a", disjoint},
{"/{$}", "/a/b", disjoint},
{"/{$}", "/{$}", equivalent},
{"/{$}", "/{x}", disjoint},
{"/{$}", "/", moreSpecific},
{"/{$}", "/{x...}", moreSpecific},
{"/b/{$}", "/b", disjoint},
{"/b/{$}", "/b/a", disjoint},
{"/b/{$}", "/b/a/b", disjoint},
{"/b/{$}", "/b/{$}", equivalent},
{"/b/{$}", "/b/{x}", disjoint},
{"/b/{$}", "/b/", moreSpecific},
{"/b/{$}", "/b/{x...}", moreSpecific},
{"/{z}/{$}", "/{z}/a", disjoint},
{"/{z}/{$}", "/{z}/a/b", disjoint},
{"/{z}/{$}", "/{z}/{$}", equivalent},
{"/{z}/{$}", "/{z}/{x}", disjoint},
{"/{z}/{$}", "/{z}/", moreSpecific},
{"/{z}/{$}", "/a/", overlaps},
{"/{z}/{$}", "/a/{x...}", overlaps},
{"/{z}/{$}", "/{z}/{x...}", moreSpecific},
{"/a/{z}/{$}", "/{z}/a/", overlaps},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
if g := pat1.comparePaths(pat1); g != equivalent {
t.Errorf("%s does not match itself; got %s", pat1, g)
}
if g := pat2.comparePaths(pat2); g != equivalent {
t.Errorf("%s does not match itself; got %s", pat2, g)
}
got := pat1.comparePaths(pat2)
if got != test.want {
t.Errorf("%s vs %s: got %s, want %s", test.p1, test.p2, got, test.want)
}
want2 := inverseRelationship(test.want)
got2 := pat2.comparePaths(pat1)
if got2 != want2 {
t.Errorf("%s vs %s: got %s, want %s", test.p2, test.p1, got2, want2)
}
}
}
func inverseRelationship(r relationship) relationship {
switch r {
case moreSpecific:
return moreGeneral
case moreGeneral:
return moreSpecific
default:
return r
}
}
func TestOverlapPath(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want string
}{
{"/a/{x}", "/{x}/a", "/a/a"},
{"/a/{z}/", "/{z}/a/", "/a/a/"},
{"/a/{z}/{m...}", "/{z}/a/", "/a/a/"},
{"/{z}/{$}", "/a/", "/a/"},
{"/{z}/{$}", "/a/{x...}", "/a/"},
{"/a/{z}/{$}", "/{z}/a/", "/a/a/"},
{"/a/{x}/b/{y...}", "/{x}/c/{y...}", "/a/c/b/"},
{"/a/{x}/b/", "/{x}/c/{y...}", "/a/c/b/"},
{"/a/{x}/b/{$}", "/{x}/c/{y...}", "/a/c/b/"},
{"/a/{z}/{x...}", "/{z}/b/{y...}", "/a/b/"},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
if pat1.comparePaths(pat2) != overlaps {
t.Fatalf("%s does not overlap %s", test.p1, test.p2)
}
got := commonPath(pat1, pat2)
if got != test.want {
t.Errorf("%s vs. %s: got %q, want %q", test.p1, test.p2, got, test.want)
}
}
}
func TestDifferencePath(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want string
}{
{"/a/{x}", "/{x}/a", "/a/x"},
{"/{x}/a", "/a/{x}", "/x/a"},
{"/a/{z}/", "/{z}/a/", "/a/z/"},
{"/{z}/a/", "/a/{z}/", "/z/a/"},
{"/{a}/a/", "/a/{z}/", "/ax/a/"},
{"/a/{z}/{x...}", "/{z}/b/{y...}", "/a/z/"},
{"/{z}/b/{y...}", "/a/{z}/{x...}", "/z/b/"},
{"/a/b/", "/a/b/c", "/a/b/"},
{"/a/b/{x...}", "/a/b/c", "/a/b/"},
{"/a/b/{x...}", "/a/b/c/d", "/a/b/"},
{"/a/b/{x...}", "/a/b/c/d/", "/a/b/"},
{"/a/{z}/{m...}", "/{z}/a/", "/a/z/"},
{"/{z}/a/", "/a/{z}/{m...}", "/z/a/"},
{"/{z}/{$}", "/a/", "/z/"},
{"/a/", "/{z}/{$}", "/a/x"},
{"/{z}/{$}", "/a/{x...}", "/z/"},
{"/a/{foo...}", "/{z}/{$}", "/a/foo"},
{"/a/{z}/{$}", "/{z}/a/", "/a/z/"},
{"/{z}/a/", "/a/{z}/{$}", "/z/a/x"},
{"/a/{x}/b/{y...}", "/{x}/c/{y...}", "/a/x/b/"},
{"/{x}/c/{y...}", "/a/{x}/b/{y...}", "/x/c/"},
{"/a/{c}/b/", "/{x}/c/{y...}", "/a/cx/b/"},
{"/{x}/c/{y...}", "/a/{c}/b/", "/x/c/"},
{"/a/{x}/b/{$}", "/{x}/c/{y...}", "/a/x/b/"},
{"/{x}/c/{y...}", "/a/{x}/b/{$}", "/x/c/"},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
rel := pat1.comparePaths(pat2)
if rel != overlaps && rel != moreGeneral {
t.Fatalf("%s vs. %s are %s, need overlaps or moreGeneral", pat1, pat2, rel)
}
got := differencePath(pat1, pat2)
if got != test.want {
t.Errorf("%s vs. %s: got %q, want %q", test.p1, test.p2, got, test.want)
}
}
}
func TestHigherPrecedence(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want bool
}{
// 1. host
{"h/", "/", true},
{"/", "h/", false},
{"h/", "h/", false},
// 2. method
{"GET /", "/", true},
{"/", "GET /", false},
{"GET /", "POST /", false},
{"GET /", "/foo", false},
{"/foo", "GET /", false},
{"HEAD /", "GET /", true},
// 3. more specific path
{"/", "/", false},
{"/a", "/", true},
{"/", "/a", false},
{"/a", "/a", false},
{"/a/", "/a", false},
{"/a", "/a/", false},
{"/a", "/a/{x}", false},
{"/a/{x}", "/a", false},
{"/a/{x}", "/a/{x}", false},
{"/a/{x...}", "/a/{x}", false},
{"/a/{x}", "/a/{x...}", true},
{"/a/bc", "/a/b", false},
{"/a/b", "/a/bc", false},
// 4. {$}
{"/{$}", "/", true},
{"/", "/{$}", false},
{"/a/{x}/{$}", "/a/{x}/", true},
{"/a/{x}/", "/a/{x}/{$}", false},
{"/a/b/", "/a/{x}/{$}", false}, // old rule 3 would say it's true
{"/a/{x}/{$}", "/a/b/", false},
{"/a/{$}", "/b/{$}", false},
// false
{"/{x}/{y}", "/{x}/a", false},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
got := pat1.HigherPrecedence(pat2)
if got != test.want {
t.Errorf("%q.HigherPrecedence(%q) = %t, want %t",
test.p1, test.p2, got, test.want)
}
}
}
func TestConflictsWith(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want bool
}{
{"/a", "/a", true},
{"/a", "/ab", false},
{"/a/b/cd", "/a/b/cd", true},
{"/a/b/cd", "/a/b/c", false},
{"/a/b/c", "/a/c/c", false},
{"/{x}", "/{y}", true},
{"/{x}", "/a", false}, // more specific
{"/{x}/{y}", "/{x}/a", false},
{"/{x}/{y}", "/{x}/a/b", false},
{"/{x}", "/a/{y}", false},
{"/{x}/{y}", "/{x}/a/", false},
{"/{x}", "/a/{y...}", false}, // more specific
{"/{x}/a/{y}", "/{x}/a/{y...}", false}, // more specific
{"/{x}/{y}", "/{x}/a/{$}", false}, // more specific
{"/{x}/{y}/{$}", "/{x}/a/{$}", false},
{"/a/{x}", "/{x}/b", true},
{"/", "GET /", false},
{"/", "GET /foo", false},
{"GET /", "GET /foo", false},
{"GET /", "/foo", true},
{"GET /foo", "HEAD /", true},
} {
pat1 := mustParse(t, test.p1)
pat2 := mustParse(t, test.p2)
got := pat1.ConflictsWith(pat2)
if got != test.want {
t.Errorf("%q.ConflictsWith(%q) = %t, want %t",
test.p1, test.p2, got, test.want)
}
// ConflictsWith should be commutative.
got = pat2.ConflictsWith(pat1)
if got != test.want {
t.Errorf("%q.ConflictsWith(%q) = %t, want %t",
test.p2, test.p1, got, test.want)
}
}
}
func TestRegisterConflict(t *testing.T) {
mux := NewServeMux()
pat1 := "/a/{x}/"
if err := mux.register(pat1, http.NotFoundHandler()); err != nil {
t.Fatal(err)
}
pat2 := "/a/{y}/{z...}"
err := mux.register(pat2, http.NotFoundHandler())
var got string
if err == nil {
got = "<nil>"
} else {
got = err.Error()
}
q1 := regexp.QuoteMeta(pat1)
q2 := regexp.QuoteMeta(pat2)
wantre := `pattern "` + q2 +
`" \(registered at .*/pattern_test.go:\d+\) conflicts with pattern "` +
q1 +
`" \(registered at .*/pattern_test.go:\d+\):
` + q2 + ` matches the same requests as ` + q1
m, err := regexp.MatchString(wantre, got)
if err != nil {
t.Fatal(err)
}
if !m {
t.Errorf("got\n%s\nwant\n%s", got, wantre)
}
}
func TestDescribeRelationship(t *testing.T) {
for _, test := range []struct {
p1, p2 string
want string
}{
{"/a/{x}", "/a/{y}", "matches the same"},
{"/a/{x}", "/{y}/b", "neither is more specific"},
{"GET /", "/", "is more specific than"},
{"GET /", "HEAD /", "is more specific than"},
{"GET /", "HEAD /", "matches \"GET"},
{"POST /", "/", "matches \"GET"},
{"/foo", "/", "is more specific than"},
{"/", "GET /", "is more specific than"},
{"/", "/foo", "is more specific than"},
{"a.com/b", "/b", "does not have a host"},
{"a.com/b", "b.com/b", "different hosts"},
} {
got := DescribeRelationship(test.p1, test.p2)
fmt.Println(got)
if !strings.Contains(got, test.want) {
t.Errorf("%s vs. %s:\ngot:\n%s\nwhich does not contain %q",
test.p1, test.p2, got, test.want)
}
}
}
func mustParse(t *testing.T, s string) *Pattern {
t.Helper()
p, err := Parse(s)
if err != nil {
t.Fatal(err)
}
return p
}