async def test_pass_through(client, parameters, expected): async with respx.HTTPXMock() as httpx_mock: request = httpx_mock.request(**parameters) with asynctest.mock.patch( "asyncio.open_connection", side_effect=ConnectionRefusedError("test request blocked"), ) as open_connection: with pytest.raises(NetworkError): await client.get("https://example.org/") assert open_connection.called is True assert request.called is True assert request.pass_through is expected httpx_mock.reset() with asynctest.mock.patch( "urllib3.PoolManager.urlopen", side_effect=SSLError("test request blocked"), ) as urlopen: with pytest.raises(NetworkError): httpx.get("https://example.org/") assert urlopen.called is True assert request.called is True assert request.pass_through is expected
async def test_url_match(client, url): async with respx.HTTPXMock(assert_all_mocked=False) as httpx_mock: request = httpx_mock.get(url, content="baz") response = await client.get("https://foo.bar/baz/") assert request.called is True assert response.status_code == 200 assert response.text == "baz"
async def test_assert_all_mocked(client, assert_all_mocked, raises): with raises: async with respx.HTTPXMock(assert_all_mocked=assert_all_mocked) as httpx_mock: response = await client.get("https://foo.bar/") assert httpx_mock.stats.call_count == 1 assert response.status_code == 200 assert httpx_mock.stats.call_count == 0
async def test_request_callback(client): def callback(request, response): if request.url.host == "foo.bar": response.headers["X-Foo"] = "bar" response.content = lambda request, name: f"hello {name}" response.context["name"] = "lundberg" return response async with respx.HTTPXMock(assert_all_called=False) as httpx_mock: request = httpx_mock.request( callback, status_code=202, headers={"X-Ham": "spam"} ) response = await client.get("https://foo.bar/") assert request.called is True assert request.pass_through is None assert response.status_code == 202 assert response.headers == httpx.Headers( {"Content-Type": "text/plain", "X-Ham": "spam", "X-Foo": "bar"} ) assert response.text == "hello lundberg" with pytest.raises(ValueError): httpx_mock.request(lambda req, res: "invalid") await client.get("https://ham.spam/")
async def test_headers(client, headers, content_type, expected): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" request = httpx_mock.get(url, content_type=content_type, headers=headers) response = await client.get(url) assert request.called is True assert response.headers == httpx.Headers(expected)
async def test_text_content(client, content, expected): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" content_type = "text/plain; charset=utf-8" # TODO: Remove once respected request = httpx_mock.post(url, content=content, content_type=content_type) response = await client.post(url) assert request.called is True assert response.text == expected
async def test_status_code(client): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" request = httpx_mock.get(url, status_code=404) response = await client.get(url) assert request.called is True assert response.status_code == 404
async def test_alias(): async with respx.HTTPXMock(assert_all_called=False) as httpx_mock: url = "https://foo.bar/" request = httpx_mock.get(url, alias="foobar") assert "foobar" not in respx.aliases assert "foobar" in httpx_mock.aliases assert httpx_mock.aliases["foobar"].url == request.url assert httpx_mock["foobar"].url == request.url
def test_deprecated(): url = "https://foo.bar/" with respx.mock: respx.request("POST", url, status_code=201) with respx.HTTPXMock() as httpx_mock: httpx_mock.request("GET", url) response = httpx.post(url) assert response.status_code == 201 response = httpx.get(url) assert response.status_code == 200
async def test_raising_content(client): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" request = httpx_mock.get(url, content=httpx.ConnectTimeout()) with pytest.raises(httpx.ConnectTimeout): await client.get(url) assert request.called is True _request, _response = request.calls[-1] assert _request is not None assert _response is None
async def test_assert_all_called(client, assert_all_called, do_post, raises): with raises: async with respx.HTTPXMock(assert_all_called=assert_all_called) as httpx_mock: request1 = httpx_mock.get("https://foo.bar/1/", status_code=404) request2 = httpx_mock.post("https://foo.bar/", status_code=201) await client.get("https://foo.bar/1/") if do_post: await client.post("https://foo.bar/") assert request1.called is True assert request2.called is do_post
async def test_json_content(client, content, headers, expected_headers): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" request = httpx_mock.get(url, content=content, headers=headers) async_response = await client.get(url) assert request.called is True assert async_response.headers == httpx.Headers(expected_headers or headers) assert async_response.json() == content httpx_mock.reset() sync_response = httpx.get(url) assert request.called is True assert sync_response.headers == httpx.Headers(expected_headers or headers) assert sync_response.json() == content
async def test_http_methods(client): async with respx.HTTPXMock() as httpx_mock: url = "https://foo.bar/" m = httpx_mock.get(url, status_code=404) httpx_mock.post(url, status_code=201) httpx_mock.put(url, status_code=202) httpx_mock.patch(url, status_code=500) httpx_mock.delete(url, status_code=204) httpx_mock.head(url, status_code=405) httpx_mock.options(url, status_code=501) response = httpx.get(url) assert response.status_code == 404 response = await client.get(url) assert response.status_code == 404 response = httpx.post(url) assert response.status_code == 201 response = await client.post(url) assert response.status_code == 201 response = httpx.put(url) assert response.status_code == 202 response = await client.put(url) assert response.status_code == 202 response = httpx.patch(url) assert response.status_code == 500 response = await client.patch(url) assert response.status_code == 500 response = httpx.delete(url) assert response.status_code == 204 response = await client.delete(url) assert response.status_code == 204 response = httpx.head(url) assert response.status_code == 405 response = await client.head(url) assert response.status_code == 405 response = httpx.options(url) assert response.status_code == 501 response = await client.options(url) assert response.status_code == 501 assert m.called is True assert httpx_mock.stats.call_count == 7 * 2
async def test_httpx_exception_handling(client): async with respx.HTTPXMock() as httpx_mock: with asynctest.mock.patch( "httpx.client.AsyncClient.dispatcher_for_url", side_effect=ValueError("mock"), ): url = "https://foo.bar/" request = httpx_mock.get(url) with pytest.raises(ValueError): await client.get(url) assert request.called is True assert httpx_mock.stats.call_count == 1 _request, _response = httpx_mock.calls[-1] assert _request is not None assert _response is None
async def test_callable_content(client): async with respx.HTTPXMock() as httpx_mock: url_pattern = re.compile(r"https://foo.bar/(?P<slug>\w+)/") content = lambda request, slug: f"hello {slug}" request = httpx_mock.get(url_pattern, content=content) async_response = await client.get("https://foo.bar/world/") assert request.called is True assert async_response.status_code == 200 assert async_response.text == "hello world" httpx_mock.reset() sync_response = httpx.get("https://foo.bar/world/") assert request.called is True assert sync_response.status_code == 200 assert sync_response.text == "hello world"
async def test_repeated_pattern(client): async with respx.HTTPXMock() as httpx_mock: url = "https://foo/bar/baz/" one = httpx_mock.post(url, status_code=201) two = httpx_mock.post(url, status_code=409) response1 = await client.post(url, json={}) response2 = await client.post(url, json={}) response3 = await client.post(url, json={}) assert response1.status_code == 201 assert response2.status_code == 409 assert response3.status_code == 409 assert httpx_mock.stats.call_count == 3 assert one.called is True assert one.call_count == 1 statuses = [response.status_code for _, response in one.calls] assert statuses == [201] assert two.called is True assert two.call_count == 2 statuses = [response.status_code for _, response in two.calls] assert statuses == [409, 409]
def httpx_mock(): """respx mock object""" with respx.HTTPXMock(base_url='http://example.com') as _httpx_mock: yield _httpx_mock
async def test_invalid_url_pattern(): async with respx.HTTPXMock() as httpx_mock: with pytest.raises(ValueError): httpx_mock.get(["invalid"])