csernazs / pytest-httpserver

Http server for pytest to test http clients
MIT License
218 stars 28 forks source link

Support for x-www-form-urlencoded #369

Open aragilar opened 2 weeks ago

aragilar commented 2 weeks ago

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

csernazs commented 2 weeks ago

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"