-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathmapping_rule_spec.lua
168 lines (141 loc) · 5.09 KB
/
mapping_rule_spec.lua
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
local MappingRule = require('apicast.mapping_rule')
describe('mapping_rule', function()
describe('.from_proxy_rule', function()
it('sets "last"', function()
local mapping_rule = MappingRule.from_proxy_rule({
last = true,
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
assert.is_true(mapping_rule.last)
end)
it('sets "last" to false by default', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
assert.is_false(mapping_rule.last)
end)
end)
describe('.matches', function()
it('returns true when method, URI, and args match', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('GET', '/abc', { a_param = '1' })
assert.is_true(match)
end)
it('returns true when method and URI match, and no args are required', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('GET', '/abc', { a_param = '1' })
assert.is_true(match)
end)
it('returns false when the method does not match', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('POST', '/abc', { a_param = '1' })
assert.is_false(match)
end)
it('returns false when the URI does not match', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('GET', '/aaa', { a_param = '1' })
assert.is_false(match)
end)
it('returns false when the args do not match', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('GET', '/abc', { a_param = '2' })
assert.is_false(match)
end)
it('returns false when method, URI, and args do not match', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/abc',
querystring_parameters = { a_param = '1' },
metric_system_name = 'hits',
delta = 1
})
local match = mapping_rule:matches('POST', '/def', { x = 'y' })
assert.is_false(match)
end)
it('returns true when wildcard value has special characters: @ : % etc.', function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = '/foo/{wildcard}/bar',
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})
assert.is_true(mapping_rule:matches('GET', '/foo/a@b/bar'))
assert.is_true(mapping_rule:matches('GET', '/foo/a:b/bar'))
assert.is_true(mapping_rule:matches('GET', "/foo/a%b/bar"))
end)
it('double slashes are transformed correctly to a simple one', function()
local test_cases = {
["/foo//bar"] = "/foo/bar",
["/foo///bar"] = "/foo/bar",
["/foo/ /bar"] = "/foo/ /bar",
["/foo/bar///"] = "/foo/bar/",
["///foo///bar///"] = "/foo/bar/",
}
for key, value in pairs(test_cases) do
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = key,
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})
assert.is_true(mapping_rule:matches('GET', value), "Invalid key:" .. key)
end
end)
end)
describe('.any_method', function()
it("Allow connections when any method is defined", function()
local mapping_rule = MappingRule.from_proxy_rule({
http_method = MappingRule.any_method,
pattern = '/foo/',
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})
assert.is_true(mapping_rule:matches('GET', '/foo/'))
assert.is_true(mapping_rule:matches('POST', '/foo/'))
assert.is_true(mapping_rule:matches('PUT', "/foo/"))
assert.is_true(mapping_rule:matches('DELETE', "/foo/"))
assert.is_true(mapping_rule:matches('PATCH', "/foo/"))
end)
end)
end)