def test_adapter_performs_a_successful_request_through_proxy(): request = PreparedRequest() request.prepare(url="http://somefakeurl", method="GET", headers={}) proxies = { "http": "http://localhost:8080", "https": "https://localhost:8081", } header_lines = [ b"HTTP/1.1 200 OK\n", b"Content-Language: en-US\n", ] pool = FakePool() pool.add_response(200, b"data obtained through proxy", header_lines) pool_provider = FakePoolProvider() pool_provider.add_pool_for_proxied_url("http://localhost:8080", request.url, pool) adapter = CURLAdapter( pool_provider_factory=lambda *args, **kwargs: pool_provider) response = adapter.send(request, proxies=proxies) assert response.status_code == 200 assert response.text == "data obtained through proxy" assert response.headers == {"Content-Language": "en-US"}
def test_adapter_clears_pool_provider_after_close(): pool_provider = FakePoolProvider() adapter = CURLAdapter( max_retries=1, pool_provider_factory=lambda *args, **kwargs: pool_provider) adapter.close() assert pool_provider.cleared
def test_adapter_translates_from_pycurl_errors(error_code, error_msg, expected_exception): request = PreparedRequest() request.prepare(url="http://somefakeurl", method="GET", headers={}) pool = FakePool() pool.add_exception(pycurl.error(error_code, error_msg)) pool_provider = FakePoolProvider() pool_provider.add_pool_for_url(request.url, pool) adapter = CURLAdapter( pool_provider_factory=lambda *args, **kwargs: pool_provider) with pytest.raises(expected_exception): adapter.send(request)
def test_adapter_reaches_max_retries_and_raises_exception(): request = PreparedRequest() request.prepare(url="http://somefakeurl", method="GET", headers={}) pool = FakePool() pool.add_exception(ConnectionError()) pool.add_exception(ReadTimeout()) pool_provider = FakePoolProvider() pool_provider.add_pool_for_url(request.url, pool) adapter = CURLAdapter( max_retries=1, pool_provider_factory=lambda *args, **kwargs: pool_provider) with pytest.raises(ReadTimeout): adapter.send(request)
def test_unauthorized_bearer_token_auth(): session = requests.Session() session.mount("http://", CURLAdapter()) url = f"{HTTP_BIN_BASE_URL}/bearer" response = session.get(url) assert response.status_code == 401
def test_adapter_performs_a_successful_request(): request = PreparedRequest() request.prepare(url="http://somefakeurl", method="GET", headers={}) header_lines = [ b"HTTP/1.1 200 OK\n", b"Content-Language: en-US\n", ] pool = FakePool() pool.add_response(200, b"somebodydata", header_lines) pool_provider = FakePoolProvider() pool_provider.add_pool_for_url(request.url, pool) adapter = CURLAdapter( pool_provider_factory=lambda *args, **kwargs: pool_provider) response = adapter.send(request) assert response.status_code == 200 assert response.text == "somebodydata" assert response.headers == {"Content-Language": "en-US"}
def test_successful_basic_auth(): session = requests.Session() session.mount("http://", CURLAdapter()) user = "******" pwd = "somepwd" url = f"{HTTP_BIN_BASE_URL}/basic-auth/{user}/{pwd}" response = session.get(url, auth=HTTPBasicAuth(user, pwd)) assert response.status_code == 200
def test_unauthorized_basic_auth(): session = requests.Session() session.mount("http://", CURLAdapter()) user = "******" pwd = "somepwd" url = f"{HTTP_BIN_BASE_URL}/basic-auth/{user}/{pwd}" response = session.get(url) assert response.status_code == 401
def test_successful_bearer_token_auth(): session = requests.Session() session.mount("http://", CURLAdapter()) token = "sometoken" url = f"{HTTP_BIN_BASE_URL}/bearer" headers = {"Authorization": f"Bearer {token}"} response = session.get(url, headers=headers) assert response.status_code == 200 assert response.json()["token"] == token
def test_http_codes(method, http_code): session = requests.Session() session_with_curl = requests.Session() session_with_curl.mount("http://", CURLAdapter()) url = f"{HTTP_BIN_BASE_URL}/status/{http_code}" response = getattr(session, method)(url) response_with_curl = getattr(session_with_curl, method)(url) headers = response.headers headers_with_curl = response_with_curl.headers # Since date may vary, we remove it from assert headers.pop("Date") headers_with_curl.pop("Date") assert response_with_curl.status_code == http_code assert headers_with_curl == headers assert response_with_curl.url == response.url assert response_with_curl.text == response.text
def test_headers(): session = requests.Session() session.mount("http://", CURLAdapter()) url = f"{HTTP_BIN_BASE_URL}/headers" headers_to_send = { "User-Agent": "my-app/0.0.1", "Dnt": "1", "Accept-Language": "en-US", "Cache-Control": "no-cache", "Date": "Tue, 15 Nov 1994 08:12:31 GMT", } response = session.get(url, headers=headers_to_send) headers_sent = response.json()["headers"] assert response.status_code == 200 for header, value in headers_to_send.items(): assert headers_sent.get(header) == value
def test_create_adapter_with_custom_retries(): adapter = CURLAdapter(max_retries=42) assert adapter.max_retries.total == 42
def test_create_adapter_with_default_retries(): adapter = CURLAdapter() assert adapter.max_retries.total == 0