From cbcc18fe15de6de19d283e7f2d8f388dd36f4fdb Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 7 Apr 2016 17:11:38 +0200 Subject: Persistent connections tests project 1 --- project1/proj1_s4498062/webtests.py | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'project1') diff --git a/project1/proj1_s4498062/webtests.py b/project1/proj1_s4498062/webtests.py index 3827144..d047526 100644 --- a/project1/proj1_s4498062/webtests.py +++ b/project1/proj1_s4498062/webtests.py @@ -2,6 +2,7 @@ import os.path import unittest import socket import sys +import time from webhttp.config import config import webhttp.message @@ -23,6 +24,10 @@ class TestGetRequests(unittest.TestCase): ('Host', 'localhost:%d' % portnr), ('Connection', 'close') ] + self.default_headers_pers = [ + ('Host', 'localhost:%d' % portnr), + ('Connection', 'keep-alive') + ] config().read(os.path.expanduser('~/.webpy.ini')) @@ -34,6 +39,15 @@ class TestGetRequests(unittest.TestCase): except: pass + def refresh_socket(self): + try: + self.client_skt.shutdown(socket.SHUT_RDWR) + self.client_skt.close() + except: + pass + self.client_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.client_skt.connect(("localhost", portnr)) + def request(self, method, uri, headers, persistent=False): request = webhttp.message.Request() request.method = method @@ -41,18 +55,20 @@ class TestGetRequests(unittest.TestCase): for name, value in headers: request.set_header(name, value) - self.client_skt.send(str(request)) + sent = self.client_skt.send(str(request)) + if sent == 0: + return None message = self.client_skt.recv(1024) - response = self.parser.parse_response(message) if not persistent: - self.client_skt.shutdown(socket.SHUT_RDWR) - self.client_skt.close() - self.client_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.client_skt.connect(("localhost", portnr)) + self.refresh_socket() - return response + if message == '': + return None + else: + response = self.parser.parse_response(message) + return response def test_existing_file(self): """GET for a single resource that exists""" @@ -110,14 +126,30 @@ class TestGetRequests(unittest.TestCase): """Multiple GETs over the same (persistent) connection with the last GET prompting closing the connection, the connection should be closed. """ - pass + try: + self.request('GET', '/test', self.default_headers_pers, True) + self.request('GET', '/test', self.default_headers_pers, True) + self.request('GET', '/test', self.default_headers_pers, True) + except socket.error: + self.fail('Persistent connection closed prematurely') + + self.request('GET', '/test', self.default_headers, True) + self.assertIsNone(self.request('GET', '/test', + self.default_headers_pers, True)) + + self.refresh_socket() def test_persistent_timeout(self): """Multiple GETs over the same (persistent) connection, followed by a wait during which the connection times out, the connection should be closed. """ - pass + self.request('GET', '/test', self.default_headers_pers, True) + time.sleep(20) + self.assertIsNone(self.request('GET', '/test', + self.default_headers_pers, True)) + + self.refresh_socket() def test_encoding(self): """GET which requests an existing resource using gzip encodign, which @@ -130,7 +162,6 @@ class TestGetRequests(unittest.TestCase): r2 = self.request('GET', '/test', self.default_headers + \ [('Accept-Encoding', '')]) self.assertEqual(r1.body, r2.body) - pass def test_doubledot(self): response = self.request('GET', '/../test', self.default_headers) -- cgit v1.2.3