aboutsummaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_http.py
diff options
context:
space:
mode:
authorMikhail Galanin <mgalanin@mimecast.com>2018-08-22 15:59:44 +0100
committerMikhail Galanin <mgalanin@mimecast.com>2018-08-22 15:59:44 +0100
commit9ee6b0c059ad291e2e1a3a5cd6f01effd6f115a3 (patch)
tree29671f01a752f35256111e244bec3accb15c2766 /test/functional/util/dummy_http.py
parent434446f72add375d0f7a94a94e4797d9bd6d1d6a (diff)
downloadrspamd-9ee6b0c059ad291e2e1a3a5cd6f01effd6f115a3.tar.gz
rspamd-9ee6b0c059ad291e2e1a3a5cd6f01effd6f115a3.zip
[Test] Added test for HTTP API
Diffstat (limited to 'test/functional/util/dummy_http.py')
-rwxr-xr-xtest/functional/util/dummy_http.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/test/functional/util/dummy_http.py b/test/functional/util/dummy_http.py
new file mode 100755
index 000000000..5a04b3664
--- /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()