test_testserver.py
This file
Path
tests/test_testserver.py
165 lines · 5.4 KiB · Python
Last changed by
Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
History of this fileTake it
The walk stopped after 200 commits: lines older than 967a05b point at it.
Update renamed GitHub handleMartinbefore adoption967a05b
line 1why, one step back1import socketAdd automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 2why, one step back2import threadingUpdate renamed GitHub handleMartinbefore adoption967a05b
lines 3–7why, one step back3import time45import pytest6from tests.testserver.server import Server7Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 8why, one step back8import requestsUpdate renamed GitHub handleMartinbefore adoption967a05b
lines 9–10why, one step back910Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 11why, one step back11class TestTestServer:Update renamed GitHub handleMartinbefore adoption967a05b
lines 12–44why, one step back12 def test_basic(self):13 """messages are sent and received properly"""14 question = b"success?"15 answer = b"yeah, success"1617 def handler(sock):18 text = sock.recv(1000)19 assert text == question20 sock.sendall(answer)2122 with Server(handler) as (host, port):23 sock = socket.socket()24 sock.connect((host, port))25 sock.sendall(question)26 text = sock.recv(1000)27 assert text == answer28 sock.close()2930 def test_server_closes(self):31 """the server closes when leaving the context manager"""32 with Server.basic_response_server() as (host, port):33 sock = socket.socket()34 sock.connect((host, port))3536 sock.close()3738 with pytest.raises(socket.error):39 new_sock = socket.socket()40 new_sock.connect((host, port))4142 def test_text_response(self):43 """the text_response_server sends the given text"""44 server = Server.text_response_server(Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 45why, one step back45 "HTTP/1.1 200 OK\r\n" "Content-Length: 6\r\n" "\r\nroflol"Update renamed GitHub handleMartinbefore adoption967a05b
lines 46–48why, one step back46 )4748 with server as (host, port):Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 49why, one step back49 r = requests.get(f"http://{host}:{port}")Update renamed GitHub handleMartinbefore adoption967a05b
lines 50–51why, one step back5051 assert r.status_code == 200Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
lines 52–53why, one step back52 assert r.text == "roflol"53 assert r.headers["Content-Length"] == "6"Update renamed GitHub handleMartinbefore adoption967a05b
lines 54–57why, one step back5455 def test_basic_response(self):56 """the basic response server returns an empty http response"""57 with Server.basic_response_server() as (host, port):Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 58why, one step back58 r = requests.get(f"http://{host}:{port}")Update renamed GitHub handleMartinbefore adoption967a05b
line 59why, one step back59 assert r.status_code == 200Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
lines 60–61why, one step back60 assert r.text == ""61 assert r.headers["Content-Length"] == "0"Update renamed GitHub handleMartinbefore adoption967a05b
lines 62–66why, one step back6263 def test_basic_waiting_server(self):64 """the server waits for the block_server event to be set before closing"""65 block_server = threading.Event()66Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
lines 67–70why, one step back67 with Server.basic_response_server(wait_to_close_event=block_server) as (68 host,69 port,70 ):Update renamed GitHub handleMartinbefore adoption967a05b
lines 71–72why, one step back71 sock = socket.socket()72 sock.connect((host, port))Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 73why, one step back73 sock.sendall(b"send something")Update renamed GitHub handleMartinbefore adoption967a05b
line 74why, one step back74 time.sleep(2.5)Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 75why, one step back75 sock.sendall(b"still alive")Update renamed GitHub handleMartinbefore adoption967a05b
lines 76–84why, one step back76 block_server.set() # release server block7778 def test_multiple_requests(self):79 """multiple requests can be served"""80 requests_to_handle = 58182 server = Server.basic_response_server(requests_to_handle=requests_to_handle)8384 with server as (host, port):Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 85why, one step back85 server_url = f"http://{host}:{port}"Update renamed GitHub handleMartinbefore adoption967a05b
lines 86–98why, one step back86 for _ in range(requests_to_handle):87 r = requests.get(server_url)88 assert r.status_code == 2008990 # the (n+1)th request fails91 with pytest.raises(requests.exceptions.ConnectionError):92 r = requests.get(server_url)9394 @pytest.mark.skip(reason="this fails non-deterministically under pytest-xdist")95 def test_request_recovery(self):96 """can check the requests content"""97 # TODO: figure out why this sometimes fails when using pytest-xdist.98 server = Server.basic_response_server(requests_to_handle=2)Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
lines 99–100why, one step back99 first_request = b"put your hands up in the air"100 second_request = b"put your hand down in the floor"Update renamed GitHub handleMartinbefore adoption967a05b
lines 101–124why, one step back101102 with server as address:103 sock1 = socket.socket()104 sock2 = socket.socket()105106 sock1.connect(address)107 sock1.sendall(first_request)108 sock1.close()109110 sock2.connect(address)111 sock2.sendall(second_request)112 sock2.close()113114 assert server.handler_results[0] == first_request115 assert server.handler_results[1] == second_request116117 def test_requests_after_timeout_are_not_received(self):118 """the basic response handler times out when receiving requests"""119 server = Server.basic_response_server(request_timeout=1)120121 with server as address:122 sock = socket.socket()123 sock.connect(address)124 time.sleep(1.5)Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 125why, one step back125 sock.sendall(b"hehehe, not received")Update renamed GitHub handleMartinbefore adoption967a05b
lines 126–127why, one step back126 sock.close()127Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 128why, one step back128 assert server.handler_results[0] == b""Update renamed GitHub handleMartinbefore adoption967a05b
lines 129–132why, one step back129130 def test_request_recovery_with_bigger_timeout(self):131 """a biggest timeout can be specified"""132 server = Server.basic_response_server(request_timeout=3)Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
line 133why, one step back133 data = b"bananadine"Update renamed GitHub handleMartinbefore adoption967a05b
lines 134–165why, one step back134135 with server as address:136 sock = socket.socket()137 sock.connect(address)138 time.sleep(1.5)139 sock.sendall(data)140 sock.close()141142 assert server.handler_results[0] == data143144 def test_server_finishes_on_error(self):145 """the server thread exits even if an exception exits the context manager"""146 server = Server.basic_response_server()147 with pytest.raises(Exception):148 with server:149 raise Exception()150151 assert len(server.handler_results) == 0152153 # if the server thread fails to finish, the test suite will hang154 # and get killed by the jenkins timeout.155156 def test_server_finishes_when_no_connections(self):157 """the server thread exits even if there are no connections"""158 server = Server.basic_response_server()159 with server:160 pass161162 assert len(server.handler_results) == 0163164 # if the server thread fails to finish, the test suite will hang165 # and get killed by the jenkins timeout.