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

Query params are being encoded too early so cannot be effectively mocked #288

Open
ghandic opened this issue Mar 11, 2023 · 1 comment
Open

Comments

@ghandic
Copy link

ghandic commented Mar 11, 2023

Describe the bug
Query params are being encoded too early so cannot be effectively mocked

To Reproduce
See full dialog in chassing/uplink-httpx#6

Expected behavior
See full dialog in chassing/uplink-httpx#6

Additional context
This seems to be an edge case when the characters are need percent url encoding

@ghandic
Copy link
Author

ghandic commented Mar 11, 2023

I found that if I set encoded=True then it is passing, though this seems a little sketchy :D as httpx seems to auto encode but other http clients might not

import httpx
import respx
from fastapi import FastAPI, Query
from uplink import Consumer
from uplink import Query as Q
from uplink import get, returns
from uplink_httpx import HttpxClient

### Given: Mock Server
BASE_URL = "https://mock.api/"

app = FastAPI()


@app.get("/user")
def get_user(select: str = Query(alias="$select")):
    return {"id": select}


mock_api = respx.mock(base_url=BASE_URL, assert_all_called=False)
mock_api.route().mock(side_effect=respx.ASGIHandler(app))


### When: Client
class BasicClient(Consumer):
    @get("user")
    @returns.json()
    def user(self, select: Q("$select", encoded=True)):
        ...


### Then: Should work with either raw client or uplink client
@mock_api
async def test_raw():
    client = BasicClient(base_url=BASE_URL, client=HttpxClient())
    resp = await client.user(select=123)
    assert resp.status_code == 200
    assert resp.json() == {"id": "123"}


@mock_api
async def test_uplink():
    resp = await httpx.AsyncClient().get(f"{BASE_URL}user?$select=123")
    assert resp.status_code == 200
    assert resp.json() == {"id": "123"}

All passing with the above

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