summaryrefslogtreecommitdiff
path: root/project1/proj1_s4498062/webtests.py
diff options
context:
space:
mode:
authorCamil Staps2016-03-04 18:33:12 +0100
committerCamil Staps2016-03-04 18:33:12 +0100
commit198db6f0003935e5f900719fa0b848924b8921bb (patch)
tree783956c7f4ced78e864a0ea7503bf5a63bef7370 /project1/proj1_s4498062/webtests.py
parentAdded framework project 1 (diff)
Project one until step 8 in the readme
Diffstat (limited to 'project1/proj1_s4498062/webtests.py')
-rw-r--r--project1/proj1_s4498062/webtests.py39
1 files changed, 28 insertions, 11 deletions
diff --git a/project1/proj1_s4498062/webtests.py b/project1/proj1_s4498062/webtests.py
index 9b0cdbf..a869de1 100644
--- a/project1/proj1_s4498062/webtests.py
+++ b/project1/proj1_s4498062/webtests.py
@@ -17,31 +17,43 @@ class TestGetRequests(unittest.TestCase):
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect(("localhost", portnr))
self.parser = webhttp.parser.ResponseParser()
+ self.default_headers = [
+ ('Host', 'localhost:%d' % portnr),
+ ('Connection', 'close')
+ ]
def tearDown(self):
"""Clean up after testing"""
self.client_socket.shutdown(socket.SHUT_RDWR)
self.client_socket.close()
- def test_existing_file(self):
- """GET for a single resource that exists"""
- # Send the request
+ def request(self, method, uri, headers):
request = webhttp.message.Request()
- request.method = "GET"
- request.uri = "/test/index.html"
- request.set_header("Host", "localhost:{}".format(portnr))
- request.set_header("Connection", "close")
+ request.method = method
+ request.uri = uri
+ for name, value in headers:
+ request.set_header(name, value)
+
self.client_socket.send(str(request))
- # Test response
message = self.client_socket.recv(1024)
response = self.parser.parse_response(message)
+
+ return response
+
+ def test_existing_file(self):
+ """GET for a single resource that exists"""
+ response = self.request(
+ 'GET', '/test/index.html', self.default_headers)
self.assertEqual(response.code, 200)
self.assertTrue(response.body)
def test_nonexistant_file(self):
"""GET for a single resource that does not exist"""
- pass
+ response = self.request(
+ 'GET', '/test/nonexistant.html', self.default_headers)
+ self.assertEqual(response.code, 404)
+ self.assertTrue(response.body)
def test_caching(self):
"""GET for an existing single resource followed by a GET for that same
@@ -51,11 +63,14 @@ class TestGetRequests(unittest.TestCase):
def test_extisting_index_file(self):
"""GET for a directory with an existing index.html file"""
- pass
+ self.assertEqual(self.request('GET', '/test', self.default_headers),
+ self.request('GET', '/test/index.html', self.default_headers))
def test_nonexistant_index_file(self):
"""GET for a directory with a non-existant index.html file"""
- pass
+ response = self.request('GET', '/test/no-index', self.default_headers)
+ self.assertEqual(response.code, 403)
+ self.assertTrue(response.body)
def test_persistent_close(self):
"""Multiple GETs over the same (persistent) connection with the last
@@ -86,6 +101,8 @@ if __name__ == "__main__":
# Arguments for the unittest framework
parser.add_argument('unittest_args', nargs='*')
args = parser.parse_args()
+
+ portnr = args.port
# Only pass the unittest arguments to unittest
sys.argv[1:] = args.unittest_args