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

Bad encoding in query parameters with new TestClient using httpx.Client #1952

Closed
Kludex opened this issue Nov 16, 2022 Discussed in #1946 · 2 comments
Closed

Bad encoding in query parameters with new TestClient using httpx.Client #1952

Kludex opened this issue Nov 16, 2022 Discussed in #1946 · 2 comments
Labels
good first issue Good for beginners testclient TestClient-related

Comments

@Kludex
Copy link
Sponsor Member

Kludex commented Nov 16, 2022

Discussed in #1946

Originally posted by azogue November 15, 2022
After the change to httpx for the TestClient in v0.21.0 (), the query parameters are not properly encoded? when sending requests with it, and strings are corrupted when received in the endpoints.

Quick example to show the problem:

import logging
from fastapi import FastAPI

app = FastAPI()

@app.get("/example")
async def _show_encoding_error(look_for: str):
    return {"found": look_for}

if __name__ == '__main__':
    from starlette.testclient import TestClient

    with TestClient(app) as client:
        params = {"look_for": "plain text"}
        resp = client.get("/example", params=params).json()
        logging.warning(resp)
        assert resp["found"] == "plain text"

        params = {"look_for": "España"}
        resp = client.get("/example", params=params).json()
        logging.warning(resp)
        assert resp["found"] == "España", resp["found"]

The example app works as expected if called from the SwaggerUI or from another python process using a plain httpx.Client, so it appears something broke with the new wrapping for TestClient 🥲

import httpx

params = {"look_for": "España"}
with httpx.Client(base_url="http://localhost:8000/") as client:
    resp = client.get("/example", params=params).json()
    assert resp["found"] == "España"

Details

First issued in fastapi repo: tiangolo/fastapi#5646, before checking the TestClient comes from here 🙈

Environment:

  • macOS M1 arm64
  • fastapi v0.87.0
  • starlette v0.21.0
  • httpx v0.23.0
@Kludex Kludex added testclient TestClient-related good first issue Good for beginners labels Nov 16, 2022
@azogue
Copy link
Contributor

azogue commented Nov 16, 2022

I just checked that removing both unquote operations for the netloc and the query parts does work as expected and does not break any of the tests 🥳 (609 passed, 2 xfailed in 2.41s in quick venv with py3.10)

So it could be as simple as that to fix this 🤩

Kludex added a commit that referenced this issue Nov 16, 2022
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@Kludex
Copy link
Sponsor Member Author

Kludex commented Nov 16, 2022

This was fixed on #1953 , and it will be available on Starlette 0.22.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for beginners testclient TestClient-related
Projects
None yet
Development

No branches or pull requests

2 participants