How to use the respx.mock function in respx

To help you get started, we’ve selected a few respx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lundberg / respx / tests / test_mock.py View on Github external
    @respx.mock(assert_all_called=False, assert_all_mocked=False)
    async def test(respx_mock):
        assert respx_mock.stats.call_count == 0
        request = respx_mock.get("https://foo.bar/")
        response = await client.get("https://some.thing/")

        assert response.status_code == 200
        assert response.headers == httpx.Headers({"Content-Type": "text/plain"})
        assert response.text == ""

        assert request.called is False
        assert respx.stats.call_count == 0
        assert respx_mock.stats.call_count == 1

        _request, _response = respx_mock.calls[-1]
        assert _request is not None
        assert _response is not None
github lundberg / respx / tests / test_mock.py View on Github external
    @respx.mock
    def test():
        assert respx.stats.call_count == 0
        request = respx.get("https://foo.bar/", status_code=202)
        response = httpx.get("https://foo.bar/")
        assert request.called is True
        assert response.status_code == 202
        assert respx.stats.call_count == 1
github lundberg / respx / tests / test_stats.py View on Github external
    @respx.mock
    async def test(backend):
        url = "https://foo.bar/1/"
        respx.get(re.compile("https://some.thing"))
        respx.delete("https://some.thing")

        foobar1 = respx.get(url, status_code=202, alias="get_foobar")
        foobar2 = respx.delete(url, status_code=200, alias="del_foobar")

        assert foobar1.called is False
        assert foobar1.call_count == len(foobar1.calls)
        assert foobar1.call_count == 0
        assert respx.stats.call_count == len(respx.calls)
        assert respx.stats.call_count == 0

        async with httpx.AsyncClient() as client:
            get_response = await client.get(url)
github lundberg / respx / tests / test_mock.py View on Github external
    @respx.mock()
    def test(respx_mock):
        assert respx.stats.call_count == 0
        request = respx_mock.get("https://foo.bar/", status_code=202)
        response = httpx.get("https://foo.bar/")
        assert request.called is True
        assert response.status_code == 202
        assert respx.stats.call_count == 0
        assert respx_mock.stats.call_count == 1
github lundberg / respx / tests / test_mock.py View on Github external
async def test_base_url(respx_mock=None):
    request = respx_mock.patch("/egg/", content="yolk")
    async with respx.mock(base_url="https://foo.bar/") as foobar_mock:
        request1 = foobar_mock.get("/baz/", content="baz")
        request2 = foobar_mock.post(re.compile(r"(?P\w+)/?$"), content="slug")
        request3 = foobar_mock.put(content="ok")

        async with httpx.AsyncClient(base_url="https://foo.bar") as client:
            response = await client.get("/baz/")
            assert request1.called is True
            assert response.text == "baz"

            response = await client.post("/apa/")
            assert request2.called is True
            assert response.text == "slug"

            response = await client.put("/")
            assert request3.called is True
            assert response.text == "ok"
github msys2 / msys2-web / tests / test_main.py View on Github external
def test_webhook_push(client, monkeypatch):
    monkeypatch.setenv("GITHUB_WEBHOOK_SECRET", "foobar")
    monkeypatch.setenv("APPVEYOR_ACCOUNT", "account")
    monkeypatch.setenv("APPVEYOR_PROJECT", "project")
    monkeypatch.setenv("APPVEYOR_TOKEN", "token")

    with respx.mock:
        request = respx.post(
            "https://ci.appveyor.com/api/builds",
            status_code=201,
            content={
                "buildId": 1234
            })

        r = client.post("/webhook", headers={
            "X-Hub-Signature": "sha1=241ebb961521e58a8b2d5d1436863df772ffd531",
            "X-GitHub-Event": "push",
        })
        assert request.called
        r.raise_for_status()
        assert "msg" in r.json()
        assert "1234" in r.json()["msg"]
github lundberg / respx / tests / test_mock.py View on Github external
async def test_nested_local_contextmanager(client):
    with respx.mock() as respx_mock_1:
        get_request = respx_mock_1.get("https://foo/bar/", status_code=202)

        with respx.mock() as respx_mock_2:
            post_request = respx_mock_2.post("https://foo/bar/", status_code=201)

            response = await client.get("https://foo/bar/")
            assert get_request.called is True
            assert response.status_code == 202
            assert respx.stats.call_count == 0
            assert respx_mock_1.stats.call_count == 1
            assert respx_mock_2.stats.call_count == 0

            response = await client.post("https://foo/bar/")
            assert post_request.called is True
            assert response.status_code == 201
            assert respx.stats.call_count == 0
            assert respx_mock_1.stats.call_count == 1
            assert respx_mock_2.stats.call_count == 1
github lundberg / respx / tests / test_mock.py View on Github external
async def test_local_contextmanager(client):
    with respx.mock() as respx_mock:
        assert respx_mock.stats.call_count == 0
        request = respx_mock.get("https://foo/bar/", status_code=202)
        response = await client.get("https://foo/bar/")
        assert request.called is True
        assert response.status_code == 202
        assert respx.stats.call_count == 0
        assert respx_mock.stats.call_count == 1

    async with respx.mock() as respx_mock:
        assert respx_mock.stats.call_count == 0
        request = respx_mock.get("https://foo/bar/", status_code=202)
        response = await client.get("https://foo/bar/")
        assert request.called is True
        assert response.status_code == 202
        assert respx.stats.call_count == 0
        assert respx_mock.stats.call_count == 1
github lundberg / respx / tests / test_mock.py View on Github external
async def test_nested_local_contextmanager(client):
    with respx.mock() as respx_mock_1:
        get_request = respx_mock_1.get("https://foo/bar/", status_code=202)

        with respx.mock() as respx_mock_2:
            post_request = respx_mock_2.post("https://foo/bar/", status_code=201)

            response = await client.get("https://foo/bar/")
            assert get_request.called is True
            assert response.status_code == 202
            assert respx.stats.call_count == 0
            assert respx_mock_1.stats.call_count == 1
            assert respx_mock_2.stats.call_count == 0

            response = await client.post("https://foo/bar/")
            assert post_request.called is True
            assert response.status_code == 201
            assert respx.stats.call_count == 0