]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Added test for HTTP API
authorMikhail Galanin <mgalanin@mimecast.com>
Wed, 22 Aug 2018 14:59:44 +0000 (15:59 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Wed, 22 Aug 2018 14:59:44 +0000 (15:59 +0100)
test/functional/cases/220_http.robot [new file with mode: 0644]
test/functional/configs/lua_test.conf
test/functional/lua/http.lua [new file with mode: 0644]
test/functional/util/dummy_fprot.py
test/functional/util/dummy_http.py [new file with mode: 0755]

diff --git a/test/functional/cases/220_http.robot b/test/functional/cases/220_http.robot
new file mode 100644 (file)
index 0000000..427b446
--- /dev/null
@@ -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
index f01f07cad2fd03d8a2c43b66d2b060c3f3637674..af84a15786fbb057d66510969ea83aa2c2b2ac0c 100644 (file)
@@ -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 (file)
index 0000000..94dd36a
--- /dev/null
@@ -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
+})
index c35f0628f5f579f197990429de5e5e23f7d0c91e..34725280bacabf29c4086a284805dfeb0a8fc3f9 100755 (executable)
@@ -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 (executable)
index 0000000..5a04b36
--- /dev/null
@@ -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()