2
2
3
3
local t = require (' luatest' )
4
4
local g = t .group (' http' )
5
- local tap = require (' tap' )
6
5
local fio = require (' fio' )
7
6
local http_lib = require (' http.lib' )
8
7
local http_client = require (' http.client' )
@@ -57,8 +56,12 @@ g.test_split_uri = function()
57
56
check (' http://abc:123?' , { scheme = ' http' , host = ' abc' , service = ' 123' })
58
57
check (' http://abc:123?query' , { scheme = ' http' , host = ' abc' ,
59
58
service = ' 123' , query = ' query' })
60
- check (' http://domain.subdomain.com:service?query' , { scheme = ' http' ,
61
- host = ' domain.subdomain.com' , service = ' service' , query = ' query' })
59
+ check (' http://domain.subdomain.com:service?query' , {
60
+ scheme = ' http' ,
61
+ host = ' domain.subdomain.com' ,
62
+ service = ' service' ,
63
+ query = ' query' ,
64
+ })
62
65
check (' google.com' , { host = ' google.com' })
63
66
check (' google.com?query' , { host = ' google.com' , query = ' query' })
64
67
check (' google.com/abc?query' , { host = ' google.com' , path = ' /abc' ,
@@ -99,12 +102,12 @@ g.test_template = function()
99
102
tt [i ] = string.rep (' #' , i )
100
103
end
101
104
102
- local rendered , code = http_lib .template (template , { t = tt })
105
+ local rendered = http_lib .template (template , { t = tt })
103
106
t .assertTrue (# rendered > 10000 , " rendered size" )
104
107
t .assertEquals (rendered :sub (# rendered - 7 , # rendered - 1 ), " </html>" , " rendered eof" )
105
108
end
106
109
107
- g .test_parse_request = function (test )
110
+ g .test_parse_request = function ()
108
111
109
112
t .assertEquals (http_lib ._parse_request (' abc' ),
110
113
{ error = ' Broken request line' , headers = {} }, ' broken request' )
@@ -138,7 +141,7 @@ g.test_parse_request = function(test)
138
141
)
139
142
end
140
143
141
- g .test_params = function (test )
144
+ g .test_params = function ()
142
145
t .assertEquals (http_lib .params (), {}, ' nil string' )
143
146
t .assertEquals (http_lib .params (' ' ), {}, ' empty string' )
144
147
t .assertEquals (http_lib .params (' a' ), {a = ' ' }, ' separate literal' )
@@ -197,15 +200,15 @@ local function cfgserv()
197
200
:route ({path = ' /aba*def' }, function () end )
198
201
:route ({path = ' /abb*def/cde' , name = ' star' }, function () end )
199
202
:route ({path = ' /banners/:token' })
200
- :helper (' helper_title' , function (self , a ) return ' Hello, ' .. a end )
203
+ :helper (' helper_title' , function (_ , a ) return ' Hello, ' .. a end )
201
204
:route ({path = ' /helper' , file = ' helper.html.el' })
202
205
:route ({ path = ' /test' , file = ' test.html.el' },
203
206
function (cx ) return cx :render ({ title = ' title: 123' }) end )
204
207
httpd :set_router (router )
205
208
return httpd , router
206
209
end
207
210
208
- g .test_server_url_match = function (test )
211
+ g .test_server_url_match = function ()
209
212
local httpd , router = cfgserv ()
210
213
t .assertIsTable (httpd , " httpd object" )
211
214
t .assertIsNil (router :match (' GET' , ' /' ))
241
244
242
245
243
246
g .test_server_url_for = function ()
244
- local httpd , router = cfgserv ()
247
+ local _ , router = cfgserv ()
245
248
t .assertEquals (router :url_for (' abcdef' ), ' /abcdef' , ' /abcdef' )
246
249
t .assertEquals (router :url_for (' test' ), ' /abc//' , ' /abc//' )
247
250
t .assertEquals (router :url_for (' test' , { cde = ' cde_v' , def = ' def_v' }),
@@ -264,17 +267,17 @@ g.test_server_requests = function()
264
267
t .assertEquals (r .reason , ' Ok' , ' /test reason' )
265
268
t .assertEquals (string.match (r .body , ' title: 123' ), ' title: 123' , ' /test body' )
266
269
267
- local r = http_client .get (' http://127.0.0.1:12345/test404' )
270
+ r = http_client .get (' http://127.0.0.1:12345/test404' )
268
271
t .assertEquals (r .status , 404 , ' /test404 code' )
269
272
-- broken in built-in tarantool/http
270
273
-- t.assertEquals(r.reason, 'Not found', '/test404 reason')
271
274
272
- local r = http_client .get (' http://127.0.0.1:12345/absent' )
275
+ r = http_client .get (' http://127.0.0.1:12345/absent' )
273
276
t .assertEquals (r .status , 500 , ' /absent code' )
274
277
-- t.assertEquals(r.reason, 'Internal server error', '/absent reason')
275
278
t .assertEquals (string.match (r .body , ' load module' ), ' load module' , ' /absent body' )
276
279
277
- local r = http_client .get (' http://127.0.0.1:12345/ctxaction' )
280
+ r = http_client .get (' http://127.0.0.1:12345/ctxaction' )
278
281
t .assertEquals (r .status , 200 , ' /ctxaction code' )
279
282
t .assertEquals (r .reason , ' Ok' , ' /ctxaction reason' )
280
283
t .assertEquals (string.match (r .body , ' Hello, Tarantool' ), ' Hello, Tarantool' ,
@@ -284,49 +287,49 @@ g.test_server_requests = function()
284
287
t .assertEquals (string.match (r .body , ' controller: module[.]controller' ),
285
288
' controller: module.controller' , ' /ctxaction body controller' )
286
289
287
- local r = http_client .get (' http://127.0.0.1:12345/ctxaction.invalid' )
290
+ r = http_client .get (' http://127.0.0.1:12345/ctxaction.invalid' )
288
291
t .assertEquals (r .status , 404 , ' /ctxaction.invalid code' ) -- WTF?
289
292
-- t.assertEquals(r.reason, 'Not found', '/ctxaction.invalid reason')
290
293
-- t.assertEquals(r.body, '', '/ctxaction.invalid body')
291
294
292
- local r = http_client .get (' http://127.0.0.1:12345/hello.html' )
295
+ r = http_client .get (' http://127.0.0.1:12345/hello.html' )
293
296
t .assertEquals (r .status , 200 , ' /hello.html code' )
294
297
t .assertEquals (r .reason , ' Ok' , ' /hello.html reason' )
295
298
t .assertEquals (string.match (r .body , ' static html' ), ' static html' ,
296
299
' /hello.html body' )
297
300
298
- local r = http_client .get (' http://127.0.0.1:12345/absentaction' )
301
+ r = http_client .get (' http://127.0.0.1:12345/absentaction' )
299
302
t .assertEquals (r .status , 500 , ' /absentaction 500' )
300
303
-- t.assertEquals(r.reason, 'Internal server error', '/absentaction reason')
301
304
t .assertEquals (string.match (r .body , ' contain function' ), ' contain function' ,
302
305
' /absentaction body' )
303
306
304
- local r = http_client .get (' http://127.0.0.1:12345/helper' )
307
+ r = http_client .get (' http://127.0.0.1:12345/helper' )
305
308
t .assertEquals (r .status , 200 , ' helper 200' )
306
309
t .assertEquals (r .reason , ' Ok' , ' helper reason' )
307
310
t .assertEquals (string.match (r .body , ' Hello, world' ), ' Hello, world' , ' helper body' )
308
311
309
- local r = http_client .get (' http://127.0.0.1:12345/helper?abc' )
312
+ r = http_client .get (' http://127.0.0.1:12345/helper?abc' )
310
313
t .assertEquals (r .status , 200 , ' helper?abc 200' )
311
314
t .assertEquals (r .reason , ' Ok' , ' helper?abc reason' )
312
315
t .assertEquals (string.match (r .body , ' Hello, world' ), ' Hello, world' , ' helper body' )
313
316
314
317
router :route ({path = ' /die' , file = ' helper.html.el' },
315
318
function () error (123 ) end )
316
319
317
- local r = http_client .get (' http://127.0.0.1:12345/die' )
320
+ r = http_client .get (' http://127.0.0.1:12345/die' )
318
321
t .assertEquals (r .status , 500 , ' die 500' )
319
322
-- t.assertEquals(r.reason, 'Internal server error', 'die reason')
320
323
321
324
router :route ({ path = ' /info' }, function (cx )
322
325
return cx :render ({ json = cx :peer () })
323
326
end )
324
327
325
- local r = json .decode (http_client .get (' http://127.0.0.1:12345/info' ).body )
328
+ r = json .decode (http_client .get (' http://127.0.0.1:12345/info' ).body )
326
329
t .assertEquals (r .host , ' 127.0.0.1' , ' peer.host' )
327
330
t .assertIsNumber (r .port , ' peer.port' )
328
331
329
- local r = router :route ({method = ' POST' , path = ' /dit' , file = ' helper.html.el' },
332
+ r = router :route ({method = ' POST' , path = ' /dit' , file = ' helper.html.el' },
330
333
function (tx )
331
334
return tx :render ({text = ' POST = ' .. tx :read ()})
332
335
end )
@@ -376,7 +379,7 @@ g.test_server_requests = function()
376
379
end )
377
380
378
381
-- http client currently doesn't support chunked encoding
379
- local r = http_client .get (' http://127.0.0.1:12345/chunked' )
382
+ r = http_client .get (' http://127.0.0.1:12345/chunked' )
380
383
t .assertEquals (r .status , 200 , ' chunked 200' )
381
384
t .assertEquals (r .headers [' transfer-encoding' ], ' chunked' , ' chunked headers' )
382
385
t .assertEquals (r .body , ' chunkedencodingt\r\n est' , ' chunked body' )
@@ -389,7 +392,7 @@ g.test_server_requests = function()
389
392
text = (' foo=%s; baz=%s' ):format (foo , baz )
390
393
})
391
394
end )
392
- local r = http_client .get (' http://127.0.0.1:12345/receive_cookie' , {
395
+ r = http_client .get (' http://127.0.0.1:12345/receive_cookie' , {
393
396
headers = {
394
397
cookie = ' foo=bar; baz=feez' ,
395
398
}
@@ -405,7 +408,7 @@ g.test_server_requests = function()
405
408
resp :setcookie ({ name = ' xxx' , value = ' yyy' })
406
409
return resp
407
410
end )
408
- local r = http_client .get (' http://127.0.0.1:12345/cookie' )
411
+ r = http_client .get (' http://127.0.0.1:12345/cookie' )
409
412
t .assertEquals (r .status , 200 , ' status' )
410
413
t .assertTrue (r .headers [' set-cookie' ] ~= nil , " header" )
411
414
@@ -425,7 +428,7 @@ g.test_server_requests = function()
425
428
status = 200 ,
426
429
}
427
430
end )
428
- local r = http_client .get (
431
+ r = http_client .get (
429
432
' http://127.0.0.1:12345/check_req_properties?foo=1&bar=2' , {
430
433
headers = {
431
434
[' X-test-header' ] = ' test-value'
@@ -475,7 +478,7 @@ g.test_server_requests = function()
475
478
})
476
479
t .assertEquals (r .status , 200 , ' status' )
477
480
478
- local parsed_body = json .decode (r .body )
481
+ parsed_body = json .decode (r .body )
479
482
t .assertEquals (parsed_body .request_line , ' POST /check_req_methods_for_json HTTP/1.1' , ' req.request_line' )
480
483
t .assertEquals (parsed_body .read_cached , ' {"kind": "json"}' , ' json req:read_cached()' )
481
484
t .assertEquals (parsed_body .json , {kind = " json" }, ' req:json()' )
@@ -491,22 +494,21 @@ g.test_server_requests = function()
491
494
492
495
if is_builtin_test () then
493
496
router :route ({ path = ' /post' , method = ' POST' }, function (req )
494
- local t = {
497
+ return req : render ({ json = {
495
498
# req :read (" \n " );
496
499
# req :read (10 );
497
500
# req :read ({ size = 10 , delimiter = " \n " });
498
501
# req :read (" \n " );
499
502
# req :read ();
500
503
# req :read ();
501
504
# req :read ();
502
- }
503
- return req :render ({json = t })
505
+ }})
504
506
end )
505
507
local bodyf = os.getenv (' LUA_SOURCE_DIR' ) or ' ./'
506
508
bodyf = io.open (fio .pathjoin (bodyf , ' test/public/lorem.txt' ))
507
509
local body = bodyf :read (' *a' )
508
510
bodyf :close ()
509
- local r = http_client .post (' http://127.0.0.1:12345/post' , body )
511
+ r = http_client .post (' http://127.0.0.1:12345/post' , body )
510
512
t .assertEquals (r .status , 200 , ' status' )
511
513
t .assertEquals (json .decode (r .body ), { 541 ,10 ,10 ,458 ,1375 ,0 ,0 },
512
514
' req:read() results' )
@@ -558,7 +560,7 @@ g.test_server_requests = function()
558
560
body = ' GET *' ,
559
561
}
560
562
end )
561
- local r = http_client .get (' http://127.0.0.1:12345/a/b/c' )
563
+ r = http_client .get (' http://127.0.0.1:12345/a/b/c' )
562
564
t .assertEquals (r .status , 200 , ' /a/b/c request returns 200' )
563
565
t .assertEquals (r .body , ' GET *' , ' GET * matches' )
564
566
@@ -568,7 +570,7 @@ g.test_server_requests = function()
568
570
body = ' ANY /a/:foo/:bar' ,
569
571
}
570
572
end )
571
- local r = http_client .get (' http://127.0.0.1:12345/a/b/c' )
573
+ r = http_client .get (' http://127.0.0.1:12345/a/b/c' )
572
574
t .assertEquals (r .status , 200 , ' /a/b/c request returns 200' )
573
575
t .assertEquals (
574
576
r .body ,
0 commit comments