diff options
author | Mikhail Galanin <mgalanin@mimecast.com> | 2018-08-30 16:51:55 +0100 |
---|---|---|
committer | Mikhail Galanin <mgalanin@mimecast.com> | 2018-08-30 16:51:55 +0100 |
commit | b4d4cff66970d51f4f82a37e03de2db07291742e (patch) | |
tree | d425b58e1f22a7c54a21016ba1d33ed37cd9ef5a /test/functional/lua | |
parent | a78803aeb558c0ebb9ada2a0f71f960ac31f373d (diff) | |
download | rspamd-b4d4cff66970d51f4f82a37e03de2db07291742e.tar.gz rspamd-b4d4cff66970d51f4f82a37e03de2db07291742e.zip |
[Test] Test for TCP library
Diffstat (limited to 'test/functional/lua')
-rw-r--r-- | test/functional/lua/http.lua | 3 | ||||
-rw-r--r-- | test/functional/lua/tcp.lua | 170 |
2 files changed, 173 insertions, 0 deletions
diff --git a/test/functional/lua/http.lua b/test/functional/lua/http.lua index 44a6c6fd3..0c1eff8ba 100644 --- a/test/functional/lua/http.lua +++ b/test/functional/lua/http.lua @@ -43,12 +43,15 @@ local function http_symbol(task) timeout = 1, }) + rspamd_logger.errx(task, 'rspamd_http.request[before]') + local err, response = rspamd_http.request({ url = 'http://127.0.0.1:18080' .. url, task = task, method = method, timeout = 1, }) + rspamd_logger.errx(task, 'rspamd_http.request[done] err: %1 response:%2', err, response) if not err then task:insert_result('HTTP_CORO_' .. response.code, 1.0, response.content) diff --git a/test/functional/lua/tcp.lua b/test/functional/lua/tcp.lua new file mode 100644 index 000000000..5b0f474bc --- /dev/null +++ b/test/functional/lua/tcp.lua @@ -0,0 +1,170 @@ +--[[[ +-- Just a test for TCP API +--]] + +local rspamd_tcp = require "rspamd_tcp" +local logger = require "rspamd_logger" +local tcp_sync = require "lua_tcp_sync" + +-- [[ old fashioned callback api ]] +local function http_simple_tcp_async_symbol(task) + logger.errx(task, 'http_tcp_symbol: begin') + local function http_get_cb(err, data, conn) + logger.errx(task, 'http_get_cb: got reply: %s, error: %s, conn: %s', data, err, conn) + task:insert_result('HTTP_ASYNC_RESPONSE_2', 1.0, data) + end + local function http_read_post_cb(err, conn) + logger.errx(task, 'http_read_post_cb: write done: error: %s, conn: %s', err, conn) + conn:add_read(http_get_cb) + end + local function http_read_cb(err, data, conn) + logger.errx(task, 'http_read_cb: got reply: %s, error: %s, conn: %s', data, err, conn) + conn:add_write(http_read_post_cb, "POST /request2 HTTP/1.1\r\n\r\n") + task:insert_result('HTTP_ASYNC_RESPONSE', 1.0, data) + end + rspamd_tcp:request({ + task = task, + callback = http_read_cb, + host = '127.0.0.1', + data = {'GET /request HTTP/1.1\r\nConnection: keep-alive\r\n\r\n'}, + read = true, + port = 18080, + }) +end + +local function http_simple_tcp_symbol(task) + logger.errx(task, 'connect_sync, before') + + local err + local is_ok, connection = tcp_sync.connect { + task = task, + host = '127.0.0.1', + timeout = 20, + port = 18080, + } + + logger.errx(task, 'connect_sync %1, %2', is_ok, tostring(connection)) + + is_ok, err = connection:write('GET /request_sync HTTP/1.1\r\nConnection: keep-alive\r\n\r\n') + + logger.errx(task, 'write %1, %2', is_ok, err) + if not is_ok then + task:insert_result('HTTP_SYNC_WRITE_ERROR', 1.0, err) + logger.errx(task, 'write error: %1', err) + end + + local data + is_ok, data = connection:read_once(); + + logger.errx(task, 'read_once: is_ok: %1, data: %2', is_ok, data) + if not is_ok then + task:insert_result('HTTP_SYNC_ERROR', 1.0, data) + else + task:insert_result('HTTP_SYNC_RESPONSE', 1.0, data) + end + + is_ok, err = connection:write("POST /request2 HTTP/1.1\r\n\r\n") + logger.errx(task, 'write[2] %1, %2', is_ok, err) + + is_ok, data = connection:read_once(); + logger.errx(task, 'read_once[2]: is_ok %1, data: %2', is_ok, data) + if not is_ok then + task:insert_result('HTTP_SYNC_ERROR_2', 1.0, data) + else + task:insert_result('HTTP_SYNC_RESPONSE_2', 1.0, data) + end + + connection:close() +end + +local function http_tcp_symbol(task) + local url = tostring(task:get_request_header('url')) + local method = tostring(task:get_request_header('method')) + + if url == 'nil' then + return + end + + local err + local is_ok, connection = tcp_sync.connect { + task = task, + host = '127.0.0.1', + timeout = 20, + port = 18080, + } + + logger.errx(task, 'connect_sync %1, %2', is_ok, tostring(connection)) + if not is_ok then + logger.errx(task, 'connect error: %1', connection) + return + end + + is_ok, err = connection:write(string.format('%s %s HTTP/1.1\r\nConnection: close\r\n\r\n', method:upper(), url)) + + logger.errx(task, 'write %1, %2', is_ok, err) + if not is_ok then + logger.errx(task, 'write error: %1', err) + return + end + + local content_length, content + + while true do + local header_line + is_ok, header_line = connection:read_until("\r\n") + if not is_ok then + logger.errx(task, 'failed to get header: %1', header_line) + return + end + + if header_line == "" then + logger.errx(task, 'headers done') + break + end + + local value + local header = header_line:gsub("([%w-]+): (.*)", + function (h, v) value = v; return h:lower() end) + + logger.errx(task, 'parsed header: %1 -> "%2"', header, value) + + if header == "content-length" then + content_length = tonumber(value) + end + + end + + if content_length then + is_ok, content = connection:read_bytes(content_length) + if is_ok then + task:insert_result('HTTP_SYNC_CONTENT_' .. method, 1.0, content) + end + else + is_ok, content = connection:read_until_eof() + if is_ok then + task:insert_result('HTTP_SYNC_EOF_' .. method, 1.0, content) + end + end + logger.errx(task, '(is_ok: %1) content [%2 bytes] %3', is_ok, content_length, content) +end + +rspamd_config:register_symbol({ + name = 'SIMPLE_TCP_ASYNC_TEST', + score = 1.0, + callback = http_simple_tcp_async_symbol, + no_squeeze = true +}) +rspamd_config:register_symbol({ + name = 'SIMPLE_TCP_TEST', + score = 1.0, + callback = http_simple_tcp_symbol, + no_squeeze = true +}) + +rspamd_config:register_symbol({ + name = 'HTTP_TCP_TEST', + score = 1.0, + callback = http_tcp_symbol, + no_squeeze = true +}) +-- ]] |