Added testcases for keep-alive handling
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Added a dummy_server module to check connection count * Added testcases for proxy and direct connections
authored by László Vaskó · committed by László Vaskó · parent 23e6f53 · Browse files · 2 files·+111 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+111 added−0 removed
- tests:+111 added−0 removed
Provenance
- Commit
e8d2092- Tree
8d103c6- Author
László Vaskó <vlacko@gmail.com>- Committer
László Vaskó <vlacko@gmail.com>- Parent
23e6f53
2 files changed+111 added−0 removed
added:tests/dummy_server.py+46 added
1+import asyncore2+import threading3+import socket4+5+class HttpServer(threading.Thread):6+ def __init__(self, port):7+ threading.Thread.__init__(self)8+ self.dispatcher = HttpServerDispatcher(port)9+10+ def run(self):11+ asyncore.loop()12+13+ @property14+ def connection_count(self):15+ return self.dispatcher.connection_count16+17+ def close(self):18+ asyncore.close_all()19+20+class HttpServerDispatcher(asyncore.dispatcher):21+ def __init__(self, port):22+ asyncore.dispatcher.__init__(self)23+ self.connected = False24+ self.create_socket(socket.AF_INET, socket.SOCK_STREAM)25+ self.bind(('127.0.0.1', port))26+ self.listen(1)27+ self.connection_count = 028+29+ def handle_accept(self):30+ self.connection_count += 131+ self.handler = RequestHandler(self.accept()[0])32+33+ def handle_close(self):34+ self.close()35+36+37+class RequestHandler(asyncore.dispatcher_with_send):38+ def __init__(self, sock):39+ asyncore.dispatcher_with_send.__init__(self, sock)40+ self.response = ("HTTP/1.1 200 OK\r\n"41+ "Connection: keep-alive\r\n"42+ "Content-Length: 0\r\n\r\n")43+44+ def handle_read(self):45+ self.recv(1024)46+ self.send(self.response)added:tests/test_keep_alive.py+65 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+4+import os5+import sys6+import unittest7+8+# Path hack.9+sys.path.insert(0, os.path.abspath('..'))10+import requests11+import dummy_server12+13+class KeepAliveTests(unittest.TestCase):14+ server_and_proxy_port = 123415+ request_count = 216+ url = 'http://localhost:{0}'.format(server_and_proxy_port)17+ proxies={'http': url}18+19+ def setUp(self):20+ self.session = requests.session()21+ self.proxy_server = dummy_server.HttpServer(self.server_and_proxy_port)22+ self.proxy_server.start()23+24+ def tearDown(self):25+ self.proxy_server.close()26+27+ def test_keep_alive_with_direct_connection(self):28+ self.make_requests()29+ self.check_each_request_are_in_same_connection()30+31+ def test_no_keep_alive_with_direct_connection(self):32+ self.disable_keep_alive_in_session()33+ self.make_requests()34+ self.check_each_request_are_in_different_connection()35+36+ def test_keep_alive_with_proxy_connection(self):37+ self.make_proxy_requests()38+ self.check_each_request_are_in_same_connection()39+40+ def test_no_keep_alive_with_proxy_connection(self):41+ self.disable_keep_alive_in_session()42+ self.make_proxy_requests()43+ self.check_each_request_are_in_different_connection()44+45+ def make_proxy_requests(self):46+ self.make_requests(self.proxies)47+48+ def make_requests(self, proxies=None):49+ for _ in xrange(self.request_count):50+ self.session.get(self.url, proxies=proxies).text51+52+ def check_each_request_are_in_same_connection(self):53+ """Keep-alive requests open a single connection to the server."""54+ self.assertEqual(self.proxy_server.connection_count, 1)55+56+ def check_each_request_are_in_different_connection(self):57+ """Keep-alive requests open a single connection to the server."""58+ self.assertEqual(self.proxy_server.connection_count, self.request_count)59+60+ def disable_keep_alive_in_session(self):61+ self.session.config['keep_alive'] = False62+63+64+if __name__ == '__main__':65+ unittest.main()