Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 22fb311

Browse files
committed
Asyncify PoolManager redirect tests
1 parent 97cd3ec commit 22fb311

File tree

2 files changed

+191
-200
lines changed

2 files changed

+191
-200
lines changed

test/with_dummyserver/async/test_poolmanager.py

+191-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import io
2+
import json
23
import pytest
34
from ahip import PoolManager, Retry
4-
from ahip.exceptions import UnrewindableBodyError
5+
from ahip.exceptions import MaxRetryError, UnrewindableBodyError
56

67
from test.with_dummyserver import conftest
78
from dummyserver.testcase import HTTPDummyServerTestCase
89

10+
from test import LONG_TIMEOUT
11+
912

1013
class TestPoolManager(HTTPDummyServerTestCase):
1114
@classmethod
@@ -34,6 +37,193 @@ async def test_redirect(self, backend, anyio_backend):
3437
assert r.status == 200
3538
assert r.data == b"Dummy server!"
3639

40+
@conftest.test_all_backends
41+
async def test_redirect_twice(self, backend, anyio_backend):
42+
with PoolManager(backend=backend) as http:
43+
r = await http.request(
44+
"GET",
45+
"%s/redirect" % self.base_url,
46+
fields={"target": "%s/redirect" % self.base_url},
47+
redirect=False,
48+
)
49+
50+
assert r.status == 303
51+
52+
r = await http.request(
53+
"GET",
54+
"%s/redirect" % self.base_url,
55+
fields={
56+
"target": "%s/redirect?target=%s/" % (self.base_url, self.base_url)
57+
},
58+
)
59+
60+
assert r.status == 200
61+
assert r.data == b"Dummy server!"
62+
63+
@conftest.test_all_backends
64+
async def test_redirect_to_relative_url(self, backend, anyio_backend):
65+
with PoolManager(backend=backend) as http:
66+
r = await http.request(
67+
"GET",
68+
"%s/redirect" % self.base_url,
69+
fields={"target": "/redirect"},
70+
redirect=False,
71+
)
72+
73+
assert r.status == 303
74+
75+
r = await http.request(
76+
"GET", "%s/redirect" % self.base_url, fields={"target": "/redirect"}
77+
)
78+
79+
assert r.status == 200
80+
assert r.data == b"Dummy server!"
81+
82+
@conftest.test_all_backends
83+
async def test_cross_host_redirect(self, backend, anyio_backend):
84+
with PoolManager(backend=backend) as http:
85+
cross_host_location = "%s/echo?a=b" % self.base_url_alt
86+
with pytest.raises(MaxRetryError):
87+
await http.request(
88+
"GET",
89+
"%s/redirect" % self.base_url,
90+
fields={"target": cross_host_location},
91+
timeout=LONG_TIMEOUT,
92+
retries=0,
93+
)
94+
95+
r = await http.request(
96+
"GET",
97+
"%s/redirect" % self.base_url,
98+
fields={"target": "%s/echo?a=b" % self.base_url_alt},
99+
timeout=LONG_TIMEOUT,
100+
retries=1,
101+
)
102+
103+
assert r._pool.host == self.host_alt
104+
105+
@conftest.test_all_backends
106+
async def test_too_many_redirects(self, backend, anyio_backend):
107+
with PoolManager(backend=backend) as http:
108+
with pytest.raises(MaxRetryError):
109+
await http.request(
110+
"GET",
111+
"%s/redirect" % self.base_url,
112+
fields={
113+
"target": "%s/redirect?target=%s/"
114+
% (self.base_url, self.base_url)
115+
},
116+
retries=1,
117+
)
118+
119+
with pytest.raises(MaxRetryError):
120+
await http.request(
121+
"GET",
122+
"%s/redirect" % self.base_url,
123+
fields={
124+
"target": "%s/redirect?target=%s/"
125+
% (self.base_url, self.base_url)
126+
},
127+
retries=Retry(total=None, redirect=1),
128+
)
129+
130+
@conftest.test_all_backends
131+
async def test_redirect_cross_host_remove_headers(self, backend, anyio_backend):
132+
with PoolManager(backend=backend) as http:
133+
r = await http.request(
134+
"GET",
135+
"%s/redirect" % self.base_url,
136+
fields={"target": "%s/headers" % self.base_url_alt},
137+
headers={"Authorization": "foo"},
138+
)
139+
140+
assert r.status == 200
141+
142+
data = json.loads(r.data.decode("utf-8"))
143+
144+
assert "Authorization" not in data
145+
146+
r = await http.request(
147+
"GET",
148+
"%s/redirect" % self.base_url,
149+
fields={"target": "%s/headers" % self.base_url_alt},
150+
headers={"authorization": "foo"},
151+
)
152+
153+
assert r.status == 200
154+
155+
data = json.loads(r.data.decode("utf-8"))
156+
157+
assert "authorization" not in data
158+
assert "Authorization" not in data
159+
160+
@conftest.test_all_backends
161+
async def test_redirect_cross_host_no_remove_headers(self, backend, anyio_backend):
162+
with PoolManager(backend=backend) as http:
163+
r = await http.request(
164+
"GET",
165+
"%s/redirect" % self.base_url,
166+
fields={"target": "%s/headers" % self.base_url_alt},
167+
headers={"Authorization": "foo"},
168+
retries=Retry(remove_headers_on_redirect=[]),
169+
)
170+
171+
assert r.status == 200
172+
173+
data = json.loads(r.data.decode("utf-8"))
174+
175+
assert data["Authorization"] == "foo"
176+
177+
@conftest.test_all_backends
178+
async def test_redirect_cross_host_set_removed_headers(
179+
self, backend, anyio_backend
180+
):
181+
with PoolManager(backend=backend) as http:
182+
r = await http.request(
183+
"GET",
184+
"%s/redirect" % self.base_url,
185+
fields={"target": "%s/headers" % self.base_url_alt},
186+
headers={"X-API-Secret": "foo", "Authorization": "bar"},
187+
retries=Retry(remove_headers_on_redirect=["X-API-Secret"]),
188+
)
189+
190+
assert r.status == 200
191+
192+
data = json.loads(r.data.decode("utf-8"))
193+
194+
assert "X-API-Secret" not in data
195+
assert data["Authorization"] == "bar"
196+
197+
r = await http.request(
198+
"GET",
199+
"%s/redirect" % self.base_url,
200+
fields={"target": "%s/headers" % self.base_url_alt},
201+
headers={"x-api-secret": "foo", "authorization": "bar"},
202+
retries=Retry(remove_headers_on_redirect=["X-API-Secret"]),
203+
)
204+
205+
assert r.status == 200
206+
207+
data = json.loads(r.data.decode("utf-8"))
208+
209+
assert "x-api-secret" not in data
210+
assert "X-API-Secret" not in data
211+
assert data["Authorization"] == "bar"
212+
213+
@conftest.test_all_backends
214+
async def test_raise_on_redirect(self, backend, anyio_backend):
215+
with PoolManager(backend=backend) as http:
216+
r = await http.request(
217+
"GET",
218+
"%s/redirect" % self.base_url,
219+
fields={
220+
"target": "%s/redirect?target=%s/" % (self.base_url, self.base_url)
221+
},
222+
retries=Retry(total=None, redirect=1, raise_on_redirect=False),
223+
)
224+
225+
assert r.status == 303
226+
37227

38228
class TestFileUploads(HTTPDummyServerTestCase):
39229
@classmethod

0 commit comments

Comments
 (0)