-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.py
More file actions
175 lines (133 loc) · 5.11 KB
/
tests.py
File metadata and controls
175 lines (133 loc) · 5.11 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
import datetime
import os
import pytest
from portale import (
PrefixedURLSession,
PrefixedURLSessionRequests,
pyreqwest_available,
)
from requests import exceptions as requests_exceptions
sessions = [PrefixedURLSessionRequests]
if pyreqwest_available:
from portale import PrefixedURLSessionPyreqwest
sessions.append(PrefixedURLSessionPyreqwest)
@pytest.fixture(params=sessions)
def session(request):
return request.param(
"https://eu.httpbin.org/",
headers={"Authorization": "Auth Token"},
cache_ttl=5,
)
def test_session_selection():
if pyreqwest_available:
assert PrefixedURLSession == PrefixedURLSessionPyreqwest
else:
assert PrefixedURLSession == PrefixedURLSessionRequests
def test_set_cache_ttl(session):
get_thing = session.GETJSONRequest("anything?thing={0}")
assert get_thing.cache_ttl == 5
get_thing = session.GETJSONRequest("anything?thing={0}", cache_ttl=10)
assert get_thing.cache_ttl == 10
get_thing = session.GETJSONRequest("anything?thing={0}", cache_ttl=0)
assert get_thing.cache_ttl == 0
res = get_thing("flask").json()
assert res["args"]["thing"] == "flask"
def test_params_in_url(session):
get_thing_by_name = session.GETRequest("anything?thing={name}")
resp = get_thing_by_name(name="snake")
assert {"thing": "snake"} == resp.json()["args"]
print(resp.json())
def test_passing_params(session):
get_thing_by_name = session.GETRequest("anything")
resp = get_thing_by_name(params={"thing": "boa"})
assert resp.url.endswith("?thing=boa")
assert {"thing": "boa"} == resp.json()["args"]
get_thing_by_name = session.POSTRequest("anything")
resp = get_thing_by_name(params={"thing": "boa"}, a=1)
assert resp.url.endswith("?thing=boa")
assert {"thing": "boa"} == resp.json()["args"]
def test_cache_request(session):
n = 2
cache_ttl = 20
long_request = session.GETJSONRequest("delay/{n}", cache_ttl=cache_ttl)
assert long_request.cache_ttl == cache_ttl
long_request.bust(
n=n
) # resetting cache to ensure we don't use previous run's data
then = datetime.datetime.now()
long_request(n=n)
now = datetime.datetime.now()
assert (now - then).seconds >= n
for i in range(9):
then = datetime.datetime.now()
long_request(n=n)
now = datetime.datetime.now()
assert (now - then).seconds < n
long_request.bust(n=n)
then = datetime.datetime.now()
long_request(n=n)
now = datetime.datetime.now()
assert (now - then).seconds >= n
assert long_request.cache.metrics["hits"]
def test_post_json(session):
post_req = session.POSTJSONRequest("anything")
data = {"a": 1, "b": 2}
resp = post_req(**data)
assert resp.json()["json"] == data
def test_url_subspost(session):
post_req = session.POSTJSONRequest("anything/{category}")
data = {"name": "The Tipping Point", "ISBN": " 0-316-34662-4"}
resp = post_req(category="books", **data)
assert resp.json()["json"] == data
def test_headers(session):
get_headers = session.GETRequest("headers")
resp = get_headers()
assert session.headers["Authorization"] == resp.json()["headers"]["Authorization"]
def test_response_wrapper(session):
get_thing = session.GETJSONRequest("anything?thing=test")
resp = get_thing()
assert resp.text is not None
assert resp.content is not None
assert resp.status_code == 200
assert resp.ok
resp.raise_for_status()
def test_raise_for_status(session):
get_thing = session.GETJSONRequest("status/404")
resp = get_thing()
assert not resp.ok
with pytest.raises(requests_exceptions.HTTPError):
resp.raise_for_status()
def test_patch_json(session):
patch_req = session.PATCHJSONRequest("patch")
data = {"a": 1, "b": 2}
resp = patch_req(**data)
assert resp.json()["json"] == data
def test_head_request(session):
head_req = session.HEADRequest("headers")
resp = head_req()
assert resp.status_code == 200
# HEAD requests should not have a body
assert not resp.content
def test_delete_request(session):
delete_req = session.DELETERequest("delete")
resp = delete_req()
assert resp.status_code == 200
def test_post_form_data(session):
post_req = session.POSTRequest("post")
data = {"a": 1, "b": 2}
resp = post_req(**data)
assert resp.json()["form"] == {"a": "1", "b": "2"}
if pyreqwest_available:
def test_pyreqwest_exception_wrapping():
session = PrefixedURLSessionPyreqwest("http://localhost:12345")
with pytest.raises(requests_exceptions.ConnectionError):
session.GETRequest("anything")()
session = PrefixedURLSessionPyreqwest(
"https://eu.httpbin.org/", timeout=0.1
)
with pytest.raises(requests_exceptions.Timeout):
session.GETRequest("delay/1")()
# As of pyreqwest 0.5.1, there isn't a direct way to trigger TooManyRedirects
# session = PrefixedURLSessionPyreqwest("https://eu.httpbin.org/")
# with pytest.raises(requests_exceptions.TooManyRedirects):
# session.GETRequest("redirect/10")()