-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
executable file
·274 lines (236 loc) · 8.87 KB
/
test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
local http = require('http.client').new()
local json = require('json')
local log = require('log')
local SERVER = arg[1]
local long_key = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ..
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
local function request(method, key, body, header)
if header == nil then
header = { ["Content-Type"] = "application/json" }
end
local resp = http:request(method, SERVER .. '/kv/' .. key, body, { headers = header })
-- log.error(resp.status .. ' ' )
-- if resp.body then log.error(resp.body) end
return resp
end
local function testFailed(msg)
log.error(debug.getinfo(2).currentline .. ' Test failed with msg ' .. msg)
os.exit(1)
end
local function checkPOST()
local js = nil
local js_text = nil
local resp = request('POST', 'testKey', nil)
if resp.status ~= 405 then
testFailed(resp.status .. ' ' .. resp.body)
end
js_text = ''
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = "key", ["value"] = true }
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
--[[
-- json module can not validate json properly
js = {["key"] = "key", ["key"] = "key", ["value"] = {["a"] = 1} }
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 201 then
testFailed('Wrong json schema ' .. resp.status .. ' ' .. resp.body)
end
js = {["key"] = "key", ["key"] = "key", ["value"] = {["a"] = 1} , ["value"] = {["a"] = 1}}
js_text = '{"key": "key", "key": "1", "value": {"a": 3} , "value": {"a": 1}}'
resp = request('POST', '', js_text)
if resp.status ~= 403 then
testFailed('Wrong json schema ' .. resp.status .. ' ' .. resp.body)
end
--]]
js = {["joke"] = "key", ["value"] = {["a"] = 1}}
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = "key", ["joke"] = {["a"] = 1}}
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = long_key, ["value"] = {["a"] = 1}}
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = "#%^&*()", ["value"] = {["a"] = 1}}
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = "key", ["value"] = {["a"] = 1} }
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 201 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = {["key"] = "key", ["value"] = {["a"] = 1} }
js_text = json.encode(js)
resp = request('POST', '', js_text)
if resp.status ~= 409 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('DELETE', 'key', nil)
if resp.status ~= 200 then
testFailed(resp.status .. ' ' .. resp.body)
end
end
local function checkPOST_PUT_GET_DELETE()
local js = {["key"] = "POST_PUT_GET_DELETE", ["value"] = {["a"] = 1} }
local js_text = json.encode(js)
local js_orig = json.encode(js)
local resp
resp = request('POST', '', js_text)
if resp.status ~= 201 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = json.decode(resp.body)
js = json.encode(js.data)
if js ~= js_orig then
testFailed(js)
end
js = {["value"] = {["a"] = 1} }
js_text = json.encode(js)
resp = request('PUT', 'POST_PUT_GET_DELETE', js_text)
if resp.status ~= 200 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = json.decode(resp.body)
js = json.encode(js.data)
if js ~= js_orig then
testFailed(js)
end
resp = request('GET', 'POST_PUT_GET_DELETE', '')
if resp.status ~= 200 then
testFailed(resp.status)
end
js = json.decode(resp.body)
js = json.encode(js.data)
if js ~= js_orig then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('DELETE', 'POST_PUT_GET_DELETE', nil)
if resp.status ~= 200 then
testFailed(resp.status .. ' ' .. resp.body)
end
js = json.decode(resp.body)
js = json.encode(js.data)
if js ~= js_orig then
testFailed(js)
end
end
local function checkDELETE()
local js = {["key"] = "delete", ["value"] = {["a"] = 1} }
local js_text = json.encode(js)
local resp
resp = request('POST', '', js_text)
if resp.status ~= 201 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('DELETE', 'delete', nil)
if resp.status ~= 200 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('DELETE', 'delete', nil)
if resp.status ~= 404 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('DELETE', '', nil)
if resp.status ~= 405 then
testFailed(resp.status .. ' ' .. resp.body)
end
end
local function checkPUT()
local js = {["key"] = "blabla", ["value"] = {["a"] = 1} }
local js_text = json.encode(js)
local resp
resp = request('PUT', 'PUT', js_text)
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('PUT', '', js_text)
if resp.status ~= 405 then
testFailed(resp.status .. ' ' .. resp.body)
end
end
local function checkGET()
local js = {["key"] = "blabla", ["value"] = {["a"] = 1} }
local js_text = json.encode(js)
local resp
resp = request('POST', '', '32423')
if resp.status ~= 400 then
testFailed(resp.status .. ' ' .. resp.body)
end
resp = request('GET', '23', '')
if resp.status ~= 404 then
testFailed(resp.status .. ' ' .. resp.body)
end
end
log.info('Start')
checkPOST()
checkPOST_PUT_GET_DELETE()
checkDELETE()
checkPUT()
checkGET()
log.info('All test passed')