1package framework
2
3import (
4	"testing"
5
6	"github.com/go-test/deep"
7)
8
9func TestPath_Regex(t *testing.T) {
10	tests := []struct {
11		pattern   string
12		input     string
13		pathMatch bool
14		captures  map[string]string
15	}{
16		{
17			pattern:   "a/b/" + GenericNameRegex("val"),
18			input:     "a/b/foo",
19			pathMatch: true,
20			captures:  map[string]string{"val": "foo"},
21		},
22		{
23			pattern:   "a/b/" + GenericNameRegex("val"),
24			input:     "a/b/foo/more",
25			pathMatch: false,
26			captures:  nil,
27		},
28		{
29			pattern:   "a/b/" + GenericNameRegex("val"),
30			input:     "a/b/abc-.123",
31			pathMatch: true,
32			captures:  map[string]string{"val": "abc-.123"},
33		},
34		{
35			pattern:   "a/b/" + GenericNameRegex("val") + "/c/d",
36			input:     "a/b/foo/c/d",
37			pathMatch: true,
38			captures:  map[string]string{"val": "foo"},
39		},
40		{
41			pattern:   "a/b/" + GenericNameRegex("val") + "/c/d",
42			input:     "a/b/foo/c/d/e",
43			pathMatch: false,
44			captures:  nil,
45		},
46		{
47			pattern:   "a/b" + OptionalParamRegex("val"),
48			input:     "a/b",
49			pathMatch: true,
50			captures:  map[string]string{"val": ""},
51		},
52		{
53			pattern:   "a/b" + OptionalParamRegex("val"),
54			input:     "a/b/foo",
55			pathMatch: true,
56			captures:  map[string]string{"val": "foo"},
57		},
58		{
59			pattern:   "foo/" + MatchAllRegex("val"),
60			input:     "foos/ball",
61			pathMatch: false,
62			captures:  nil,
63		},
64		{
65			pattern:   "foos/" + MatchAllRegex("val"),
66			input:     "foos/ball",
67			pathMatch: true,
68			captures:  map[string]string{"val": "ball"},
69		},
70		{
71			pattern:   "foos/ball/" + MatchAllRegex("val"),
72			input:     "foos/ball/with/more/stuff/at_the/end",
73			pathMatch: true,
74			captures:  map[string]string{"val": "with/more/stuff/at_the/end"},
75		},
76		{
77			pattern:   MatchAllRegex("val"),
78			input:     "foos/ball/with/more/stuff/at_the/end",
79			pathMatch: true,
80			captures:  map[string]string{"val": "foos/ball/with/more/stuff/at_the/end"},
81		},
82	}
83
84	for i, test := range tests {
85		b := Backend{
86			Paths: []*Path{{Pattern: test.pattern}},
87		}
88		path, captures := b.route(test.input)
89		pathMatch := path != nil
90		if pathMatch != test.pathMatch {
91			t.Fatalf("[%d] unexpected path match result (%s): expected %t, actual %t", i, test.pattern, test.pathMatch, pathMatch)
92		}
93		if diff := deep.Equal(captures, test.captures); diff != nil {
94			t.Fatal(diff)
95		}
96	}
97
98}
99