From: Mikhail Galanin Date: Wed, 22 Aug 2018 14:59:44 +0000 (+0100) Subject: [Test] Added test for HTTP API X-Git-Tag: 1.8.0~216^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ee6b0c059ad291e2e1a3a5cd6f01effd6f115a3;p=thirdparty%2Frspamd.git [Test] Added test for HTTP API --- diff --git a/test/functional/cases/220_http.robot b/test/functional/cases/220_http.robot new file mode 100644 index 0000000000..427b4467f0 --- /dev/null +++ b/test/functional/cases/220_http.robot @@ -0,0 +1,41 @@ +*** Settings *** +# Test Setup Http Setup +Test Teardown Http Teardown +Library Process +Library ${TESTDIR}/lib/rspamd.py +Resource ${TESTDIR}/lib/rspamd.robot +Variables ${TESTDIR}/lib/vars.py + +*** Variables *** +# ${CONFIG} ${TESTDIR}/configs/http.conf +${URL_TLD} ${TESTDIR}/../lua/unit/test_tld.dat +${CONFIG} ${TESTDIR}/configs/lua_test.conf +${MESSAGE} ${TESTDIR}/messages/spam_message.eml +${MESSAGE2} ${TESTDIR}/messages/freemail.eml +${REDIS_SCOPE} Suite +${RSPAMD_SCOPE} Suite + +*** Test Cases *** +HTTP + Run Dummy Http + [Setup] Lua Setup ${TESTDIR}/lua/http.lua + ${result} = Scan Message With Rspamc ${MESSAGE} + Check Rspamc ${result} HTTP_DNS_200 + Check Rspamc ${result} HTTP_200 + + +*** Keywords *** +Lua Setup + [Arguments] ${LUA_SCRIPT} + Set Test Variable ${LUA_SCRIPT} + Generic Setup + +Http Teardown + ${http_pid} = Get File /tmp/dummy_http.pid + # Shutdown Process With Children ${http_pid} + Normal Teardown + +Run Dummy Http + [Arguments] + ${result} = Start Process ${TESTDIR}/util/dummy_http.py + Wait Until Created /tmp/dummy_http.pid diff --git a/test/functional/configs/lua_test.conf b/test/functional/configs/lua_test.conf index f01f07cad2..af84a15786 100644 --- a/test/functional/configs/lua_test.conf +++ b/test/functional/configs/lua_test.conf @@ -10,6 +10,10 @@ options = { name = "example.com", type = "a"; replies = ["93.184.216.34"]; + }, { + name = "site.resolveme", + type = "a"; + replies = ["127.0.0.1"]; }] } } diff --git a/test/functional/lua/http.lua b/test/functional/lua/http.lua new file mode 100644 index 0000000000..94dd36a29c --- /dev/null +++ b/test/functional/lua/http.lua @@ -0,0 +1,32 @@ +local rspamd_http = require "rspamd_http" + +local function http_symbol(task) + local function http_callback(err, code, body) + task:insert_result('HTTP_' .. code, 1.0) + end + + local function http_dns_callback(err, code, body) + task:insert_result('HTTP_DNS_' .. code, 1.0) + end + + rspamd_http.request({ + url = 'http://127.0.0.1:18080/request', + task = task, + method = 'post', + callback = http_callback, + }) + + --[[ request to this address involved DNS resolver subsystem ]] + rspamd_http.request({ + url = 'http://site.resolveme:18080/request', + task = task, + method = 'post', + callback = http_dns_callback, + }) +end + +rspamd_config:register_symbol({ + name = 'SIMPLE_TEST', + score = 1.0, + callback = http_symbol +}) diff --git a/test/functional/util/dummy_fprot.py b/test/functional/util/dummy_fprot.py index c35f0628f5..34725280ba 100755 --- a/test/functional/util/dummy_fprot.py +++ b/test/functional/util/dummy_fprot.py @@ -1,14 +1,15 @@ #!/usr/bin/env python - -PID = "/tmp/dummy_fprot.pid" - import os import sys +import signal + + try: import SocketServer as socketserver except: import socketserver -import signal + +PID = "/tmp/dummy_fprot.pid" class MyTCPHandler(socketserver.BaseRequestHandler): diff --git a/test/functional/util/dummy_http.py b/test/functional/util/dummy_http.py new file mode 100755 index 0000000000..5a04b3664c --- /dev/null +++ b/test/functional/util/dummy_http.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +import BaseHTTPServer +import time +import os +import sys +import signal + +PORT = 18080 +HOST_NAME = '127.0.0.1' + +PID = "/tmp/dummy_http.pid" + + +class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): + + def do_HEAD(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + + def do_GET(self): + """Respond to a GET request.""" + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write("hello world") + + def do_POST(self): + """Respond to a GET request.""" + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write("hello post") + + +class MyHttp(BaseHTTPServer.HTTPServer): + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=False): + BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate) + self.keep_running = True + + def run(self): + self.server_bind() + self.server_activate() + + with open(PID, 'w+') as f: + f.write(str(os.getpid())) + f.close() + + while self.keep_running: + try: + self.handle_request() + except Exception: + pass + + def stop(self): + self.keep_running = False + self.server_close() + + +if __name__ == '__main__': + server_class = BaseHTTPServer.HTTPServer + httpd = MyHttp((HOST_NAME, PORT), MyHandler) + httpd.allow_reuse_address = True + httpd.timeout = 1 + + def alarm_handler(signum, frame): + httpd.stop() + + signal.signal(signal.SIGALRM, alarm_handler) + signal.signal(signal.SIGTERM, alarm_handler) + signal.alarm(5) + + try: + httpd.run() + except KeyboardInterrupt: + pass + httpd.server_close()