def test_callback_with_pattern_in_url(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: return httpx.Response( status_code=200, http_version="HTTP/1.1", headers=[], stream=content_streams.JSONStream({"url": str(request.url)}), request=request, ) def custom_response2(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: return httpx.Response( status_code=200, http_version="HTTP/2.0", headers=[], stream=content_streams.JSONStream({"url": str(request.url)}), request=request, ) httpx_mock.add_callback(custom_response, url=re.compile(".*test.*")) httpx_mock.add_callback(custom_response2, url="http://unmatched") with httpx.Client() as client: response = client.get("http://unmatched") assert response.http_version == "HTTP/2.0" response = client.get("http://test_url") assert response.http_version == "HTTP/1.1"
async def test_callback_returning_response(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, *args, **kwargs): return to_response(json={"url": str(request.url)},) httpx_mock.add_callback(custom_response, url="http://test_url") async with httpx.AsyncClient() as client: response = await client.get("http://test_url") assert response.json() == {"url": "http://test_url"}
async def test_callback_raising_exception(httpx_mock: HTTPXMock): def raise_timeout(*args, **kwargs): raise httpx.ReadTimeout() httpx_mock.add_callback(raise_timeout, url="http://test_url") async with httpx.AsyncClient() as client: with pytest.raises(httpx.ReadTimeout): await client.get("http://test_url")
def test_callback_raising_exception(httpx_mock: HTTPXMock): def raise_timeout(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: raise httpx.exceptions.TimeoutException() httpx_mock.add_callback(raise_timeout, url="http://test_url") with httpx.Client() as client: with pytest.raises(httpx.exceptions.TimeoutException): client.get("http://test_url")
async def test_callback_matching_method(httpx_mock: HTTPXMock): def custom_response(*args, **kwargs): return to_response(json=["content"]) httpx_mock.add_callback(custom_response, method="GET") async with httpx.AsyncClient() as client: response = await client.get("http://test_url") assert response.json() == ["content"] response = await client.get("http://test_url2") assert response.json() == ["content"]
async def test_callback_executed_twice(httpx_mock: HTTPXMock): def custom_response(*args, **kwargs): return to_response(json=["content"],) httpx_mock.add_callback(custom_response) async with httpx.AsyncClient() as client: response = await client.get("http://test_url") assert response.json() == ["content"] response = await client.post("http://test_url") assert response.json() == ["content"]
async def test_callback_with_pattern_in_url(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, *args, **kwargs): return to_response(json={"url": str(request.url)}) def custom_response2(request: httpx.Request, *args, **kwargs): return to_response(http_version="HTTP/2.0", json={"url": str(request.url)}) httpx_mock.add_callback(custom_response, url=re.compile(".*test.*")) httpx_mock.add_callback(custom_response2, url="http://unmatched") async with httpx.AsyncClient() as client: response = await client.get("http://unmatched") assert response.http_version == "HTTP/2.0" response = await client.get("http://test_url") assert response.http_version == "HTTP/1.1"
def test_callback_returning_response(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: return httpx.Response( status_code=200, http_version="HTTP/1.1", headers=[], stream=content_streams.JSONStream({"url": str(request.url)}), request=request, ) httpx_mock.add_callback(custom_response, url="http://test_url") with httpx.Client() as client: response = client.get("http://test_url") assert response.json() == {"url": "http://test_url"}
def test_callback_matching_method(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: return httpx.Response( status_code=200, http_version="HTTP/1.1", headers=[], stream=content_streams.JSONStream(["content"]), request=request, ) httpx_mock.add_callback(custom_response, method="GET") with httpx.Client() as client: response = client.get("http://test_url") assert response.json() == ["content"] response = client.get("http://test_url2") assert response.json() == ["content"]
async def test_callback_executed_twice(httpx_mock: HTTPXMock): def custom_response(request: httpx.Request, timeout: Optional[httpx.Timeout], *args, **kwargs) -> httpx.Response: return httpx.Response( status_code=200, http_version="HTTP/1.1", headers=[], stream=content_streams.JSONStream(["content"]), request=request, ) httpx_mock.add_callback(custom_response) async with httpx.AsyncClient() as client: response = await client.get("http://test_url") assert response.json() == ["content"] response = await client.post("http://test_url") assert response.json() == ["content"]
def test_custom_failure_status_exception_health_check( mock_http_health_datetime, httpx_mock: HTTPXMock ): def send_failure(request, *args, **kwargs): raise httpx.NetworkError("", request=request) httpx_mock.add_callback( url="http://test/health", method="GET", callback=send_failure ) assert healthpy.httpx.check( "tests", "http://test/health", error_status_extracting=lambda x: x.trigger_exception, ) == ( "fail", { "tests:health": { "componentType": "http://test/health", "output": "", "status": "fail", "time": "2018-10-11T15:05:05.663979", } }, )
def test_httpx_mock_unused_callback(httpx_mock: HTTPXMock): def unused(*args, **kwargs): pass httpx_mock.add_callback(unused)