-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy path03-api_spec.lua
More file actions
184 lines (168 loc) · 5.33 KB
/
03-api_spec.lua
File metadata and controls
184 lines (168 loc) · 5.33 KB
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
local cjson = require "cjson"
local helpers = require "spec.helpers"
local Errors = require "kong.db.errors"
for _, strategy in helpers.each_strategy() do
describe("Plugin: rate-limiting (API) [#" .. strategy .. "]", function()
local admin_client
local bp
lazy_setup(function()
bp = helpers.get_db_utils(strategy, {
"routes",
"services",
"plugins",
})
end)
lazy_teardown(function()
if admin_client then
admin_client:close()
end
helpers.stop_kong()
end)
-- Some random changes in a file --- 11
describe("POST", function()
local route, route2
lazy_setup(function()
local service = bp.services:insert()
route = bp.routes:insert {
hosts = { "test1.test" },
protocols = { "http", "https" },
service = service
}
route2 = bp.routes:insert {
hosts = { "test2.test" },
protocols = { "http", "https" },
service = service
}
assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
admin_client = helpers.admin_client()
end)
it("should not save with empty config", function()
local res = assert(admin_client:send {
method = "POST",
path = "/plugins",
body = {
name = "rate-limiting",
route = { id = route.id },
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(400, res)
local json = cjson.decode(body)
local msg = [[at least one of these fields must be non-empty: ]] ..
[['config.second', 'config.minute', 'config.hour', ]] ..
[['config.day', 'config.month', 'config.year']]
assert.same({
code = Errors.codes.SCHEMA_VIOLATION,
fields = {
["@entity"] = { msg }
},
message = "schema violation (" .. msg .. ")",
name = "schema violation",
}, json)
end)
it("should save with proper config", function()
local res = assert(admin_client:send {
method = "POST",
path = "/plugins",
body = {
name = "rate-limiting",
route = { id = route.id },
config = {
second = 10
}
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = cjson.decode(assert.res_status(201, res))
assert.equal(10, body.config.second)
end)
if strategy == "off" then
it("sets policy to local by default on dbless", function()
local id = "bac2038a-205c-4013-8830-e6dde503a3e3"
local res = admin_client:post("/config", {
body = {
_format_version = "1.1",
plugins = {
{
id = id,
name = "rate-limiting",
config = {
second = 10
}
}
}
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = cjson.decode(assert.res_status(201, res))
assert.equal("local", body.plugins[id].config.policy)
end)
it("does not allow setting policy to cluster on dbless", function()
local id = "bac2038a-205c-4013-8830-e6dde503a3e3"
local res = admin_client:post("/config", {
body = {
_format_version = "1.1",
plugins = {
{
id = id,
name = "rate-limiting",
config = {
policy = "cluster",
second = 10
}
}
}
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = cjson.decode(assert.res_status(400, res))
assert.equal("expected one of: local, redis", body.fields.plugins[1].config.policy)
end)
else
it("sets policy to local by default", function()
local res = admin_client:post("/plugins", {
body = {
name = "rate-limiting",
config = {
second = 10
}
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = cjson.decode(assert.res_status(201, res))
assert.equal("local", body.config.policy)
end)
it("does allow setting policy to cluster on non-dbless", function()
local res = admin_client:post("/plugins", {
body = {
name = "rate-limiting",
route = { id = route2.id },
config = {
policy = 'cluster',
second = 10
}
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = cjson.decode(assert.res_status(201, res))
assert.equal("cluster", body.config.policy)
end)
end
end)
end)
end