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:])