diff --git a/.travis.yml b/.travis.yml index 688f659..47f0f87 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,10 +24,10 @@ _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.3.0 + - tarantoolctl rocks install luatest 0.4.0 script: - .rocks/bin/luacheck . - - .rocks/bin/luatest + - .rocks/bin/luatest -v --shuffle all _deploy: &deploy provider: packagecloud diff --git a/test/helper.lua b/test/helper.lua new file mode 100644 index 0000000..521ea51 --- /dev/null +++ b/test/helper.lua @@ -0,0 +1,77 @@ +local fio = require('fio') +local t = require('luatest') +local http_server = require('http.server') +local http_router = require('http.router') +local ngx_server = require('http.nginx_server') + +local helper = {} + +helper.base_port = 12345 +helper.base_host = '127.0.0.1' +helper.base_uri = ('http://%s:%s/'):format(helper.base_host, helper.base_port) + +helper.is_nginx_test = function () + local server_type = os.getenv('SERVER_TYPE') or 'builtin' + return server_type:lower() == 'nginx' +end + +helper.is_builtin_test = function () + return not helper.is_nginx_test() +end + +local function choose_server() + local log_requests = true + local log_errors = true + + if helper.is_nginx_test() then + -- host and port are for SERVER_NAME, SERVER_PORT only. + -- TODO: are they required? + + return ngx_server.new({ + host = helper.base_host, + port = helper.base_port, + tnt_method = 'nginx_entrypoint', + log_requests = log_requests, + log_errors = log_errors, + }) + end + + return http_server.new(helper.base_host, helper.base_port, { + log_requests = log_requests, + log_errors = log_errors + }) +end + +helper.cfgserv = function () + local path = os.getenv('LUA_SOURCE_DIR') or './' + path = fio.pathjoin(path, 'test') + + local httpd = choose_server() + local router = http_router.new({app_dir = path}) + :route({path = '/abc/:cde/:def', name = 'test'}, function() end) + :route({path = '/abc'}, function() end) + :route({path = '/ctxaction'}, 'module.controller#action') + :route({path = '/absentaction'}, 'module.controller#absent') + :route({path = '/absent'}, 'module.absent#action') + :route({path = '/abc/:cde'}, function() end) + :route({path = '/abc_:cde_def'}, function() end) + :route({path = '/abc-:cde-def'}, function() end) + :route({path = '/aba*def'}, function() end) + :route({path = '/abb*def/cde', name = 'star'}, function() end) + :route({path = '/banners/:token'}) + :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) + httpd:set_router(router) + return httpd, router +end + +t.before_suite(function() + box.cfg{listen = '127.0.0.1:3301'} + box.schema.user.grant( + 'guest', 'read,write,execute', 'universe', nil, {if_not_exists = true} + ) +end) + +return helper diff --git a/test/http_test.lua b/test/http_test.lua deleted file mode 100755 index ce7fb77..0000000 --- a/test/http_test.lua +++ /dev/null @@ -1,682 +0,0 @@ -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') -local http_server = require('http.server') -local ngx_server = require('http.nginx_server') -local http_router = require('http.router') -local json = require('json') -local urilib = require('uri') - -g.before_all = function() - box.cfg{listen = '127.0.0.1:3301'} - box.schema.user.grant( - 'guest', 'read,write,execute', 'universe', nil, {if_not_exists = true} - ) -end - -g.test_split_uri = function() - local function check(uri, rhs) - local lhs = urilib.parse(uri) - local extra = { lhs = lhs, rhs = rhs } - if lhs.query == '' then - lhs.query = nil - end - 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 ='/'}) - check('http://abc?', { scheme = 'http', host = 'abc'}) - check('http://abc/?', { scheme = 'http', host = 'abc', path ='/'}) - check('http://abc/?', { scheme = 'http', host = 'abc', path ='/'}) - check('http://abc:123', { scheme = 'http', host = 'abc', service = '123' }) - 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('google.com', { host = 'google.com'}) - check('google.com?query', { host = 'google.com', query = 'query'}) - check('google.com/abc?query', { host = 'google.com', path = '/abc', - query = 'query'}) - check('https://google.com:443/abc?query', { scheme = 'https', - host = 'google.com', service = '443', path = '/abc', query = 'query'}) -end - -g.test_template = function() - 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.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.assert(r == false and msg:match("call local 'ab'") ~= nil, "bad template") - - -- gh-18: rendered tempate is truncated - local template = [[ - -
-| <%= i %> | -<%= v %> | -