Skip to content
Myr

Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Braulio Valdivielso Martínez · committed by Braulio Valdivielso Martínez · parent 578b032 · Browse files · 2 files+49 added−6 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+49 added−6 removed

  • other:+49 added−6 removed

2 files+49 added−6 removed

modified:testserver/server.py+28 added-5 removed
1#!/usr/bin/python23import threading 4import socket5import select66def consume_socket(sock, chunks=65536):7 while not sock.recv(chunks).endswith(b'\r\n\r\n'):8 pass78def consume_socket_content(sock, chunks=65536, timeout=0.5):9 content = ""10 more_to_read = select.select([sock], [], [], timeout)[0]1112 while more_to_read:13 new_content = sock.recv(chunks).decode("utf-8")1415 if len(new_content) == 0:16 more_to_read = False # empty recv means the socket disconnected1718 else:19 content += new_content 20 # stop reading if no new data is received for a while 21 more_to_read = select.select([sock], [], [], timeout)[0] 2223 return content2425class Server(threading.Thread):26 """ Dummy server using for unit testing """2728 def __init__(self, handler, host='localhost', port=0, requests_to_handle=1, wait_to_close_event=None):29 threading.Thread.__init__(self)3031 self.handler = handler32 self.handler_results = []3334 self.host = host35 self.port = port36 self.requests_to_handle = requests_to_handle37
1 unmodified line
39 self.ready_event = threading.Event()40 self.stop_event = threading.Event()4142 @classmethod26 def text_response_server(cls, text, **kwargs):43 def text_response_server(cls, text, request_timeout=0.5, **kwargs):44 def text_response_handler(sock):45 request_content = consume_socket_content(sock, timeout=request_timeout)46 sock.send(text.encode())4748 return request_content495051 server = Server(text_response_handler, **kwargs)5253 return server54
26 unmodified lines
8182 def _handle_requests_and_close_server(self, server_sock):83 for _ in range(self.requests_to_handle):84 sock = server_sock.accept()[0]64 self.handler(sock)85 handler_result = self.handler(sock)8687 self.handler_results.append(handler_result)88 89 if self.wait_to_close_event:90 self.wait_to_close_event.wait()91
15 unmodified lines
modified:test_requests.py+21 added-1 removed
1777 unmodified lines
1778 def test_server_closes(self):1779 with Server.basic_response_server() as (host, port):1780 sock = socket.socket()1781 sock.connect((host, port))17821783 sock.close()17841785 with pytest.raises(socket.error):1786 new_sock = socket.socket()
10 unmodified lines
1797 r = requests.get('http://{}:{}'.format(host, port))17981799 assert r.status_code == 2001800 assert r.text == 'roflol'1800 assert r.headers['Content-Length'] == '6'1801 assert r.headers['Content-Length'] == '6' 1802 1803 def test_basic_response(self):1804 with Server.basic_response_server() as (host, port):1805 r = requests.get('http://{}:{}'.format(host, port))
26 unmodified lines
1832 # the (n+1)th request fails1833 with pytest.raises(requests.exceptions.ConnectionError):1834 r = requests.get(server_url)18351836 def test_request_recovery(self):1837 server = Server.basic_response_server(requests_to_handle=2)1838 first_request = "put your hands up in the air"1839 second_request = "put your hand down in the floor"18401841 with server as address:1842 sock1 = socket.socket()1843 sock2 = socket.socket()1844 1845 sock1.connect(address)1846 sock1.send(first_request.encode())1847 sock1.close()18481849 sock2.connect(address)1850 sock2.send(second_request.encode())1851 sock2.close()18521853 assert server.handler_results[0] == first_request1854 assert server.handler_results[1] == second_request185518561857if __name__ == '__main__':1858 unittest.main()