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

Support for x-www-form-urlencoded #369

Open
aragilar opened this issue Oct 2, 2024 · 1 comment
Open

Support for x-www-form-urlencoded #369

aragilar opened this issue Oct 2, 2024 · 1 comment

Comments

@aragilar
Copy link

aragilar commented Oct 2, 2024

Is adding support for x-www-form-urlencoded something that would be accepted/supported?

@csernazs
Copy link
Owner

csernazs commented Oct 3, 2024

hi @aragilar ,

Yes, I think supporting that would make sense.

Just to be precise, do you mean sending a POST/PUT/etc request to the server with the body like this?

    requests.post(httpserver.url_for("/foo"), data={"foo": "bar"})

And then, pytest-httpserver would have something like this:

    httpserver.expect_request("/foo", data_form=MultiDict({"foo": "bar"})).respond_with_handler(handler)

(see data_form field).

On the other hand, do you know that you can specify your own handler?

from pytest_httpserver import HTTPServer
import requests
from werkzeug import Request, Response
from werkzeug.datastructures import MultiDict


def test_www_form_urlencoded(httpserver: HTTPServer):
    def handler(request: Request) -> Response:
        if request.form == MultiDict({"foo": "bar"}):
            return Response("OK")
        else:
            return Response(status=500)

    httpserver.expect_request("/foo").respond_with_handler(handler)

    assert requests.post(httpserver.url_for("/foo"), data={"foo": "bar"}).text == "OK"

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

2 participants