From: László Vaskó Date: Sat, 1 Dec 2012 23:21:08 +0000 (+0100) Subject: Added testcases for keep-alive handling X-Git-Tag: v1.0.0~86^2^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e8d20926d380a02bba4b03530eae6fbf54374b21;p=services%2Fpython-requests.git Added testcases for keep-alive handling * Added a dummy_server module to check connection count * Added testcases for proxy and direct connections --- diff --git a/tests/dummy_server.py b/tests/dummy_server.py new file mode 100644 index 0000000..1096e25 --- /dev/null +++ b/tests/dummy_server.py @@ -0,0 +1,46 @@ +import asyncore +import threading +import socket + +class HttpServer(threading.Thread): + def __init__(self, port): + threading.Thread.__init__(self) + self.dispatcher = HttpServerDispatcher(port) + + def run(self): + asyncore.loop() + + @property + def connection_count(self): + return self.dispatcher.connection_count + + def close(self): + asyncore.close_all() + +class HttpServerDispatcher(asyncore.dispatcher): + def __init__(self, port): + asyncore.dispatcher.__init__(self) + self.connected = False + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.bind(('127.0.0.1', port)) + self.listen(1) + self.connection_count = 0 + + def handle_accept(self): + self.connection_count += 1 + self.handler = RequestHandler(self.accept()[0]) + + def handle_close(self): + self.close() + + +class RequestHandler(asyncore.dispatcher_with_send): + def __init__(self, sock): + asyncore.dispatcher_with_send.__init__(self, sock) + self.response = ("HTTP/1.1 200 OK\r\n" + "Connection: keep-alive\r\n" + "Content-Length: 0\r\n\r\n") + + def handle_read(self): + self.recv(1024) + self.send(self.response) diff --git a/tests/test_keep_alive.py b/tests/test_keep_alive.py new file mode 100644 index 0000000..447fc8e --- /dev/null +++ b/tests/test_keep_alive.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import sys +import unittest + +# Path hack. +sys.path.insert(0, os.path.abspath('..')) +import requests +import dummy_server + +class KeepAliveTests(unittest.TestCase): + server_and_proxy_port = 1234 + request_count = 2 + url = 'http://localhost:{0}'.format(server_and_proxy_port) + proxies={'http': url} + + def setUp(self): + self.session = requests.session() + self.proxy_server = dummy_server.HttpServer(self.server_and_proxy_port) + self.proxy_server.start() + + def tearDown(self): + self.proxy_server.close() + + def test_keep_alive_with_direct_connection(self): + self.make_requests() + self.check_each_request_are_in_same_connection() + + def test_no_keep_alive_with_direct_connection(self): + self.disable_keep_alive_in_session() + self.make_requests() + self.check_each_request_are_in_different_connection() + + def test_keep_alive_with_proxy_connection(self): + self.make_proxy_requests() + self.check_each_request_are_in_same_connection() + + def test_no_keep_alive_with_proxy_connection(self): + self.disable_keep_alive_in_session() + self.make_proxy_requests() + self.check_each_request_are_in_different_connection() + + def make_proxy_requests(self): + self.make_requests(self.proxies) + + def make_requests(self, proxies=None): + for _ in xrange(self.request_count): + self.session.get(self.url, proxies=proxies).text + + def check_each_request_are_in_same_connection(self): + """Keep-alive requests open a single connection to the server.""" + self.assertEqual(self.proxy_server.connection_count, 1) + + def check_each_request_are_in_different_connection(self): + """Keep-alive requests open a single connection to the server.""" + self.assertEqual(self.proxy_server.connection_count, self.request_count) + + def disable_keep_alive_in_session(self): + self.session.config['keep_alive'] = False + + +if __name__ == '__main__': + unittest.main()