From 9257de3a2ad330e0cef78cbbae9567d9d750e949 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Sun, 1 Mar 2015 23:07:23 +0100 Subject: [PATCH] start tests --- run_server.sh | 2 +- run_tests.sh | 3 +++ webserver/server.py | 19 ++++++++++----- webtests/tests.py | 57 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) mode change 100644 => 100755 run_tests.sh create mode 100644 webtests/tests.py diff --git a/run_server.sh b/run_server.sh index 3268b9f..5fd1756 100755 --- a/run_server.sh +++ b/run_server.sh @@ -1,3 +1,3 @@ #/bin/sh -python2 webserver/server.py +python2 webserver/server.py $1 diff --git a/run_tests.sh b/run_tests.sh old mode 100644 new mode 100755 index e69de29..1c223b2 --- a/run_tests.sh +++ b/run_tests.sh @@ -0,0 +1,3 @@ +#/bin/sh + +python2 webtests/tests.py $1 diff --git a/webserver/server.py b/webserver/server.py index 2f88537..050bb2e 100644 --- a/webserver/server.py +++ b/webserver/server.py @@ -3,6 +3,8 @@ from urllib import unquote import hashlib from datetime import datetime, timedelta +import sys + import asyncserver @@ -119,10 +121,15 @@ class HTTPDirClient(HTTPClient): if __name__ == '__main__': - s = asyncserver.Server(HTTPDirClient) try: - while 1: - s.run() - except: - s.close() - raise + port = int(sys.argv[1]) + except (ValueError, IndexError): + print "invalid port given" + else: + s = asyncserver.Server(HTTPDirClient, port = port) + try: + while 1: + s.run() + except: + s.close() + raise diff --git a/webtests/tests.py b/webtests/tests.py new file mode 100644 index 0000000..83568ca --- /dev/null +++ b/webtests/tests.py @@ -0,0 +1,57 @@ +import sys +import unittest +import socket + +def http_req(url, headers = []): + h = "".join([hdr + '\r\n' for hdr in ["Connection: close"] + headers]) + return "GET %s HTTP/1.1\r\n%s\r\n" % (url, h) + + + +def recvall(sock): + """call recv until the connection is closed""" + d = "" + while 1: + s = sock.recv(8192) + if not s: return d + d += s + +# GET for an existing single resource +class GETSingles(unittest.TestCase): # in YOUR local area NOW + def test_single_existing(self): + s = socket.create_connection(('localhost', 8080)) + s.send(http_req('/')) + resp = recvall(s) + assert resp.startswith("HTTP/1.1 200 OK\r\n") + # TODO: check content + # GET for a single resource that doesn't exist + def test_single_notexisting(self): + s = socket.create_connection(('localhost', 8080)) + s.send(http_req('/this_is_not_the_path/you/are/looking/for')) + resp = recvall(s) + assert resp.startswith("HTTP/1.1 404 Not Found\r\n") + + +# GET for an existing single resource followed by a GET for that same resource, +# with caching utilized on the client/tester side +# GET for a directory with an existing index.html file +# GET for a directory with non-existing index.html file + +# multiple GETs over the same (persistent) connection with the last GET prompting closing the con- +# nection, the connection should be closed +# multiple GETs over the same (persistent) connection, followed by a wait during which the connection +# times out, the connection should be closed +# multiple GETs, some of which are parallel (think of the situation when your browser is fetching a +# composite resource), the responses should be sent in an orderly fashion + +#In each case, you should test the response code after every step, as well as the content (if any is expected +#or if none is expected). + +if __name__ == '__main__': + try: + port = int(sys.argv[1]) + except (ValueError, IndexError): + print "invalid port given" + else: + # raise NotImplementedError() + unittest.main(argv=sys.argv[1:])