-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_post.py
74 lines (54 loc) · 1.95 KB
/
test_post.py
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
import json
from urllib.parse import urlencode, quote_plus
import fastrequest.http
test_query_params = {"foo1": "bar1", "foo2": "bar2"}
test_headers = {
"X-FR-Test": "v1.2.6",
"X-Hello": "World"
}
test_json_payload = {
"test": 123,
"foo": {
"bar": 46,
"bar2": [3, 4, {}]
}
}
encode_f = lambda x: urlencode(x, quote_via=quote_plus)
def test_basic_post():
return fastrequest.http.post("https://postman-echo.com/post")
def test_with_urlparams():
res = fastrequest.http.post("https://postman-echo.com/post?" + encode_f(test_query_params))
assert(encode_f(res.json()["args"]) == encode_f(test_query_params))
return res
def test_with_payload_string():
res = fastrequest.http.post("https://postman-echo.com/post", encode_f(test_query_params))
assert(encode_f(res.json()["form"]) == encode_f(test_query_params))
return res
def test_with_payload_json():
res = fastrequest.http.post("https://postman-echo.com/post", test_json_payload)
assert(json.dumps(res.json()["json"]) == json.dumps(test_json_payload))
return res
def test_with_payload_custom():
return fastrequest.http.post("https://postman-echo.com/post", [1, 2, 3, ("q")])
def test_with_headers():
return fastrequest.http.post("https://postman-echo.com/post", "", test_headers)
def test_with_all():
return fastrequest.http.post("https://postman-echo.com/post", test_json_payload, test_headers)
if __name__ == "__main__":
tests = [
test_basic_post,
test_with_headers,
test_with_urlparams,
test_with_payload_string,
test_with_payload_json,
test_with_payload_custom,
test_with_all
]
for testfunc in tests:
print(".", end="", flush=True)
try:
testfunc()
except Exception as e:
print(f"\n[!!] Caught exception in '{testfunc.__name__}':", e)
exit(1)
print("\n[+] All tests passed!")