-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathquery_params_spec.lua
127 lines (96 loc) · 3.62 KB
/
query_params_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
local QueryParams = require('apicast.query_params')
describe('QueryParams', function()
before_each(function()
stub(ngx.req, 'set_uri_args')
end)
describe('.push', function()
describe('if the arg is not in the query', function()
it('creates it with the given value', function()
local params = QueryParams.new({ a = '1' })
params:push('b', '2')
local expected_args = { a = '1', b = '2' }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
describe('if the arg is in the query', function()
describe('and it has a single value', function()
it('adds a new value for it', function()
local params = QueryParams.new({ a = '1' })
params:push('a', '2')
local expected_args = { a = { '1', '2' } }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
describe('and it is an array', function()
it('adds a new value for it', function()
local params = QueryParams.new({ a = { '1', '2' } })
params:push('a', '3')
local expected_args = { a = { '1', '2', '3' } }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
end)
end)
describe('.set', function()
describe('if the arg is not in the query', function()
it('creates it with the given value', function()
local params = QueryParams.new({ a = '1' })
params:set('b', '2')
local expected_args = { a = '1', b = '2' }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
describe('if the arg is in the query', function()
it('replaces its value with the given one', function()
local params = QueryParams.new({ a = { '1', '2' } })
params:set('a', '3')
local expected_args = { a = '3' }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
end)
describe('.add', function()
describe('if the arg is not in the query', function()
it('does nothing', function()
local params = QueryParams.new({ a = '1' })
params:add('b', '2')
assert.stub(ngx.req.set_uri_args).was_not_called()
end)
end)
describe('if the arg is in the query', function()
describe('and it has a single value', function()
it('adds a new value for it', function()
local params = QueryParams.new({ a = '1' })
params:add('a', '2')
local expected_args = { a = { '1', '2' } }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
describe('and it is an array', function()
it('adds a new value for it', function()
local params = QueryParams.new({ a = { '1', '2' } })
params:add('a', '3')
local expected_args = { a = { '1', '2', '3' } }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
end)
end)
describe('.delete', function()
describe('if the argument is in the query', function()
it('deletes it', function()
local params = QueryParams.new({ a = '1', b = '2' })
params:delete('a')
local expected_args = { b = '2' }
assert.stub(ngx.req.set_uri_args).was_called_with(expected_args)
end)
end)
describe('if the argument is not in the query', function()
it('does not delete anything', function()
local params = QueryParams.new({ a = '1' })
params:delete('b')
assert.stub(ngx.req.set_uri_args).was_not_called()
end)
end)
end)
end)