Skip to content
Myr

test_testserver.py

This file

Path

tests/test_testserver.py

166 lines · 5.5 KiB · Python

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 916d171 point at it.

Merge branch 'master' of github.com:kennethreitz/requestsKenneth Reitzbefore adoption916d171

lines 1–166why, one step back
1# -*- coding: utf-8 -*-
23import threading4import socket5import time67import pytest8import requests9from tests.testserver.server import Server101112class TestTestServer:1314 def test_basic(self):15 """messages are sent and received properly"""16 question = b"success?"17 answer = b"yeah, success"1819 def handler(sock):20 text = sock.recv(1000)21 assert text == question22 sock.sendall(answer)2324 with Server(handler) as (host, port):25 sock = socket.socket()26 sock.connect((host, port))27 sock.sendall(question)28 text = sock.recv(1000)29 assert text == answer30 sock.close()3132 def test_server_closes(self):33 """the server closes when leaving the context manager"""34 with Server.basic_response_server() as (host, port):35 sock = socket.socket()36 sock.connect((host, port))3738 sock.close()3940 with pytest.raises(socket.error):41 new_sock = socket.socket()42 new_sock.connect((host, port))4344 def test_text_response(self):45 """the text_response_server sends the given text"""46 server = Server.text_response_server(47 "HTTP/1.1 200 OK\r\n" +48 "Content-Length: 6\r\n" +49 "\r\nroflol"50 )5152 with server as (host, port):53 r = requests.get('http://{0}:{1}'.format(host, port))5455 assert r.status_code == 20056 assert r.text == u'roflol'57 assert r.headers['Content-Length'] == '6'5859 def test_basic_response(self):60 """the basic response server returns an empty http response"""61 with Server.basic_response_server() as (host, port):62 r = requests.get('http://{0}:{1}'.format(host, port))63 assert r.status_code == 20064 assert r.text == u''65 assert r.headers['Content-Length'] == '0'6667 def test_basic_waiting_server(self):68 """the server waits for the block_server event to be set before closing"""69 block_server = threading.Event()7071 with Server.basic_response_server(wait_to_close_event=block_server) as (host, port):72 sock = socket.socket()73 sock.connect((host, port))74 sock.sendall(b'send something')75 time.sleep(2.5)76 sock.sendall(b'still alive')77 block_server.set() # release server block7879 def test_multiple_requests(self):80 """multiple requests can be served"""81 requests_to_handle = 58283 server = Server.basic_response_server(requests_to_handle=requests_to_handle)8485 with server as (host, port):86 server_url = 'http://{0}:{1}'.format(host, port)87 for _ in range(requests_to_handle):88 r = requests.get(server_url)89 assert r.status_code == 2009091 # the (n+1)th request fails92 with pytest.raises(requests.exceptions.ConnectionError):93 r = requests.get(server_url)9495 @pytest.mark.skip(reason="this fails non-deterministically under pytest-xdist")96 def test_request_recovery(self):97 """can check the requests content"""98 # TODO: figure out why this sometimes fails when using pytest-xdist.99 server = Server.basic_response_server(requests_to_handle=2)100 first_request = b'put your hands up in the air'101 second_request = b'put your hand down in the floor'102103 with server as address:104 sock1 = socket.socket()105 sock2 = socket.socket()106107 sock1.connect(address)108 sock1.sendall(first_request)109 sock1.close()110111 sock2.connect(address)112 sock2.sendall(second_request)113 sock2.close()114115 assert server.handler_results[0] == first_request116 assert server.handler_results[1] == second_request117118 def test_requests_after_timeout_are_not_received(self):119 """the basic response handler times out when receiving requests"""120 server = Server.basic_response_server(request_timeout=1)121122 with server as address:123 sock = socket.socket()124 sock.connect(address)125 time.sleep(1.5)126 sock.sendall(b'hehehe, not received')127 sock.close()128129 assert server.handler_results[0] == b''130131 def test_request_recovery_with_bigger_timeout(self):132 """a biggest timeout can be specified"""133 server = Server.basic_response_server(request_timeout=3)134 data = b'bananadine'135136 with server as address:137 sock = socket.socket()138 sock.connect(address)139 time.sleep(1.5)140 sock.sendall(data)141 sock.close()142143 assert server.handler_results[0] == data144145 def test_server_finishes_on_error(self):146 """the server thread exits even if an exception exits the context manager"""147 server = Server.basic_response_server()148 with pytest.raises(Exception):149 with server:150 raise Exception()151152 assert len(server.handler_results) == 0153154 # if the server thread fails to finish, the test suite will hang155 # and get killed by the jenkins timeout.156157 def test_server_finishes_when_no_connections(self):158 """the server thread exits even if there are no connections"""159 server = Server.basic_response_server()160 with server:161 pass162163 assert len(server.handler_results) == 0164165 # if the server thread fails to finish, the test suite will hang166 # and get killed by the jenkins timeout.