|
| 1 | +local fio = require('fio') |
| 2 | +local http_server = require('http.server') |
| 3 | +local http_client = require('http.client') |
| 4 | + |
| 5 | +local helpers = table.copy(require('luatest').helpers) |
| 6 | + |
| 7 | +helpers.base_port = 12345 |
| 8 | +helpers.base_host = '127.0.0.1' |
| 9 | +helpers.base_uri = ('http://%s:%s'):format(helpers.base_host, helpers.base_port) |
| 10 | + |
| 11 | +helpers.cfgserv = function() |
| 12 | + local path = os.getenv('LUA_SOURCE_DIR') or './' |
| 13 | + path = fio.pathjoin(path, 'test') |
| 14 | + |
| 15 | + local httpd = http_server.new(helpers.base_host, helpers.base_port, { |
| 16 | + app_dir = path, |
| 17 | + log_requests = false, |
| 18 | + log_errors = false |
| 19 | + }) |
| 20 | + :route({path = '/abc/:cde/:def', name = 'test'}, function() end) |
| 21 | + :route({path = '/abc'}, function() end) |
| 22 | + :route({path = '/ctxaction'}, 'module.controller#action') |
| 23 | + :route({path = '/absentaction'}, 'module.controller#absent') |
| 24 | + :route({path = '/absent'}, 'module.absent#action') |
| 25 | + :route({path = '/abc/:cde'}, function() end) |
| 26 | + :route({path = '/abc_:cde_def'}, function() end) |
| 27 | + :route({path = '/abc-:cde-def'}, function() end) |
| 28 | + :route({path = '/aba*def'}, function() end) |
| 29 | + :route({path = '/abb*def/cde', name = 'star'}, function() end) |
| 30 | + :route({path = '/banners/:token'}) |
| 31 | + :helper('helper_title', function(self, a) return 'Hello, ' .. a end) |
| 32 | + :route({path = '/helper', file = 'helper.html.el'}) |
| 33 | + :route({path = '/test', file = 'test.html.el' }, |
| 34 | + function(cx) return cx:render({ title = 'title: 123' }) end) |
| 35 | + |
| 36 | + return httpd |
| 37 | +end |
| 38 | + |
| 39 | +local log_queue = {} |
| 40 | + |
| 41 | +helpers.clear_log_queue = function() |
| 42 | + log_queue = {} |
| 43 | +end |
| 44 | + |
| 45 | +helpers.custom_logger = { |
| 46 | + debug = function() end, |
| 47 | + verbose = function() |
| 48 | + table.insert(log_queue, { |
| 49 | + log_lvl = 'verbose', |
| 50 | + }) |
| 51 | + end, |
| 52 | + info = function(...) |
| 53 | + table.insert(log_queue, { |
| 54 | + log_lvl = 'info', |
| 55 | + msg = string.format(...) |
| 56 | + }) |
| 57 | + end, |
| 58 | + warn = function(...) |
| 59 | + table.insert(log_queue, { |
| 60 | + log_lvl = 'warn', |
| 61 | + msg = string.format(...) |
| 62 | + }) |
| 63 | + end, |
| 64 | + error = function(...) |
| 65 | + table.insert(log_queue, { |
| 66 | + log_lvl = 'error', |
| 67 | + msg = string.format(...) |
| 68 | + }) |
| 69 | + end |
| 70 | +} |
| 71 | + |
| 72 | +helpers.find_msg_in_log_queue = function(msg, strict) |
| 73 | + for _, log in ipairs(log_queue) do |
| 74 | + if not strict then |
| 75 | + if log.msg:match(msg) then |
| 76 | + return log |
| 77 | + end |
| 78 | + else |
| 79 | + if log.msg == msg then |
| 80 | + return log |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +helpers.teardown = function(httpd) |
| 87 | + httpd:stop() |
| 88 | + helpers.retrying({}, function() |
| 89 | + local r = http_client.request('GET', helpers.base_uri) |
| 90 | + return r == nil |
| 91 | + end) |
| 92 | +end |
| 93 | + |
| 94 | +return helpers |
0 commit comments