Example #1
0
def test_content_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(match_content=b"This is the body")

    with httpx.Client() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            client.post("http://test_url", content=b"This is the body2")
        assert (
            str(exception_info.value) ==
            """No response can be found for POST request on http://test_url with b'This is the body2' body amongst:
Match all requests with b'This is the body' body""")

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #2
0
def test_method_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(method="get")

    with httpx.Client() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            client.post("http://test_url")
        assert (
            str(exception_info.value) ==
            """No response can be found for POST request on http://test_url amongst:
Match GET requests""")

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #3
0
async def test_url_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(url="http://test_url")

    async with httpx.AsyncClient() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            await client.get("http://test_url2")
        assert (
            str(exception_info.value) ==
            """No response can be found for GET request on http://test_url2 amongst:
Match all requests on http://test_url""")

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #4
0
def test_url_query_string_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(url="http://test_url?a=1&a=2")

    with httpx.Client() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            # Same parameter order matters as it corresponds to a list on server side
            client.get("http://test_url?a=2&a=1")
        assert (
            str(exception_info.value) ==
            """No response can be found for GET request on http://test_url?a=2&a=1 amongst:
Match all requests on http://test_url?a=1&a=2""")

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #5
0
def test_headers_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(
        match_headers={
            "user-agent": f"python-httpx/{httpx.__version__}",
            "host": "test_url2",
            "host2": "test_url",
        })

    with httpx.Client() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            client.get("http://test_url")
        assert (
            str(exception_info.value) ==
            f"""No response can be found for GET request on http://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2', 'host2': 'test_url'}} headers"""
        )

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #6
0
def test_headers_and_content_not_matching(httpx_mock: HTTPXMock):
    httpx_mock.add_response(
        match_headers={
            "user-agent": f"python-httpx/{httpx.__version__}",
            "host": "test_url2",
        },
        match_content=b"This is the body2",
    )

    with httpx.Client() as client:
        with pytest.raises(httpx.TimeoutException) as exception_info:
            client.post("http://test_url", data=b"This is the body")
        assert (
            str(exception_info.value)
            == f"""No response can be found for POST request on http://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
        )

    # Clean up responses to avoid assertion failure
    httpx_mock.reset(assert_all_responses_were_requested=False)
Example #7
0
def httpx_session(httpx_mock: HTTPXMock):

    # Override reset since we don't care if not all mocked routes are called
    def reset_override(*args):
        pass

    httpx_mock.reset = reset_override

    # Load a minimal channel response to mock a Mirth server
    responses_path = Path(__file__).resolve().parent.joinpath("responses")
    with open(responses_path.joinpath("channels.xml"), "r") as f:
        channels_response: str = f.read()
    with open(responses_path.joinpath("channelStatistics.xml"), "r") as f:
        channel_statistics_response: str = f.read()
    with open(responses_path.joinpath("channelGroups.xml"), "r") as f:
        channel_groups_response: str = f.read()
    with open(responses_path.joinpath("messageResponse.xml"), "r") as f:
        message_999_response: str = f.read()

    httpx_mock.add_response(url=re.compile(r"mock:\/\/mirth.url\/channels"),
                            data=channels_response)
    httpx_mock.add_response(
        url=re.compile(r"mock:\/\/mirth.url\/channels\/statistics"),
        data=channel_statistics_response,
    )
    httpx_mock.add_response(
        url=re.compile(r"mock:\/\/mirth.url\/channelgroups"),
        data=channel_groups_response,
    )

    httpx_mock.add_response(
        method="POST",
        data="<long>999</long>",
        url=re.compile(r"mock:\/\/mirth.url\/channels\/.*\/messages"),
    )

    httpx_mock.add_response(
        method="GET",
        data=message_999_response,
        url=re.compile(r"mock:\/\/mirth.url\/channels\/.*\/messages\/999"),
    )

    httpx_mock.add_response(
        status_code=204,
        url=re.compile(r"mock:\/\/mirth.url\/channels\/.*\/messages"))