From b660d8efe9cba9f37a682fd400171f8a30b64687 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Wed, 23 Oct 2019 12:34:08 +0300 Subject: [PATCH 1/3] Run luacheck on CI, fix warnings --- .luacheckrc | 40 ++++---------------------- .travis.yml | 5 +++- examples/middleware.lua | 2 +- http/router/fs.lua | 2 +- http/router/init.lua | 2 +- http/router/request.lua | 16 +++++------ http/server/init.lua | 7 ++--- test/http_test.lua | 64 +++++++++++++++++++++-------------------- 8 files changed, 57 insertions(+), 81 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 9320391..0be09dd 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,36 +1,8 @@ -redefined = false include_files = {"**/*.lua", "*.rockspec", "*.luacheckrc"} -exclude_files = {"lua_modules", ".luarocks", ".rocks", "luatest/luaunit.lua", "build"} -new_read_globals = { - 'box', - '_TARANTOOL', - 'tonumber64', - os = { - fields = { - 'environ', - } - }, - string = { - fields = { - 'split', - 'startswith', - }, - }, - table = { - fields = { - 'maxn', - 'copy', - 'new', - 'clear', - 'move', - 'foreach', - 'sort', - 'remove', - 'foreachi', - 'deepcopy', - 'getn', - 'concat', - 'insert', - }, - }, +exclude_files = { + "lua_modules", + ".luarocks", + ".rocks", + "build", + "http/mime_types.lua", } diff --git a/.travis.yml b/.travis.yml index fa786ff..befe9df 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,8 +23,11 @@ _test: &test - sudo apt-get -y update - sudo apt-get install -y tarantool tarantool-dev - tarantoolctl rocks make + - tarantoolctl rocks install luacheck 0.25.0 - tarantoolctl rocks install luatest 0.2.2 - script: .rocks/bin/luatest + script: + - .rocks/bin/luacheck . + - .rocks/bin/luatest _deploy: &deploy provider: packagecloud diff --git a/examples/middleware.lua b/examples/middleware.lua index 0092f62..640c9f3 100755 --- a/examples/middleware.lua +++ b/examples/middleware.lua @@ -5,7 +5,7 @@ local tsgi = require('http.tsgi') local json = require('json') local log = require('log') -box.cfg{} -- luacheck: ignore +box.cfg{} local httpd = http_server.new('127.0.0.1', 12345, { log_requests = true, diff --git a/http/router/fs.lua b/http/router/fs.lua index a635c20..0e3fe58 100644 --- a/http/router/fs.lua +++ b/http/router/fs.lua @@ -28,7 +28,7 @@ local function catfile(...) return end - for i, pe in pairs(sp) do + for _, pe in pairs(sp) do if path == nil then path = pe elseif string.match(path, '.$') ~= '/' then diff --git a/http/router/init.lua b/http/router/init.lua index db6aa63..e34ce81 100644 --- a/http/router/init.lua +++ b/http/router/init.lua @@ -175,7 +175,7 @@ local function use_middleware(self, handler, opts) before = '?string|table', after = '?string|table', }) - local opts = table.deepcopy(opts) + opts = table.deepcopy(opts) opts.handler = handler local uuid = require('uuid') diff --git a/http/router/request.lua b/http/router/request.lua index 1ba4dc5..0980025 100644 --- a/http/router/request.lua +++ b/http/router/request.lua @@ -82,13 +82,13 @@ end local function post_param(self, name) local content_type = self:content_type() - if self:content_type() == 'multipart/form-data' then + if content_type == 'multipart/form-data' then -- TODO: do that! rawset(self, 'post_params', {}) - elseif self:content_type() == 'application/json' then + elseif content_type == 'application/json' then local params = self:json() rawset(self, 'post_params', params) - elseif self:content_type() == 'application/x-www-form-urlencoded' then + elseif content_type == 'application/x-www-form-urlencoded' then local params = lib.params(self:read_cached()) local pres = {} for k, v in pairs(params) do @@ -136,8 +136,8 @@ local function cookie(self, cookiename) return nil end -local function iterate(self, gen, param, state) - return setmetatable({ body = { gen = gen, param = param, state = state } }, +local function iterate(_, gen, gen_param, state) + return setmetatable({ body = { gen = gen, param = gen_param, state = state } }, response.metatable) end @@ -168,12 +168,12 @@ end local function request_json(req) local data = req:read_cached() - local s, json = pcall(json.decode, data) + local s, json_data = pcall(json.decode, data) if s then - return json + return json_data else error(utils.sprintf("Can't decode json in request '%s': %s", - data, tostring(json))) + data, tostring(json_data))) return nil end end diff --git a/http/server/init.lua b/http/server/init.lua index 3b3385b..768ebc6 100644 --- a/http/server/init.lua +++ b/http/server/init.lua @@ -10,9 +10,9 @@ local errno = require('errno') local DETACHED = 101 -local ok, VERSION = pcall(require, 'http.VERSION') -if not ok then - VERSION = 'unknown' +local VERSION = 'unknown' +if package.search('http.VERSION') then + VERSION = require('http.VERSION') end --------- @@ -239,7 +239,6 @@ local function process_client(self, s, peer) if not s:write(response) then break end - response = nil -- luacheck: ignore 311 -- Transfer-Encoding: chunked for _, part in gen, param, state do part = tostring(part) diff --git a/test/http_test.lua b/test/http_test.lua index c60ee68..f2ca9de 100755 --- a/test/http_test.lua +++ b/test/http_test.lua @@ -2,7 +2,6 @@ local t = require('luatest') local g = t.group('http') -local tap = require('tap') local fio = require('fio') local http_lib = require('http.lib') local http_client = require('http.client') @@ -57,8 +56,12 @@ g.test_split_uri = function() check('http://abc:123?', { scheme = 'http', host = 'abc', service = '123'}) check('http://abc:123?query', { scheme = 'http', host = 'abc', service = '123', query = 'query'}) - check('http://domain.subdomain.com:service?query', { scheme = 'http', - host = 'domain.subdomain.com', service = 'service', query = 'query'}) + check('http://domain.subdomain.com:service?query', { + scheme = 'http', + host = 'domain.subdomain.com', + service = 'service', + query = 'query', + }) check('google.com', { host = 'google.com'}) check('google.com?query', { host = 'google.com', query = 'query'}) check('google.com/abc?query', { host = 'google.com', path = '/abc', @@ -99,12 +102,12 @@ g.test_template = function() tt[i] = string.rep('#', i) end - local rendered, code = http_lib.template(template, { t = tt }) + local rendered = http_lib.template(template, { t = tt }) t.assertTrue(#rendered > 10000, "rendered size") t.assertEquals(rendered:sub(#rendered - 7, #rendered - 1), "", "rendered eof") end -g.test_parse_request = function(test) +g.test_parse_request = function() t.assertEquals(http_lib._parse_request('abc'), { error = 'Broken request line', headers = {} }, 'broken request') @@ -138,7 +141,7 @@ g.test_parse_request = function(test) ) end -g.test_params = function(test) +g.test_params = function() t.assertEquals(http_lib.params(), {}, 'nil string') t.assertEquals(http_lib.params(''), {}, 'empty string') t.assertEquals(http_lib.params('a'), {a = ''}, 'separate literal') @@ -197,7 +200,7 @@ local function cfgserv() :route({path = '/aba*def'}, function() end) :route({path = '/abb*def/cde', name = 'star'}, function() end) :route({path = '/banners/:token'}) - :helper('helper_title', function(self, a) return 'Hello, ' .. a end) + :helper('helper_title', function(_, a) return 'Hello, ' .. a end) :route({path = '/helper', file = 'helper.html.el'}) :route({ path = '/test', file = 'test.html.el' }, function(cx) return cx:render({ title = 'title: 123' }) end) @@ -205,7 +208,7 @@ local function cfgserv() return httpd, router end -g.test_server_url_match = function(test) +g.test_server_url_match = function() local httpd, router = cfgserv() t.assertIsTable(httpd, "httpd object") t.assertIsNil(router:match('GET', '/')) @@ -241,7 +244,7 @@ end g.test_server_url_for = function() - local httpd, router = cfgserv() + local _, router = cfgserv() t.assertEquals(router:url_for('abcdef'), '/abcdef', '/abcdef') t.assertEquals(router:url_for('test'), '/abc//', '/abc//') t.assertEquals(router:url_for('test', { cde = 'cde_v', def = 'def_v' }), @@ -264,17 +267,17 @@ g.test_server_requests = function() t.assertEquals(r.reason, 'Ok', '/test reason') t.assertEquals(string.match(r.body, 'title: 123'), 'title: 123', '/test body') - local r = http_client.get('http://127.0.0.1:12345/test404') + r = http_client.get('http://127.0.0.1:12345/test404') t.assertEquals(r.status, 404, '/test404 code') -- broken in built-in tarantool/http --t.assertEquals(r.reason, 'Not found', '/test404 reason') - local r = http_client.get('http://127.0.0.1:12345/absent') + r = http_client.get('http://127.0.0.1:12345/absent') t.assertEquals(r.status, 500, '/absent code') --t.assertEquals(r.reason, 'Internal server error', '/absent reason') t.assertEquals(string.match(r.body, 'load module'), 'load module', '/absent body') - local r = http_client.get('http://127.0.0.1:12345/ctxaction') + r = http_client.get('http://127.0.0.1:12345/ctxaction') t.assertEquals(r.status, 200, '/ctxaction code') t.assertEquals(r.reason, 'Ok', '/ctxaction reason') t.assertEquals(string.match(r.body, 'Hello, Tarantool'), 'Hello, Tarantool', @@ -284,29 +287,29 @@ g.test_server_requests = function() t.assertEquals(string.match(r.body, 'controller: module[.]controller'), 'controller: module.controller', '/ctxaction body controller') - local r = http_client.get('http://127.0.0.1:12345/ctxaction.invalid') + r = http_client.get('http://127.0.0.1:12345/ctxaction.invalid') t.assertEquals(r.status, 404, '/ctxaction.invalid code') -- WTF? --t.assertEquals(r.reason, 'Not found', '/ctxaction.invalid reason') --t.assertEquals(r.body, '', '/ctxaction.invalid body') - local r = http_client.get('http://127.0.0.1:12345/hello.html') + r = http_client.get('http://127.0.0.1:12345/hello.html') t.assertEquals(r.status, 200, '/hello.html code') t.assertEquals(r.reason, 'Ok', '/hello.html reason') t.assertEquals(string.match(r.body, 'static html'), 'static html', '/hello.html body') - local r = http_client.get('http://127.0.0.1:12345/absentaction') + r = http_client.get('http://127.0.0.1:12345/absentaction') t.assertEquals(r.status, 500, '/absentaction 500') --t.assertEquals(r.reason, 'Internal server error', '/absentaction reason') t.assertEquals(string.match(r.body, 'contain function'), 'contain function', '/absentaction body') - local r = http_client.get('http://127.0.0.1:12345/helper') + r = http_client.get('http://127.0.0.1:12345/helper') t.assertEquals(r.status, 200, 'helper 200') t.assertEquals(r.reason, 'Ok', 'helper reason') t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') - local r = http_client.get('http://127.0.0.1:12345/helper?abc') + r = http_client.get('http://127.0.0.1:12345/helper?abc') t.assertEquals(r.status, 200, 'helper?abc 200') t.assertEquals(r.reason, 'Ok', 'helper?abc reason') t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') @@ -314,7 +317,7 @@ g.test_server_requests = function() router:route({path = '/die', file = 'helper.html.el'}, function() error(123) end ) - local r = http_client.get('http://127.0.0.1:12345/die') + r = http_client.get('http://127.0.0.1:12345/die') t.assertEquals(r.status, 500, 'die 500') --t.assertEquals(r.reason, 'Internal server error', 'die reason') @@ -322,11 +325,11 @@ g.test_server_requests = function() return cx:render({ json = cx:peer() }) end) - local r = json.decode(http_client.get('http://127.0.0.1:12345/info').body) + r = json.decode(http_client.get('http://127.0.0.1:12345/info').body) t.assertEquals(r.host, '127.0.0.1', 'peer.host') t.assertIsNumber(r.port, 'peer.port') - local r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'}, + r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'}, function(tx) return tx:render({text = 'POST = ' .. tx:read()}) end) @@ -376,7 +379,7 @@ g.test_server_requests = function() end) -- http client currently doesn't support chunked encoding - local r = http_client.get('http://127.0.0.1:12345/chunked') + r = http_client.get('http://127.0.0.1:12345/chunked') t.assertEquals(r.status, 200, 'chunked 200') t.assertEquals(r.headers['transfer-encoding'], 'chunked', 'chunked headers') t.assertEquals(r.body, 'chunkedencodingt\r\nest', 'chunked body') @@ -389,7 +392,7 @@ g.test_server_requests = function() text = ('foo=%s; baz=%s'):format(foo, baz) }) end) - local r = http_client.get('http://127.0.0.1:12345/receive_cookie', { + r = http_client.get('http://127.0.0.1:12345/receive_cookie', { headers = { cookie = 'foo=bar; baz=feez', } @@ -405,7 +408,7 @@ g.test_server_requests = function() resp:setcookie({ name = 'xxx', value = 'yyy' }) return resp end) - local r = http_client.get('http://127.0.0.1:12345/cookie') + r = http_client.get('http://127.0.0.1:12345/cookie') t.assertEquals(r.status, 200, 'status') t.assertTrue(r.headers['set-cookie'] ~= nil, "header") @@ -425,7 +428,7 @@ g.test_server_requests = function() status = 200, } end) - local r = http_client.get( + r = http_client.get( 'http://127.0.0.1:12345/check_req_properties?foo=1&bar=2', { headers = { ['X-test-header'] = 'test-value' @@ -475,7 +478,7 @@ g.test_server_requests = function() }) t.assertEquals(r.status, 200, 'status') - local parsed_body = json.decode(r.body) + parsed_body = json.decode(r.body) t.assertEquals(parsed_body.request_line, 'POST /check_req_methods_for_json HTTP/1.1', 'req.request_line') t.assertEquals(parsed_body.read_cached, '{"kind": "json"}', 'json req:read_cached()') t.assertEquals(parsed_body.json, {kind = "json"}, 'req:json()') @@ -491,7 +494,7 @@ g.test_server_requests = function() if is_builtin_test() then router:route({ path = '/post', method = 'POST'}, function(req) - local t = { + return req:render({json = { #req:read("\n"); #req:read(10); #req:read({ size = 10, delimiter = "\n"}); @@ -499,14 +502,13 @@ g.test_server_requests = function() #req:read(); #req:read(); #req:read(); - } - return req:render({json = t}) + }}) end) local bodyf = os.getenv('LUA_SOURCE_DIR') or './' bodyf = io.open(fio.pathjoin(bodyf, 'test/public/lorem.txt')) local body = bodyf:read('*a') bodyf:close() - local r = http_client.post('http://127.0.0.1:12345/post', body) + r = http_client.post('http://127.0.0.1:12345/post', body) t.assertEquals(r.status, 200, 'status') t.assertEquals(json.decode(r.body), { 541,10,10,458,1375,0,0 }, 'req:read() results') @@ -558,7 +560,7 @@ g.test_server_requests = function() body = 'GET *', } end) - local r = http_client.get('http://127.0.0.1:12345/a/b/c') + r = http_client.get('http://127.0.0.1:12345/a/b/c') t.assertEquals(r.status, 200, '/a/b/c request returns 200') t.assertEquals(r.body, 'GET *', 'GET * matches') @@ -568,7 +570,7 @@ g.test_server_requests = function() body = 'ANY /a/:foo/:bar', } end) - local r = http_client.get('http://127.0.0.1:12345/a/b/c') + r = http_client.get('http://127.0.0.1:12345/a/b/c') t.assertEquals(r.status, 200, '/a/b/c request returns 200') t.assertEquals( r.body, From 54d0fca7873155715ab0e3fff20bf443ce13f5c1 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Wed, 23 Oct 2019 13:08:16 +0300 Subject: [PATCH 2/3] Rewrite tap tests to luatest --- test/http_test.lua | 19 +--------- ...iddleware.test.lua => middleware_test.lua} | 35 ++++--------------- 2 files changed, 8 insertions(+), 46 deletions(-) rename test/{middleware.test.lua => middleware_test.lua} (62%) diff --git a/test/http_test.lua b/test/http_test.lua index f2ca9de..d951bcf 100755 --- a/test/http_test.lua +++ b/test/http_test.lua @@ -1,7 +1,6 @@ -#!/usr/bin/env tarantool - local t = require('luatest') local g = t.group('http') + local fio = require('fio') local http_lib = require('http.lib') local http_client = require('http.client') @@ -11,22 +10,6 @@ local http_router = require('http.router') local json = require('json') local urilib = require('uri') --- fix tap and http logs interleaving. --- --- tap module writes to stdout, --- http-server logs to stderr. --- this results in non-synchronized output. --- --- somehow redirecting stdout to stderr doesn't --- remove buffering of tap logs (at least on OSX). --- Monkeypatching to the rescue! - -local orig_iowrite = io.write -package.loaded['io'].write = function(...) - orig_iowrite(...) - io.flush() -end - g.before_all = function() box.cfg{listen = '127.0.0.1:3301'} box.schema.user.grant( diff --git a/test/middleware.test.lua b/test/middleware_test.lua similarity index 62% rename from test/middleware.test.lua rename to test/middleware_test.lua index ad9c5c1..f120e27 100755 --- a/test/middleware.test.lua +++ b/test/middleware_test.lua @@ -1,30 +1,9 @@ -#!/usr/bin/env tarantool +local t = require('luatest') +local g = t.group('middleware') -local tap = require('tap') local middleware_module = require('http.router.middleware') --- fix tap and http logs interleaving. --- --- tap module writes to stdout, --- http-server logs to stderr. --- this results in non-synchronized output. --- --- somehow redirecting stdout to stderr doesn't --- remove buffering of tap logs (at least on OSX). --- Monkeypatching to the rescue! - -local orig_iowrite = io.write -package.loaded['io'].write = function(...) - orig_iowrite(...) - io.flush() -end - -local test = tap.test("http") -test:plan(1) - -test:test("ordering", function(test) - test:plan(7) - +g.test_ordering = function() local middleware = middleware_module.new() local add = function(opts, add_opts) @@ -36,17 +15,17 @@ test:test("ordering", function(test) end local ok = middleware:use(opts) - test:is(ok, should_be_ok, msg) + t.assert_equals(ok, should_be_ok, msg) end local ensure_before = function(mwname1, mwname2) local msg = ('%s must be ordered before %s'):format(mwname1, mwname2) for _, mw in ipairs(middleware:ordered()) do if mw.name == mwname1 then - test:ok(true, msg) + -- test:ok(true, msg) return elseif mw.name == mwname2 then - test:fail(msg) + t.fail(msg) return end end @@ -78,4 +57,4 @@ test:test("ordering", function(test) }) middleware:clear() -end) +end From 1df02966f10a5af3d52dfb6fb86b659cb4489f04 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Wed, 23 Oct 2019 13:18:10 +0300 Subject: [PATCH 3/3] Update luatest to 0.3.0 --- .travis.yml | 2 +- test/http_test.lua | 269 ++++++++++++++++++++++----------------------- 2 files changed, 132 insertions(+), 139 deletions(-) diff --git a/.travis.yml b/.travis.yml index befe9df..688f659 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ _test: &test - sudo apt-get install -y tarantool tarantool-dev - tarantoolctl rocks make - tarantoolctl rocks install luacheck 0.25.0 - - tarantoolctl rocks install luatest 0.2.2 + - tarantoolctl rocks install luatest 0.3.0 script: - .rocks/bin/luacheck . - .rocks/bin/luatest diff --git a/test/http_test.lua b/test/http_test.lua index d951bcf..bf98ee5 100755 --- a/test/http_test.lua +++ b/test/http_test.lua @@ -24,11 +24,11 @@ g.test_split_uri = function() if lhs.query == '' then lhs.query = nil end - t.assertEquals(lhs.scheme, rhs.scheme, uri.." scheme", extra) - t.assertEquals(lhs.host, rhs.host, uri.." host", extra) - t.assertEquals(lhs.service, rhs.service, uri.." service", extra) - t.assertEquals(lhs.path, rhs.path, uri.." path", extra) - t.assertEquals(lhs.query, rhs.query, uri.." query", extra) + t.assert_equals(lhs.scheme, rhs.scheme, uri.." scheme", extra) + t.assert_equals(lhs.host, rhs.host, uri.." host", extra) + t.assert_equals(lhs.service, rhs.service, uri.." service", extra) + t.assert_equals(lhs.path, rhs.path, uri.." path", extra) + t.assert_equals(lhs.query, rhs.query, uri.." query", extra) end check('http://abc', { scheme = 'http', host = 'abc'}) check('http://abc/', { scheme = 'http', host = 'abc', path ='/'}) @@ -54,15 +54,15 @@ g.test_split_uri = function() end g.test_template = function() - t.assertEquals(http_lib.template("<% for i = 1, cnt do %> <%= abc %> <% end %>", + t.assert_equals(http_lib.template("<% for i = 1, cnt do %> <%= abc %> <% end %>", {abc = '1 <3>&" ', cnt = 3}), ' 1 <3>&" 1 <3>&" 1 <3>&" ', "tmpl1") - t.assertEquals(http_lib.template("<% for i = 1, cnt do %> <%= ab %> <% end %>", + t.assert_equals(http_lib.template("<% for i = 1, cnt do %> <%= ab %> <% end %>", {abc = '1 <3>&" ', cnt = 3}), ' nil nil nil ', "tmpl2") local r, msg = pcall(http_lib.template, "<% ab() %>", {ab = '1'}) - t.assertTrue(r == false and msg:match("call local 'ab'") ~= nil, "bad template") + t.assert(r == false and msg:match("call local 'ab'") ~= nil, "bad template") -- gh-18: rendered tempate is truncated local template = [[ @@ -86,38 +86,38 @@ g.test_template = function() end local rendered = http_lib.template(template, { t = tt }) - t.assertTrue(#rendered > 10000, "rendered size") - t.assertEquals(rendered:sub(#rendered - 7, #rendered - 1), "", "rendered eof") + t.assert(#rendered > 10000, "rendered size") + t.assert_equals(rendered:sub(#rendered - 7, #rendered - 1), "", "rendered eof") end g.test_parse_request = function() - t.assertEquals(http_lib._parse_request('abc'), + t.assert_equals(http_lib._parse_request('abc'), { error = 'Broken request line', headers = {} }, 'broken request') - t.assertEquals( + t.assert_equals( http_lib._parse_request("GET / HTTP/1.1\nHost: s.com\r\n\r\n").path, '/', 'path' ) - t.assertEquals( + t.assert_equals( http_lib._parse_request("GET / HTTP/1.1\nHost: s.com\r\n\r\n").proto, {1,1}, 'proto' ) - t.assertEquals( + t.assert_equals( http_lib._parse_request("GET / HTTP/1.1\nHost: s.com\r\n\r\n").headers, {host = 's.com'}, 'host' ) - t.assertEquals( + t.assert_equals( http_lib._parse_request("GET / HTTP/1.1\nHost: s.com\r\n\r\n").method, 'GET', 'method' ) - t.assertEquals( + t.assert_equals( http_lib._parse_request("GET / HTTP/1.1\nHost: s.com\r\n\r\n").query, '', 'query' @@ -125,12 +125,12 @@ g.test_parse_request = function() end g.test_params = function() - t.assertEquals(http_lib.params(), {}, 'nil string') - t.assertEquals(http_lib.params(''), {}, 'empty string') - t.assertEquals(http_lib.params('a'), {a = ''}, 'separate literal') - t.assertEquals(http_lib.params('a=b'), {a = 'b'}, 'one variable') - t.assertEquals(http_lib.params('a=b&b=cde'), {a = 'b', b = 'cde'}, 'some') - t.assertEquals(http_lib.params('a=b&b=cde&a=1'), + t.assert_equals(http_lib.params(), {}, 'nil string') + t.assert_equals(http_lib.params(''), {}, 'empty string') + t.assert_equals(http_lib.params('a'), {a = ''}, 'separate literal') + t.assert_equals(http_lib.params('a=b'), {a = 'b'}, 'one variable') + t.assert_equals(http_lib.params('a=b&b=cde'), {a = 'b', b = 'cde'}, 'some') + t.assert_equals(http_lib.params('a=b&b=cde&a=1'), {a = { 'b', '1' }, b = 'cde'}, 'array') end @@ -193,48 +193,48 @@ end g.test_server_url_match = function() local httpd, router = cfgserv() - t.assertIsTable(httpd, "httpd object") - t.assertIsNil(router:match('GET', '/')) - t.assertEquals(router:match('GET', '/abc').endpoint.path, "/abc", "/abc") - t.assertEquals(#router:match('GET', '/abc').stash, 0, "/abc") - t.assertEquals(router:match('GET', '/abc/123').endpoint.path, "/abc/:cde", "/abc/123") - t.assertEquals(router:match('GET', '/abc/123').stash.cde, "123", "/abc/123") - t.assertEquals(router:match('GET', '/abc/123/122').endpoint.path, "/abc/:cde/:def", + t.assert_type(httpd, 'table') + t.assert_is(router:match('GET', '/'), nil) + t.assert_equals(router:match('GET', '/abc').endpoint.path, "/abc", "/abc") + t.assert_equals(#router:match('GET', '/abc').stash, 0, "/abc") + t.assert_equals(router:match('GET', '/abc/123').endpoint.path, "/abc/:cde", "/abc/123") + t.assert_equals(router:match('GET', '/abc/123').stash.cde, "123", "/abc/123") + t.assert_equals(router:match('GET', '/abc/123/122').endpoint.path, "/abc/:cde/:def", "/abc/123/122") - t.assertEquals(router:match('GET', '/abc/123/122').stash.def, "122", + t.assert_equals(router:match('GET', '/abc/123/122').stash.def, "122", "/abc/123/122") - t.assertEquals(router:match('GET', '/abc/123/122').stash.cde, "123", + t.assert_equals(router:match('GET', '/abc/123/122').stash.cde, "123", "/abc/123/122") - t.assertEquals(router:match('GET', '/abc_123-122').endpoint.path, "/abc_:cde_def", + t.assert_equals(router:match('GET', '/abc_123-122').endpoint.path, "/abc_:cde_def", "/abc_123-122") - t.assertEquals(router:match('GET', '/abc_123-122').stash.cde_def, "123-122", + t.assert_equals(router:match('GET', '/abc_123-122').stash.cde_def, "123-122", "/abc_123-122") - t.assertEquals(router:match('GET', '/abc-123-def').endpoint.path, "/abc-:cde-def", + t.assert_equals(router:match('GET', '/abc-123-def').endpoint.path, "/abc-:cde-def", "/abc-123-def") - t.assertEquals(router:match('GET', '/abc-123-def').stash.cde, "123", + t.assert_equals(router:match('GET', '/abc-123-def').stash.cde, "123", "/abc-123-def") - t.assertEquals(router:match('GET', '/aba-123-dea/1/2/3').endpoint.path, + t.assert_equals(router:match('GET', '/aba-123-dea/1/2/3').endpoint.path, "/aba*def", '/aba-123-dea/1/2/3') - t.assertEquals(router:match('GET', '/aba-123-dea/1/2/3').stash.def, + t.assert_equals(router:match('GET', '/aba-123-dea/1/2/3').stash.def, "-123-dea/1/2/3", '/aba-123-dea/1/2/3') - t.assertEquals(router:match('GET', '/abb-123-dea/1/2/3/cde').endpoint.path, + t.assert_equals(router:match('GET', '/abb-123-dea/1/2/3/cde').endpoint.path, "/abb*def/cde", '/abb-123-dea/1/2/3/cde') - t.assertEquals(router:match('GET', '/abb-123-dea/1/2/3/cde').stash.def, + t.assert_equals(router:match('GET', '/abb-123-dea/1/2/3/cde').stash.def, "-123-dea/1/2/3", '/abb-123-dea/1/2/3/cde') - t.assertEquals(router:match('GET', '/banners/1wulc.z8kiy.6p5e3').stash.token, + t.assert_equals(router:match('GET', '/banners/1wulc.z8kiy.6p5e3').stash.token, '1wulc.z8kiy.6p5e3', "stash with dots") end g.test_server_url_for = function() local _, router = cfgserv() - t.assertEquals(router:url_for('abcdef'), '/abcdef', '/abcdef') - t.assertEquals(router:url_for('test'), '/abc//', '/abc//') - t.assertEquals(router:url_for('test', { cde = 'cde_v', def = 'def_v' }), + t.assert_equals(router:url_for('abcdef'), '/abcdef', '/abcdef') + t.assert_equals(router:url_for('test'), '/abc//', '/abc//') + t.assert_equals(router:url_for('test', { cde = 'cde_v', def = 'def_v' }), '/abc/cde_v/def_v', '/abc/cde_v/def_v') - t.assertEquals(router:url_for('star', { def = '/def_v' }), + t.assert_equals(router:url_for('star', { def = '/def_v' }), '/abb/def_v/cde', '/abb/def_v/cde') - t.assertEquals(router:url_for('star', { def = '/def_v' }, { a = 'b', c = 'd' }), + t.assert_equals(router:url_for('star', { def = '/def_v' }, { a = 'b', c = 'd' }), '/abb/def_v/cde?a=b&c=d', '/abb/def_v/cde?a=b&c=d') end @@ -243,80 +243,80 @@ g.test_server_requests = function() httpd:start() local r = http_client.get('http://127.0.0.1:12345/test') - t.assertEquals(r.status, 200, '/test code') + t.assert_equals(r.status, 200, '/test code') - t.assertEquals(r.proto[1], 1, '/test http 1.1') - t.assertEquals(r.proto[2], 1, '/test http 1.1') - t.assertEquals(r.reason, 'Ok', '/test reason') - t.assertEquals(string.match(r.body, 'title: 123'), 'title: 123', '/test body') + t.assert_equals(r.proto[1], 1, '/test http 1.1') + t.assert_equals(r.proto[2], 1, '/test http 1.1') + t.assert_equals(r.reason, 'Ok', '/test reason') + t.assert_equals(string.match(r.body, 'title: 123'), 'title: 123', '/test body') r = http_client.get('http://127.0.0.1:12345/test404') - t.assertEquals(r.status, 404, '/test404 code') + t.assert_equals(r.status, 404, '/test404 code') -- broken in built-in tarantool/http - --t.assertEquals(r.reason, 'Not found', '/test404 reason') + --t.assert_equals(r.reason, 'Not found', '/test404 reason') r = http_client.get('http://127.0.0.1:12345/absent') - t.assertEquals(r.status, 500, '/absent code') - --t.assertEquals(r.reason, 'Internal server error', '/absent reason') - t.assertEquals(string.match(r.body, 'load module'), 'load module', '/absent body') + t.assert_equals(r.status, 500, '/absent code') + --t.assert_equals(r.reason, 'Internal server error', '/absent reason') + t.assert_equals(string.match(r.body, 'load module'), 'load module', '/absent body') r = http_client.get('http://127.0.0.1:12345/ctxaction') - t.assertEquals(r.status, 200, '/ctxaction code') - t.assertEquals(r.reason, 'Ok', '/ctxaction reason') - t.assertEquals(string.match(r.body, 'Hello, Tarantool'), 'Hello, Tarantool', + t.assert_equals(r.status, 200, '/ctxaction code') + t.assert_equals(r.reason, 'Ok', '/ctxaction reason') + t.assert_equals(string.match(r.body, 'Hello, Tarantool'), 'Hello, Tarantool', '/ctxaction body') - t.assertEquals(string.match(r.body, 'action: action'), 'action: action', + t.assert_equals(string.match(r.body, 'action: action'), 'action: action', '/ctxaction body action') - t.assertEquals(string.match(r.body, 'controller: module[.]controller'), + t.assert_equals(string.match(r.body, 'controller: module[.]controller'), 'controller: module.controller', '/ctxaction body controller') r = http_client.get('http://127.0.0.1:12345/ctxaction.invalid') - t.assertEquals(r.status, 404, '/ctxaction.invalid code') -- WTF? - --t.assertEquals(r.reason, 'Not found', '/ctxaction.invalid reason') - --t.assertEquals(r.body, '', '/ctxaction.invalid body') + t.assert_equals(r.status, 404, '/ctxaction.invalid code') -- WTF? + --t.assert_equals(r.reason, 'Not found', '/ctxaction.invalid reason') + --t.assert_equals(r.body, '', '/ctxaction.invalid body') r = http_client.get('http://127.0.0.1:12345/hello.html') - t.assertEquals(r.status, 200, '/hello.html code') - t.assertEquals(r.reason, 'Ok', '/hello.html reason') - t.assertEquals(string.match(r.body, 'static html'), 'static html', + t.assert_equals(r.status, 200, '/hello.html code') + t.assert_equals(r.reason, 'Ok', '/hello.html reason') + t.assert_equals(string.match(r.body, 'static html'), 'static html', '/hello.html body') r = http_client.get('http://127.0.0.1:12345/absentaction') - t.assertEquals(r.status, 500, '/absentaction 500') - --t.assertEquals(r.reason, 'Internal server error', '/absentaction reason') - t.assertEquals(string.match(r.body, 'contain function'), 'contain function', + t.assert_equals(r.status, 500, '/absentaction 500') + --t.assert_equals(r.reason, 'Internal server error', '/absentaction reason') + t.assert_equals(string.match(r.body, 'contain function'), 'contain function', '/absentaction body') r = http_client.get('http://127.0.0.1:12345/helper') - t.assertEquals(r.status, 200, 'helper 200') - t.assertEquals(r.reason, 'Ok', 'helper reason') - t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') + t.assert_equals(r.status, 200, 'helper 200') + t.assert_equals(r.reason, 'Ok', 'helper reason') + t.assert_equals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') r = http_client.get('http://127.0.0.1:12345/helper?abc') - t.assertEquals(r.status, 200, 'helper?abc 200') - t.assertEquals(r.reason, 'Ok', 'helper?abc reason') - t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') + t.assert_equals(r.status, 200, 'helper?abc 200') + t.assert_equals(r.reason, 'Ok', 'helper?abc reason') + t.assert_equals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body') router:route({path = '/die', file = 'helper.html.el'}, function() error(123) end ) r = http_client.get('http://127.0.0.1:12345/die') - t.assertEquals(r.status, 500, 'die 500') - --t.assertEquals(r.reason, 'Internal server error', 'die reason') + t.assert_equals(r.status, 500, 'die 500') + --t.assert_equals(r.reason, 'Internal server error', 'die reason') router:route({ path = '/info' }, function(cx) return cx:render({ json = cx:peer() }) end) r = json.decode(http_client.get('http://127.0.0.1:12345/info').body) - t.assertEquals(r.host, '127.0.0.1', 'peer.host') - t.assertIsNumber(r.port, 'peer.port') + t.assert_equals(r.host, '127.0.0.1', 'peer.host') + t.assert_type(r.port, 'number') r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'}, function(tx) return tx:render({text = 'POST = ' .. tx:read()}) end) - t.assertIsTable(r, ':route') + t.assert_type(r, 'table') -- GET/POST at one route @@ -324,38 +324,38 @@ g.test_server_requests = function() function(tx) return tx:render({text = 'POST = ' .. tx:read()}) end) - t.assertIsTable(r, 'add POST method') + t.assert_type(r, 'table') r = router:route({method = 'GET', path = '/dit', file = 'helper.html.el'}, function(tx) return tx:render({text = 'GET = ' .. tx:read()}) end ) - t.assertIsTable(r, 'add GET method') + t.assert_type(r, 'table') r = router:route({method = 'DELETE', path = '/dit', file = 'helper.html.el'}, function(tx) return tx:render({text = 'DELETE = ' .. tx:read()}) end ) - t.assertIsTable(r, 'add DELETE method') + t.assert_type(r, 'table') r = router:route({method = 'PATCH', path = '/dit', file = 'helper.html.el'}, function(tx) return tx:render({text = 'PATCH = ' .. tx:read()}) end ) - t.assertIsTable(r, 'add PATCH method') + t.assert_type(r, 'table') -- TODO r = http_client.request('POST', 'http://127.0.0.1:12345/dit', 'test') - t.assertEquals(r.body, 'POST = test', 'POST reply') + t.assert_equals(r.body, 'POST = test', 'POST reply') r = http_client.request('GET', 'http://127.0.0.1:12345/dit') - t.assertEquals(r.body, 'GET = ', 'GET reply') + t.assert_equals(r.body, 'GET = ', 'GET reply') r = http_client.request('DELETE', 'http://127.0.0.1:12345/dit', 'test1') - t.assertEquals(r.body, 'DELETE = test1', 'DELETE reply') + t.assert_equals(r.body, 'DELETE = test1', 'DELETE reply') r = http_client.request('PATCH', 'http://127.0.0.1:12345/dit', 'test2') - t.assertEquals(r.body, 'PATCH = test2', 'PATCH reply') + t.assert_equals(r.body, 'PATCH = test2', 'PATCH reply') router:route({path = '/chunked'}, function(self) return self:iterate(ipairs({'chunked', 'encoding', 't\r\nest'})) @@ -363,9 +363,9 @@ g.test_server_requests = function() -- http client currently doesn't support chunked encoding r = http_client.get('http://127.0.0.1:12345/chunked') - t.assertEquals(r.status, 200, 'chunked 200') - t.assertEquals(r.headers['transfer-encoding'], 'chunked', 'chunked headers') - t.assertEquals(r.body, 'chunkedencodingt\r\nest', 'chunked body') + t.assert_equals(r.status, 200, 'chunked 200') + t.assert_equals(r.headers['transfer-encoding'], 'chunked', 'chunked headers') + t.assert_equals(r.body, 'chunkedencodingt\r\nest', 'chunked body') -- get cookie router:route({path = '/receive_cookie'}, function(req) @@ -380,8 +380,8 @@ g.test_server_requests = function() cookie = 'foo=bar; baz=feez', } }) - t.assertEquals(r.status, 200, 'status') - t.assertEquals(r.body, 'foo=bar; baz=feez', 'body') + t.assert_equals(r.status, 200, 'status') + t.assert_equals(r.body, 'foo=bar; baz=feez', 'body') -- cookie router:route({path = '/cookie'}, function(req) @@ -392,8 +392,8 @@ g.test_server_requests = function() return resp end) r = http_client.get('http://127.0.0.1:12345/cookie') - t.assertEquals(r.status, 200, 'status') - t.assertTrue(r.headers['set-cookie'] ~= nil, "header") + t.assert_equals(r.status, 200, 'status') + t.assert(r.headers['set-cookie'] ~= nil, "header") -- request object with GET method @@ -417,15 +417,15 @@ g.test_server_requests = function() ['X-test-header'] = 'test-value' } }) - t.assertEquals(r.status, 200, 'status') + t.assert_equals(r.status, 200, 'status') local parsed_body = json.decode(r.body) - t.assertEquals(parsed_body.headers['x-test-header'], 'test-value', 'req.headers') - t.assertEquals(parsed_body.method, 'GET', 'req.method') - t.assertEquals(parsed_body.path, '/check_req_properties', 'req.path') - t.assertEquals(parsed_body.query, 'foo=1&bar=2', 'req.query') - t.assertEquals(parsed_body.query_param_bar, '2', 'req:query_param()') - t.assertEquals(parsed_body.proto, {1, 1}, 'req.proto') + t.assert_equals(parsed_body.headers['x-test-header'], 'test-value', 'req.headers') + t.assert_equals(parsed_body.method, 'GET', 'req.method') + t.assert_equals(parsed_body.path, '/check_req_properties', 'req.path') + t.assert_equals(parsed_body.query, 'foo=1&bar=2', 'req.query') + t.assert_equals(parsed_body.query_param_bar, '2', 'req:query_param()') + t.assert_equals(parsed_body.proto, {1, 1}, 'req.proto') -- request object methods router:route({path = '/check_req_methods_for_json', method = 'POST'}, function(req) @@ -459,21 +459,21 @@ g.test_server_requests = function() ['X-test-header'] = 'test-value' } }) - t.assertEquals(r.status, 200, 'status') + t.assert_equals(r.status, 200, 'status') parsed_body = json.decode(r.body) - t.assertEquals(parsed_body.request_line, 'POST /check_req_methods_for_json HTTP/1.1', 'req.request_line') - t.assertEquals(parsed_body.read_cached, '{"kind": "json"}', 'json req:read_cached()') - t.assertEquals(parsed_body.json, {kind = "json"}, 'req:json()') - t.assertEquals(parsed_body.post_param_for_kind, "json", 'req:post_param()') + t.assert_equals(parsed_body.request_line, 'POST /check_req_methods_for_json HTTP/1.1', 'req.request_line') + t.assert_equals(parsed_body.read_cached, '{"kind": "json"}', 'json req:read_cached()') + t.assert_equals(parsed_body.json, {kind = "json"}, 'req:json()') + t.assert_equals(parsed_body.post_param_for_kind, "json", 'req:post_param()') r = http_client.post( 'http://127.0.0.1:12345/check_req_methods', 'hello mister' ) - t.assertEquals(r.status, 200, 'status') + t.assert_equals(r.status, 200, 'status') parsed_body = json.decode(r.body) - t.assertEquals(parsed_body.read_cached, 'hello mister', 'non-json req:read_cached()') + t.assert_equals(parsed_body.read_cached, 'hello mister', 'non-json req:read_cached()') if is_builtin_test() then router:route({ path = '/post', method = 'POST'}, function(req) @@ -492,11 +492,9 @@ g.test_server_requests = function() local body = bodyf:read('*a') bodyf:close() r = http_client.post('http://127.0.0.1:12345/post', body) - t.assertEquals(r.status, 200, 'status') - t.assertEquals(json.decode(r.body), { 541,10,10,458,1375,0,0 }, + t.assert_equals(r.status, 200, 'status') + t.assert_equals(json.decode(r.body), { 541,10,10,458,1375,0,0 }, 'req:read() results') - else - t.assertTrue(true, 'post body - ignore on NGINX') end -- hijacking @@ -518,22 +516,17 @@ g.test_server_requests = function() -- 1. set-up socket local socket = require('socket') local sock = socket.tcp_connect('127.0.0.1', 12345) - t.assertTrue(sock ~= nil, 'HTTP client connection established') + t.assert(sock ~= nil, 'HTTP client connection established') -- 2. over raw-socket send HTTP POST (to get it routed to route) local upgrade_request = 'POST /upgrade HTTP/1.1\r\nConnection: upgrade\r\n\r\n' local bytessent = sock:write(upgrade_request) - t.assertEquals(bytessent, #upgrade_request, 'upgrade request sent fully') + t.assert_equals(bytessent, #upgrade_request, 'upgrade request sent fully') -- 3. send ping, receive pong - t.assertEquals(sock:read(5), 'ready', 'server is ready') + t.assert_equals(sock:read(5), 'ready', 'server is ready') sock:write('ping') - t.assertEquals(sock:read(4), 'pong', 'pong receieved') - else - t.assertTrue(true, 'HTTP client connection established - ignored on NGINX') - t.assertTrue(true, 'upgrade request sent fully - ignored on NGINX') - t.assertTrue(true, 'server is ready - ignored on NGINX') - t.assertTrue(true, 'pong received - ignored on NGINX') + t.assert_equals(sock:read(4), 'pong', 'pong receieved') end -- prioritization of more specific routes @@ -544,8 +537,8 @@ g.test_server_requests = function() } end) r = http_client.get('http://127.0.0.1:12345/a/b/c') - t.assertEquals(r.status, 200, '/a/b/c request returns 200') - t.assertEquals(r.body, 'GET *', 'GET * matches') + t.assert_equals(r.status, 200, '/a/b/c request returns 200') + t.assert_equals(r.body, 'GET *', 'GET * matches') router:route({method = 'ANY', path = '/a/:foo/:bar'}, function(_) return { @@ -554,8 +547,8 @@ g.test_server_requests = function() } end) r = http_client.get('http://127.0.0.1:12345/a/b/c') - t.assertEquals(r.status, 200, '/a/b/c request returns 200') - t.assertEquals( + t.assert_equals(r.status, 200, '/a/b/c request returns 200') + t.assert_equals( r.body, 'ANY /a/:foo/:bar', '# of stashes matched doesnt matter - only # of known symbols by the route matters' @@ -592,10 +585,10 @@ g.test_middleware = function() path = '/.*', method = {'GET', 'POST'}, }) - t.assertTrue(ok, 'hello_world middleware added successfully') + t.assert(ok, 'hello_world middleware added successfully') local middlewares_ordered = router.middleware:ordered() - t.assertEquals(#middlewares_ordered, 1, 'one middleware is registered') + t.assert_equals(#middlewares_ordered, 1, 'one middleware is registered') ok = router:use(add_helloworld_before_to_response, { name = 'hello_world_before', @@ -603,13 +596,13 @@ g.test_middleware = function() method = 'ANY', before = 'hello_world', }) - t.assertTrue(ok, 'hello_world_before middleware added successfully') + t.assert(ok, 'hello_world_before middleware added successfully') middlewares_ordered = router.middleware:ordered() - t.assertEquals(#middlewares_ordered, 2, 'both middlewares are registered') - t.assertEquals(middlewares_ordered[1].name, 'hello_world_before', + t.assert_equals(#middlewares_ordered, 2, 'both middlewares are registered') + t.assert_equals(middlewares_ordered[1].name, 'hello_world_before', 'hello_world_before is first') - t.assertEquals(middlewares_ordered[2].name, 'hello_world', + t.assert_equals(middlewares_ordered[2].name, 'hello_world', 'hello_world is last') local apple_handler = function() @@ -640,11 +633,11 @@ g.test_middleware = function() local r = http_client.get( 'http://127.0.0.1:12345/fruits/apple' ) - t.assertEquals(r.status, 200, 'status') + t.assert_equals(r.status, 200, 'status') require('log').info('DEBUG: /fruits/apple response: %s', r.body) local parsed_body = json.decode(r.body) - t.assertEquals(parsed_body.kind, 'apple', 'body is correct') - t.assertEquals(parsed_body.message, 'hello world! (before)', 'hello_world middleware invoked last') + t.assert_equals(parsed_body.kind, 'apple', 'body is correct') + t.assert_equals(parsed_body.message, 'hello world! (before)', 'hello_world middleware invoked last') local function swap_orange_and_apple(req) local path_info = req['PATH_INFO'] @@ -664,14 +657,14 @@ g.test_middleware = function() preroute = true, name = 'swap_orange_and_apple', }) - t.assertTrue(ok, 'swap_orange_and_apple middleware added successfully') + t.assert(ok, 'swap_orange_and_apple middleware added successfully') r = http_client.get( 'http://127.0.0.1:12345/fruits/apple' ) - t.assertEquals(r.status, 200, 'status') + t.assert_equals(r.status, 200, 'status') parsed_body = json.decode(r.body) - t.assertEquals(parsed_body.kind, 'orange', 'route swapped from apple handler to orange') + t.assert_equals(parsed_body.kind, 'orange', 'route swapped from apple handler to orange') httpd:stop() end