Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to temporarily pass through requests(?) #270

Open
flaeppe opened this issue Apr 15, 2024 · 2 comments
Open

No way to temporarily pass through requests(?) #270

flaeppe opened this issue Apr 15, 2024 · 2 comments

Comments

@flaeppe
Copy link
Contributor

flaeppe commented Apr 15, 2024

In my suite a want the global default behaviour of disabling all external calls, unless explicitly excepted by a pass through.

I have the following fixture in a "root conftest.py" disabling calls:

@pytest.fixture(scope="session", autouse=True)
def _disable_external_httpx_calls() -> Generator[None, None, None]:
    with respx.mock:
        yield None

But now I want to, for a limited part of my tests, pass through some external request e.g.

def test_something():
    respx.route(host="localhost").pass_through()
    ...
    # Test done, but "localhost" will still be allowed

But as far as I can tell, the above pass through is globally configured. Which means that after I've called .pass_through() there's no reverting it.

Optimally I'd like to scope the pass through for a limited context, e.g.

def test_something():
    with respx.route(host="localhost").pass_through():
        ...
    # Test done, "localhost" disabled
@flaeppe
Copy link
Contributor Author

flaeppe commented Apr 15, 2024

To clarify I meant a minimal case looking along the lines of:

import httpx
import pytest
import respx

@pytest.fixture(scope="session", autouse=True)
def _disable_external_httpx_calls():
    with respx.mock:
        yield None

def test_first():
    respx.route(host="localhost").pass_through()
    httpx.get("http://localhost:8000/")

def test_second():
    httpx.get("http://localhost:8000/")

If test_first is called before test_second, the request in test_second will pass through to "localhost" but in that case I want to be able to block the request

@flaeppe
Copy link
Contributor Author

flaeppe commented Apr 15, 2024

I managed to figure out that since it's a global object involved I can create a fixture that wraps the pass through in a snapshot that'll get rolled back.

It auto-enables respx mocking essentially doing duplicate work, and it's slightly more verbose but that's only visible in the allowing fixture. But it's something that works at least.

@pytest.fixture(scope="session", autouse=True)
def _disable_external_httpx_calls():
    with respx.mock:
        yield None

@pytest.fixture()
def allow_localhost():
    with respx.mock:
        respx.route(host="localhost").pass_through()
        yield

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant