pytest-httpserver icon indicating copy to clipboard operation
pytest-httpserver copied to clipboard

Support for x-www-form-urlencoded

Open aragilar opened this issue 1 year ago • 1 comments

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

aragilar avatar Oct 02 '24 04:10 aragilar

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"

csernazs avatar Oct 03 '24 06:10 csernazs