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