repositori / bagas / requests

requests public ready

https://github.com/psf/requests commit 3c7baaa 48 berkas · 374 potongan 2026-11-21T15:17:50Z
48berkas
36berkas python
374potongan
python 36 markdown 5 yaml 2 text 2 toml 1 css 1 html 1
tests/test_testserver.pypython 185 baris ditampilkan
1
import socket
2
import threading
3
import time
4
5
import pytest
6
7
import requests
8
from tests.testserver.server import Server
9
10
11
class TestTestServer:
12
def test_basic(self):
13
"""messages are sent and received properly"""
14
question = b"success?"
15
answer = b"yeah, success"
16
17
def handler(sock):
18
text = sock.recv(1000)
19
assert text == question
20
sock.sendall(answer)
21
22
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 == answer
28
sock.close()
29
30
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))
35
36
sock.close()
37
38
with pytest.raises(socket.error):
39
new_sock = socket.socket()
40
new_sock.connect((host, port))
41
42
def test_text_response(self):
43
"""the text_response_server sends the given text"""
44
server = Server.text_response_server(
45
"HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nroflol"
46
)
47
48
with server as (host, port):
49
r = requests.get(f"http://{host}:{port}")
50
51
assert r.status_code == 200
52
assert r.text == "roflol"
53
assert r.headers["Content-Length"] == "6"
49
r = requests.get(f"http://{host}:{port}")
50
51
assert r.status_code == 200
52
assert r.text == "roflol"
53
assert r.headers["Content-Length"] == "6"
54
55
def test_basic_response(self):
56
"""the basic response server returns an empty http response"""
57
with Server.basic_response_server() as (host, port):
58
r = requests.get(f"http://{host}:{port}")
59
assert r.status_code == 200
60
assert r.text == ""
61
assert r.headers["Content-Length"] == "0"
62
63
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()
66
67
with Server.basic_response_server(wait_to_close_event=block_server) as (
68
host,
69
port,
70
):
71
sock = socket.socket()
72
sock.connect((host, port))
73
sock.sendall(b"send something")
74
time.sleep(2.5)
75
sock.sendall(b"still alive")
76
block_server.set() # release server block
77
78
def test_multiple_requests(self):
79
"""multiple requests can be served"""
80
requests_to_handle = 5
81
82
server = Server.basic_response_server(requests_to_handle=requests_to_handle)
83
84
with server as (host, port):
85
server_url = f"http://{host}:{port}"
86
for _ in range(requests_to_handle):
87
r = requests.get(server_url)
88
assert r.status_code == 200
84
with server as (host, port):
85
server_url = f"http://{host}:{port}"
86
for _ in range(requests_to_handle):
87
r = requests.get(server_url)
88
assert r.status_code == 200
89
90
# the (n+1)th request fails
91
with pytest.raises(requests.exceptions.ConnectionError):
92
r = requests.get(server_url)
93
94
@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)
99
first_request = b"put your hands up in the air"
100
second_request = b"put your hand down in the floor"
101
102
with server as address:
103
sock1 = socket.socket()
104
sock2 = socket.socket()
105
106
sock1.connect(address)
107
sock1.sendall(first_request)
108
sock1.close()
109
110
sock2.connect(address)
111
sock2.sendall(second_request)
112
sock2.close()
113
114
assert server.handler_results[0] == first_request
115
assert server.handler_results[1] == second_request
116
117
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)
115
assert server.handler_results[1] == second_request
116
117
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)
120
121
with server as address:
122
sock = socket.socket()
123
sock.connect(address)
124
time.sleep(1.5)
125
sock.sendall(b"hehehe, not received")
126
sock.close()
127
128
assert server.handler_results[0] == b""
129
130
def test_request_recovery_with_bigger_timeout(self):
131
"""a biggest timeout can be specified"""
132
server = Server.basic_response_server(request_timeout=3)
133
data = b"bananadine"
134
135
with server as address:
136
sock = socket.socket()
137
sock.connect(address)
138
time.sleep(1.5)
139
sock.sendall(data)
140
sock.close()
141
142
assert server.handler_results[0] == data
143
144
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()
150
151
assert len(server.handler_results) == 0
152
153
# if the server thread fails to finish, the test suite will hang
154
# and get killed by the jenkins timeout.
150
151
assert len(server.handler_results) == 0
152
153
# if the server thread fails to finish, the test suite will hang
154
# and get killed by the jenkins timeout.
155
156
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
pass
161
162
assert len(server.handler_results) == 0
163
164
# if the server thread fails to finish, the test suite will hang
165
# and get killed by the jenkins timeout.