Skip to content

Commit e392573

Browse files
committed
requests: Add request header and body tests
1 parent 80b21be commit e392573

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import io
2+
import sys
3+
4+
5+
class Socket:
6+
def __init__(self):
7+
self._write_buffer = io.BytesIO()
8+
self._read_buffer = io.BytesIO(b"HTTP/1.0 200 OK\r\n\r\n")
9+
10+
def connect(self, address):
11+
pass
12+
13+
def write(self, buf):
14+
self._write_buffer.write(buf)
15+
16+
def readline(self):
17+
return self._read_buffer.readline()
18+
19+
20+
class usocket:
21+
AF_INET = 2
22+
SOCK_STREAM = 1
23+
IPPROTO_TCP = 6
24+
25+
@staticmethod
26+
def getaddrinfo(host, port, af=0, type=0, flags=0):
27+
return [(usocket.AF_INET, usocket.SOCK_STREAM, usocket.IPPROTO_TCP, "", ("127.0.0.1", 80))]
28+
29+
def socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP):
30+
return Socket()
31+
32+
33+
sys.modules["usocket"] = usocket
34+
import requests
35+
36+
37+
def format_message(response):
38+
return response.raw._write_buffer.getvalue().decode('utf8')
39+
40+
41+
def test_simple_get():
42+
response = requests.request("GET", "http://example.com")
43+
44+
assert response.raw._write_buffer.getvalue() == (
45+
b"GET / HTTP/1.0\r\n"
46+
+ b"Connection: close\r\n"
47+
+ b"Host: example.com\r\n\r\n"), format_message(response)
48+
49+
50+
def test_get_auth():
51+
response = requests.request("GET", "http://example.com",
52+
auth=("test-username", "test-password"))
53+
54+
assert response.raw._write_buffer.getvalue() == (
55+
b"GET / HTTP/1.0\r\n"
56+
+ b"Host: example.com\r\n"
57+
+ b"Authorization: Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk\r\n"
58+
+ b"Connection: close\r\n\r\n"), format_message(response)
59+
60+
61+
def test_get_custom_header():
62+
response = requests.request("GET", "http://example.com",
63+
headers={"User-Agent": "test-agent"})
64+
65+
assert response.raw._write_buffer.getvalue() == (
66+
b"GET / HTTP/1.0\r\n"
67+
+ b"User-Agent: test-agent\r\n"
68+
+ b"Host: example.com\r\n"
69+
+ b"Connection: close\r\n\r\n"), format_message(response)
70+
71+
72+
def test_post_json():
73+
response = requests.request("GET", "http://example.com", json="test")
74+
75+
assert response.raw._write_buffer.getvalue() == (
76+
b"GET / HTTP/1.0\r\n"
77+
+ b"Connection: close\r\n"
78+
+ b"Content-Type: application/json\r\n"
79+
+ b"Host: example.com\r\n"
80+
+ b"Content-Length: 6\r\n\r\n"
81+
+ b"\"test\""), format_message(response)
82+
83+
84+
def test_post_chunked_data():
85+
def chunks():
86+
yield "test"
87+
88+
response = requests.request("GET", "http://example.com", data=chunks())
89+
90+
assert response.raw._write_buffer.getvalue() == (
91+
b"GET / HTTP/1.0\r\n"
92+
+ b"Transfer-Encoding: chunked\r\n"
93+
+ b"Host: example.com\r\n"
94+
+ b"Connection: close\r\n\r\n"
95+
+ b"4\r\ntest\r\n"
96+
+ b"0\r\n\r\n"), format_message(response)
97+
98+
99+
def test_overwrite_get_headers():
100+
response = requests.request("GET", "http://example.com",
101+
headers={"Connection": "keep-alive", "Host": "test.com"})
102+
103+
assert response.raw._write_buffer.getvalue() == (
104+
b"GET / HTTP/1.0\r\n"
105+
+ b"Host: test.com\r\n"
106+
+ b"Connection: keep-alive\r\n\r\n"), format_message(response)
107+
108+
109+
def test_overwrite_post_json_headers():
110+
response = requests.request("GET", "http://example.com",
111+
json="test",
112+
headers={"Content-Type": "text/plain", "Content-Length": "10"})
113+
114+
assert response.raw._write_buffer.getvalue() == (
115+
b"GET / HTTP/1.0\r\n"
116+
+ b"Connection: close\r\n"
117+
+ b"Content-Length: 10\r\n"
118+
+ b"Content-Type: text/plain\r\n"
119+
+ b"Host: example.com\r\n\r\n"
120+
+ b"\"test\""), format_message(response)
121+
122+
123+
def test_overwrite_post_chunked_data_headers():
124+
def chunks():
125+
yield "test"
126+
127+
response = requests.request("GET", "http://example.com",
128+
data=chunks(),
129+
headers={"Content-Length": "4"})
130+
131+
assert response.raw._write_buffer.getvalue() == (
132+
b"GET / HTTP/1.0\r\n"
133+
+ b"Host: example.com\r\n"
134+
+ b"Content-Length: 4\r\n"
135+
+ b"Connection: close\r\n\r\n"
136+
+ b"test"), format_message(response)
137+
138+
139+
test_simple_get()
140+
test_get_auth()
141+
test_get_custom_header()
142+
test_post_json()
143+
test_post_chunked_data()
144+
test_overwrite_get_headers()
145+
test_overwrite_post_json_headers()
146+
test_overwrite_post_chunked_data_headers()

0 commit comments

Comments
 (0)