-
Notifications
You must be signed in to change notification settings - Fork 170
/
soap_spec.lua
149 lines (120 loc) · 4.79 KB
/
soap_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
local Usage = require('apicast.usage')
describe('SOAP policy', function()
describe('.rewrite', function()
local context -- Context shared between policies
local full_url = "http://www.example.com:80/path/to/myfile.html?" ..
"key1=value1&key2=value2#SomewhereInTheDocument"
-- Define a config with 3 rules. Their patterns have values that allow us
-- to easily associate them with a SOAP action receive via SOAPAction
-- header or via Content-Type. The third one is used to tests matching of
-- full URLs.
local policy_config = {
mapping_rules = {
{
pattern = '/soap_action$',
metric_system_name = 'hits',
delta = 10
},
{
pattern = '/soap_action_ctype$',
metric_system_name = 'hits',
delta = 20
},
{
pattern = full_url,
metric_system_name = 'hits',
delta = 30
},
}
}
local soap_policy = require('apicast.policy.soap').new(policy_config)
before_each(function()
-- Initialize a shared context with a usage of hits = 1.
context = { usage = Usage.new() }
context.usage:add('hits', 1)
end)
describe('when the SOAP action is in the SOAPAction header', function()
it('calculates the usage and merges it with the one in the context', function()
ngx.req.get_headers = function()
return { SOAPAction = '/soap_action' }
end
soap_policy:rewrite(context)
assert.equals(11, context.usage.deltas['hits'])
end)
end)
describe('when the SOAP action is in the Content-Type header', function()
describe('and it is the only param', function()
it('calculates the usage and merges it with the one in the context', function()
local header_val = "application/soap+xml;action=/soap_action_ctype"
ngx.req.get_headers = function()
return { ["Content-Type"] = header_val }
end
soap_policy:rewrite(context)
assert.equals(21, context.usage.deltas['hits'])
end)
end)
describe('and there are other params', function()
it('calculates the usage and merges it with the one in the context', function()
local header_val = "application/soap+xml;a_param=x;" ..
"action=/soap_action_ctype;another_param=y"
ngx.req.get_headers = function()
return { ["Content-Type"] = header_val }
end
soap_policy:rewrite(context)
assert.equals(21, context.usage.deltas['hits'])
end)
end)
describe('and the params contain some upper-case chars or spaces', function()
it('calculates the usage and merges it with the one in the context', function()
local header_vals = {
-- Upper-case chars in type/subtype
"Application/SOAP+xml;action=/soap_action_ctype",
-- Upper-case chars in 'Action'
"application/soap+xml;Action=/soap_action_ctype",
-- "" in action value
'application/soap+xml;action="/soap_action_ctype"',
-- Spaces
"application/soap+xml; action=/soap_action_ctype; a_param=x"
}
for _, header_val in ipairs(header_vals) do
ngx.req.get_headers = function()
return { ["Content-Type"] = header_val }
end
context = { usage = Usage.new() }
context.usage:add('hits', 1)
soap_policy:rewrite(context)
assert.equals(21, context.usage.deltas['hits'])
end
end)
end)
describe('and the action is a full URL', function()
it('calculates the usage and merges it with the one in the context', function()
ngx.req.get_headers = function()
return { ["Content-Type"] = 'application/soap+xml;action=' .. full_url }
end
soap_policy:rewrite(context)
assert.equals(31, context.usage.deltas['hits'])
end)
end)
end)
describe('when the SOAP action is in the SOAPAction and the Content-Type headers', function()
it('calculates the usage and merges it with the one in the context', function()
ngx.req.get_headers = function()
return {
SOAPAction = '/soap_action',
["Content-Type"] = "application/soap+xml;action=/soap_action_ctype"
}
end
soap_policy:rewrite(context)
assert.equals(21, context.usage.deltas['hits'])
end)
end)
describe('when the SOAP action is not specified', function()
it('it does not modify the usage received in the context', function()
ngx.req.get_headers = function() return {} end
soap_policy:rewrite(context)
assert.equals(1, context.usage.deltas['hits'])
end)
end)
end)
end)