def test_raise_known_save_errors_2(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=400, data=b"Feeds are required") with pytest.raises(FeedsRequiredError): fake_request_with_raise_known_save_errors() assert httpx_mock.get_requests()
def test_none_on_400_ALREADY_ARCHIVED_2(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=401, data=b"ALREADY_ARCHIVED") with pytest.raises(httpx.HTTPStatusError): fake_request_with_none_on_400_ALREADY_ARCHIVED() assert httpx_mock.get_requests()
def test_none_on_404_1(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=404) res = fake_request_with_none_on_404_deco() assert httpx_mock.get_requests() assert res is None
def test_none_on_400_ALREADY_ARCHIVED_1(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=400, data=b"ALREADY_ARCHIVED") res = fake_request_with_none_on_400_ALREADY_ARCHIVED() assert httpx_mock.get_requests() assert res is None
def test_none_on_code_and_message_3(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=403, data=b"pas le mot dedans") with pytest.raises(httpx.HTTPStatusError): fake_request_with_none_on_code_and_message() assert httpx_mock.get_requests()
def test_none_on_code_and_message_1(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=403, data=b"message") res = fake_request_with_none_on_code_and_message() assert httpx_mock.get_requests() assert res is None
def test_none_on_http_codes_2(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=401) with pytest.raises(httpx.HTTPStatusError): fake_request_with_400_decorator() assert httpx_mock.get_requests()
def test_raise_known_save_errors_1(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=400, data=b"URL_ALREADY_EXISTS") with pytest.raises(UrlAlreadyExistsError): fake_request_with_raise_known_save_errors() assert httpx_mock.get_requests()
def test_none_on_400_SUBSCRIPTION_ALREADY_EXISTS_OR_PINNED_3( httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=401, data=b"Already pinned") with pytest.raises(httpx.HTTPStatusError): fake_request_with_none_on_400_SUBSCRIPTION_ALREADY_EXISTS_OR_PINNED() assert httpx_mock.get_requests()
def test_none_on_400_SUBSCRIPTION_ALREADY_EXISTS_OR_PINNED_2( httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=400, data=b"Already pinned") res = fake_request_with_none_on_400_SUBSCRIPTION_ALREADY_EXISTS_OR_PINNED() assert httpx_mock.get_requests() assert res is None
def test_retry_on_http_codes(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=404) with pytest.raises(httpx.HTTPStatusError): fake_request_retry_on_http_codes() requests = httpx_mock.get_requests() assert requests assert len(requests) == max_attempts
def test_default_requests_retrieval(httpx_mock: HTTPXMock): httpx_mock.add_response() with httpx.Client() as client: client.post("http://test_url", headers={"X-TEST": "test header 1"}) client.get("http://test_url2", headers={"X-TEST": "test header 2"}) requests = httpx_mock.get_requests() assert len(requests) == 2 assert requests[0].headers["x-test"] == "test header 1" assert requests[1].headers["x-test"] == "test header 2"
def test_requests_retrieval_on_same_url(httpx_mock: HTTPXMock): httpx_mock.add_response(url="http://test_url") with httpx.Client() as client: client.get("http://test_url", headers={"X-TEST": "test header 1"}) client.get("http://test_url", headers={"X-TEST": "test header 2"}) requests = httpx_mock.get_requests(url=httpx.URL("http://test_url")) assert len(requests) == 2 assert requests[0].headers["x-test"] == "test header 1" assert requests[1].headers["x-test"] == "test header 2"
async def test_requests_retrieval_on_same_method(httpx_mock: HTTPXMock): httpx_mock.add_response() async with httpx.AsyncClient() as client: await client.get("http://test_url", headers={"X-TEST": "test header 1"}) await client.get("http://test_url2", headers={"X-TEST": "test header 2"}) requests = httpx_mock.get_requests(method="GET") assert len(requests) == 2 assert requests[0].headers["x-test"] == "test header 1" assert requests[1].headers["x-test"] == "test header 2"
async def test_push_realistic_payload(fake_async_fcm_client_w_creds, fake_device_token, httpx_mock: HTTPXMock): creds = fake_async_fcm_client_w_creds._credentials httpx_mock.add_response( status_code=200, json={ "name": f"projects/{creds.project_id}/messages/0:1612788010922733%7606eb247606eb24" }) apns_config: APNSConfig = fake_async_fcm_client_w_creds.build_apns_config( priority="normal", apns_topic="Your bucket has been updated", collapse_key="BUCKET_UPDATED", badge=1, category="CATEGORY_BUCKET_UPDATED", custom_data={ "bucket_name": "3bc56ff12a", "bucket_link": "/link/to/bucket/3bc56ff12a", "aliases": ["happy_friends", "mobile_groups"], "updated_count": 1, }, mutable_content=True, content_available=True, ) with patch("google.oauth2.service_account.Credentials.refresh", fake_google_refresh): await fake_async_fcm_client_w_creds.push( device_token=fake_device_token, apns=apns_config) request_payload = json.loads(httpx_mock.get_requests()[0].read()) assert request_payload == { "message": { "apns": { "headers": apns_config.headers, "payload": { "aps": { "badge": 1, "category": "CATEGORY_BUCKET_UPDATED", "content-available": True, "mutable-content": True }, "bucket_name": "3bc56ff12a", "bucket_link": "/link/to/bucket/3bc56ff12a", "aliases": ["happy_friends", "mobile_groups"], "updated_count": 1, } }, "token": fake_device_token }, "validate_only": False }
def test_requests_with_pattern_in_url(httpx_mock: HTTPXMock): httpx_mock.add_response(url="http://test_url") httpx_mock.add_response(url="http://tests_url") httpx_mock.add_response(url="http://unmatched") with httpx.Client() as client: client.get("http://tests_url", headers={"X-Test": "1"}) client.get("http://unmatched", headers={"X-Test": "2"}) client.get("http://test_url") requests = httpx_mock.get_requests(url=re.compile(".*test.*")) assert len(requests) == 2 assert requests[0].headers["x-test"] == "1" assert "x-test" not in requests[1].headers
def test_none_on_http_codes_1(httpx_mock: HTTPXMock): httpx_mock.add_response(status_code=400) res = fake_request_with_400_decorator() assert httpx_mock.get_requests() assert res is None