|
1 | 1 | import io
|
2 | 2 | import json
|
3 | 3 | import pytest
|
| 4 | +from ahip.base import DEFAULT_PORTS |
4 | 5 | from ahip import PoolManager, Retry
|
5 | 6 | from ahip.exceptions import MaxRetryError, UnrewindableBodyError
|
6 | 7 |
|
@@ -224,6 +225,153 @@ async def test_raise_on_redirect(self, backend, anyio_backend):
|
224 | 225 |
|
225 | 226 | assert r.status == 303
|
226 | 227 |
|
| 228 | + @conftest.test_all_backends |
| 229 | + async def test_raise_on_status(self, backend, anyio_backend): |
| 230 | + with PoolManager(backend=backend) as http: |
| 231 | + with pytest.raises(MaxRetryError): |
| 232 | + # the default is to raise |
| 233 | + r = await http.request( |
| 234 | + "GET", |
| 235 | + "%s/status" % self.base_url, |
| 236 | + fields={"status": "500 Internal Server Error"}, |
| 237 | + retries=Retry(total=1, status_forcelist=range(500, 600)), |
| 238 | + ) |
| 239 | + |
| 240 | + with pytest.raises(MaxRetryError): |
| 241 | + # raise explicitly |
| 242 | + r = await http.request( |
| 243 | + "GET", |
| 244 | + "%s/status" % self.base_url, |
| 245 | + fields={"status": "500 Internal Server Error"}, |
| 246 | + retries=Retry( |
| 247 | + total=1, status_forcelist=range(500, 600), raise_on_status=True |
| 248 | + ), |
| 249 | + ) |
| 250 | + |
| 251 | + # don't raise |
| 252 | + r = await http.request( |
| 253 | + "GET", |
| 254 | + "%s/status" % self.base_url, |
| 255 | + fields={"status": "500 Internal Server Error"}, |
| 256 | + retries=Retry( |
| 257 | + total=1, status_forcelist=range(500, 600), raise_on_status=False |
| 258 | + ), |
| 259 | + ) |
| 260 | + |
| 261 | + assert r.status == 500 |
| 262 | + |
| 263 | + @pytest.mark.parametrize( |
| 264 | + ["target", "expected_target"], |
| 265 | + [ |
| 266 | + ("/echo_uri?q=1#fragment", b"/echo_uri?q=1"), |
| 267 | + ("/echo_uri?#", b"/echo_uri?"), |
| 268 | + ("/echo_uri#?", b"/echo_uri"), |
| 269 | + ("/echo_uri#?#", b"/echo_uri"), |
| 270 | + ("/echo_uri??#", b"/echo_uri??"), |
| 271 | + ("/echo_uri?%3f#", b"/echo_uri?%3F"), |
| 272 | + ("/echo_uri?%3F#", b"/echo_uri?%3F"), |
| 273 | + ("/echo_uri?[]", b"/echo_uri?%5B%5D"), |
| 274 | + ], |
| 275 | + ) |
| 276 | + @conftest.test_all_backends |
| 277 | + async def test_encode_http_target( |
| 278 | + self, target, expected_target, backend, anyio_backend |
| 279 | + ): |
| 280 | + with PoolManager() as http: |
| 281 | + url = "http://%s:%d%s" % (self.host, self.port, target) |
| 282 | + r = await http.request("GET", url) |
| 283 | + assert r.data == expected_target |
| 284 | + |
| 285 | + @conftest.test_all_backends |
| 286 | + async def test_missing_port(self, backend, anyio_backend): |
| 287 | + # Can a URL that lacks an explicit port like ':80' succeed, or |
| 288 | + # will all such URLs fail with an error? |
| 289 | + |
| 290 | + with PoolManager(backend=backend) as http: |
| 291 | + # By globally adjusting `DEFAULT_PORTS` we pretend for a moment |
| 292 | + # that HTTP's default port is not 80, but is the port at which |
| 293 | + # our test server happens to be listening. |
| 294 | + DEFAULT_PORTS["http"] = self.port |
| 295 | + try: |
| 296 | + r = await http.request("GET", "http://%s/" % self.host, retries=0) |
| 297 | + finally: |
| 298 | + DEFAULT_PORTS["http"] = 80 |
| 299 | + |
| 300 | + assert r.status == 200 |
| 301 | + assert r.data == b"Dummy server!" |
| 302 | + |
| 303 | + @conftest.test_all_backends |
| 304 | + async def test_headers(self, backend, anyio_backend): |
| 305 | + with PoolManager(backend=backend, headers={"Foo": "bar"}) as http: |
| 306 | + r = await http.request("GET", "%s/headers" % self.base_url) |
| 307 | + returned_headers = json.loads(r.data.decode()) |
| 308 | + assert returned_headers.get("Foo") == "bar" |
| 309 | + |
| 310 | + r = await http.request("POST", "%s/headers" % self.base_url) |
| 311 | + returned_headers = json.loads(r.data.decode()) |
| 312 | + assert returned_headers.get("Foo") == "bar" |
| 313 | + |
| 314 | + r = await http.request_encode_url("GET", "%s/headers" % self.base_url) |
| 315 | + returned_headers = json.loads(r.data.decode()) |
| 316 | + assert returned_headers.get("Foo") == "bar" |
| 317 | + |
| 318 | + r = await http.request_encode_body("POST", "%s/headers" % self.base_url) |
| 319 | + returned_headers = json.loads(r.data.decode()) |
| 320 | + assert returned_headers.get("Foo") == "bar" |
| 321 | + |
| 322 | + r = await http.request_encode_url( |
| 323 | + "GET", "%s/headers" % self.base_url, headers={"Baz": "quux"} |
| 324 | + ) |
| 325 | + returned_headers = json.loads(r.data.decode()) |
| 326 | + assert returned_headers.get("Foo") is None |
| 327 | + assert returned_headers.get("Baz") == "quux" |
| 328 | + |
| 329 | + r = await http.request_encode_body( |
| 330 | + "GET", "%s/headers" % self.base_url, headers={"Baz": "quux"} |
| 331 | + ) |
| 332 | + returned_headers = json.loads(r.data.decode()) |
| 333 | + assert returned_headers.get("Foo") is None |
| 334 | + assert returned_headers.get("Baz") == "quux" |
| 335 | + |
| 336 | + @conftest.test_all_backends |
| 337 | + async def test_http_with_ssl_keywords(self, backend, anyio_backend): |
| 338 | + with PoolManager(backend=backend, ca_certs="REQUIRED") as http: |
| 339 | + r = await http.request("GET", "http://%s:%s/" % (self.host, self.port)) |
| 340 | + assert r.status == 200 |
| 341 | + |
| 342 | + @conftest.test_all_backends |
| 343 | + async def test_http_with_ca_cert_dir(self, backend, anyio_backend): |
| 344 | + with PoolManager( |
| 345 | + backend=backend, ca_certs="REQUIRED", ca_cert_dir="/nosuchdir" |
| 346 | + ) as http: |
| 347 | + r = await http.request("GET", "http://%s:%s/" % (self.host, self.port)) |
| 348 | + assert r.status == 200 |
| 349 | + |
| 350 | + @conftest.test_all_backends |
| 351 | + async def test_cleanup_on_connection_error(self, backend, anyio_backend): |
| 352 | + """ |
| 353 | + Test that connections are recycled to the pool on |
| 354 | + connection errors where no http response is received. |
| 355 | + """ |
| 356 | + poolsize = 3 |
| 357 | + |
| 358 | + with PoolManager(backend=backend, maxsize=poolsize, block=True) as http: |
| 359 | + pool = http.connection_from_host(self.host, self.port) |
| 360 | + assert pool.pool.qsize() == poolsize |
| 361 | + |
| 362 | + # force a connection error by supplying a non-existent |
| 363 | + # url. We won't get a response for this and so the |
| 364 | + # conn won't be implicitly returned to the pool. |
| 365 | + url = "%s/redirect" % self.base_url |
| 366 | + with pytest.raises(MaxRetryError): |
| 367 | + await http.request("GET", url, fields={"target": "/"}, retries=0) |
| 368 | + |
| 369 | + r = await http.request("GET", url, fields={"target": "/"}, retries=1) |
| 370 | + r.release_conn() |
| 371 | + |
| 372 | + # the pool should still contain poolsize elements |
| 373 | + assert pool.pool.qsize() == poolsize |
| 374 | + |
227 | 375 |
|
228 | 376 | class TestFileUploads(HTTPDummyServerTestCase):
|
229 | 377 | @classmethod
|
|
0 commit comments