Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
269 lines (209 loc) · 8.04 KB
/
Copy pathtest.py
File metadata and controls
269 lines (209 loc) · 8.04 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import json
import requests
def test_hello_world():
response = requests.get("http://localhost:8000/hello")
assert response.ok
assert response.content.decode("utf-8") == "Hello, world!"
def test_global_middleware():
response = requests.get("http://localhost:8000/global_middleware")
assert response.ok
assert response.content.decode("utf-8") == "True"
def test_global_and_router_middleware():
response = requests.get("http://localhost:8000/router/router_middleware")
assert response.ok
assert response.json() == {"global": True, "router": True}
def test_rejecting_middleware():
name = "adhom"
response = requests.get(f"http://localhost:8000/unauthorized")
assert not response.ok
assert response.status_code == 401
def test_dynamic_routing():
name = "adhom"
response = requests.get(f"http://localhost:8000/greet/{name}")
assert response.ok
assert response.content.decode("utf-8") == f"Greetings, {name}"
def test_dynamic_route_doesnt_save_other_params():
response = requests.get(f"http://localhost:8000/test1/ayo/test1")
assert response.ok
assert response.json() == {"test1": "ayo"}
response = requests.get(f"http://localhost:8000/test1/aloha/test1")
assert response.ok
assert response.json() == {"test1": "aloha"}
response = requests.get(f"http://localhost:8000/test1/oya/test2")
assert response.ok
assert response.json() == {"test2": "oya"}
response = requests.get(f"http://localhost:8000/test1/ayo")
assert response.ok
assert response.json() == {"test1": "ayo"}
response = requests.get(f"http://localhost:8000/test2/oya")
assert response.ok
assert response.json() == {"test2": "oya"}
def test_router():
response = requests.get("http://localhost:8000/router/test")
assert response.ok
assert response.content.decode("utf-8") == "test"
def test_dynamic_routing_for_router():
response = requests.get("http://localhost:8000/router/dynamic/test")
assert response.ok
assert response.content.decode("utf-8") == "dynamic"
def test_post_request_json():
item = {"name": "RTX 3090", "price": "4000"}
response = requests.post(f"http://localhost:8000/items", json=item)
assert response.ok
assert json.loads(response.content.decode("utf-8")) == item
def test_html_response():
response = requests.get("http://localhost:8000/html")
assert response.ok
assert response.headers["content-type"] == "text/html; charset=UTF-8"
def test_status_code_400():
response = requests.get("http://localhost:8000/400")
assert response.status_code == 400
def test_put_request():
item = {"hello": "world"}
response = requests.put("http://localhost:8000/put", json=item)
assert response.ok
assert json.loads(response.content.decode("utf-8")) == item
def test_patch_request():
item = {"hello": "world"}
response = requests.patch("http://localhost:8000/patch", json=item)
assert response.ok
assert json.loads(response.content.decode("utf-8")) == item
def test_delete_request():
response = requests.delete("http://localhost:8000/delete")
assert response.ok
def test_head_request():
response = requests.head("http://localhost:8000/head")
assert response.ok
def test_options_request():
response = requests.options("http://localhost:8000/options")
assert response.ok
def test_middleware():
user = {"username": "adhom", "email": "adhom@adhom.com"}
response = requests.post("http://localhost:8000/middleware", json=user)
assert response.ok
assert json.loads(response.content.decode("utf-8")) == user
def test_get_query_request():
response = requests.get(f"http://localhost:8000/query?q=cats&p=dogs&tags=a&tags=b")
assert response.ok
assert json.loads(response.content.decode("utf-8")) == {
"q": "cats",
"p": "dogs",
"tags": ["a", "b"],
}
def test_dependency_injection():
item = {"name": "RTX 3090", "price": 3000.50}
response = requests.post("http://localhost:8000/dependency-injection", json=item)
assert response.ok
assert json.loads(response.content.decode("utf-8")) == item
def test_dependency_injection_error():
item = {"name": "RTX 3090", "price": 3000.50}
response = requests.post(
"http://localhost:8000/dependency-injection-error", json=item
)
assert not response.ok
assert response.status_code == 403
assert response.json() == {
"errors": [
{
"loc": ["active"],
"msg": "Body field active is required to be of type bool but it's missing",
}
]
}
def test_router_get():
response = requests.get("http://localhost:8000/router/test")
assert response.ok
assert response.text == "test"
def test_router_post():
response = requests.post("http://localhost:8000/router/test")
assert response.ok
assert response.text == "test"
def test_not_found():
response = requests.get("http://localhost:8000/non-exisiting-route")
assert not response.ok
assert response.status_code == 404
assert response.json() == {"errors": [{"loc": ["request"], "msg": "404 Not Found"}]}
def test_set_cookies():
response = requests.get("http://localhost:8000/set-cookie")
assert response.ok
assert response.cookies.items() == [
("key1", "value1"),
("key2", "value2"),
("key3", "value3"),
]
def test_reading_cookies():
cookies = {"name": "adhom", "age": "23"}
response = requests.get("http://localhost:8000/set-cookie", cookies=cookies)
assert response.ok
assert response.json() == cookies
# ! Test cookies whose value contain a "="
cookies = {"jwt": "sdasdasdasd=="}
response = requests.get("http://localhost:8000/set-cookie", cookies=cookies)
assert response.ok
assert response.json() == cookies
def test_set_headers():
response = requests.get("http://localhost:8000/set-headers")
assert response.ok
headers = [
("header1", "value1"),
("header2", "value2"),
("header3", "value3"),
]
for k, v in headers:
assert k in response.headers.keys()
assert v in response.headers.values()
assert response.headers[k] == v
def test_reading_headers():
headers = {"name": "adhom", "age": "23"}
response = requests.get("http://localhost:8000/set-headers", headers=headers)
assert response.ok
json_response = response.json()
for k, v in headers.items():
assert k in json_response.keys()
assert v in json_response.values()
assert json_response[k] == v
def test_query_type_hints():
response = requests.get("http://localhost:8000/query-type-hint?")
assert not response.ok
assert response.status_code == 403
json_response = response.json()
assert json_response == {
"errors": [
{
"loc": ["age"],
"msg": f"Query paramater age is required",
}
]
}
response = requests.get("http://localhost:8000/query-type-hint?age=69")
assert response.ok
json_response = response.json()
assert json_response == {"name": "adhom", "age": "69"}
response = requests.get(
"http://localhost:8000/query-type-hint?name=sasa&age=69&age=420"
)
assert response.ok
json_response = response.json()
assert json_response == {"name": "sasa", "age": ["69", "420"]}
def test_pydantic_validation():
# bad body
body = {"item": {"name": "adhom", "price": "not a float"}}
response = requests.post("http://localhost:8000/pydantic", json=body)
print(response.content)
assert not response.ok
json_response = response.json()
assert json_response == {
"errors": [
{
"loc": ["price"],
"msg": "value is not a valid float",
"type": "type_error.float",
}
]
}
body["item"]["price"] = 22.5 # type: ignore
response = requests.post("http://localhost:8000/pydantic", json=body)
print(response.content)
assert response.ok
json_response = response.json()
assert json_response == body