Pylons / webtest

Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.
https://docs.pylonsproject.org/projects/webtest/en/latest/
Other
335 stars 109 forks source link

Can we (optionally) allow for malformed JSON content when testing? #214

Closed JamesMakela closed 5 years ago

JamesMakela commented 5 years ago

Hey guys, I am not sure if this is something I just don't know how to do or if the TestApp can't really do this at the moment. I have looked through the issues and the docs I could google, and couldn't find anything related.

Basically I would like to be able to test for malformed JSON content in the request.body.

I have a RESTful web api and I would like to test that certain HTTP status codes are returned in certain situations. So if the api is unable to decode the JSON payload, we would return a status of 400 (HTTPBadRequest). So how do we form such a test with our TestApp? Here's an example of what I have tried:

self.testapp.post_json('/rest_obj', params='{"malformed":', status=400)

Looking at the post_json() function a bit, I thought it would maybe use the param string directly when building the request.body, but no. It would appear that the TestApp jsonifies the string, so what is actually sent in the request.body is this:

'"{\\"malformed\\":"'

So when my view function tries to loads(request.body), it succeeds instead of raising a ValueError. I am not sure how to test for this at the moment. An actual malformed dict would fail before the test even begins.

Is there an option in the post_json() function that can stop the jsonification of a string param?

gawel commented 5 years ago

I'm not sure it's a good idea.

Did you try something like app.request(method='POST', body='{', headers={'Content-Type': 'application/json', status='*') ?

JamesMakela commented 5 years ago

@gawel: Ok I tried your suggestion, and it worked for me. Just to be complete, here is a working code snippet:

        self.testapp.request('/rest_obj', method='POST',
                             body='{',
                             headers={'Content-Type': 'application/json'},
                             status=400)

Is it a good idea? Well that depends on what you are referring to.

Is it a good idea to ensure your REST APIs are failing correctly? Absolutely.

Is it a good idea to put this functionality into post_json()? Well, I am sort of ambivalent. It would have been a convenience, but not absolutely necessary since there is a workaround. I am just happy that there is a way to do this.

Anyway, thank you very much for the quick feedback. Much appreciated.

gawel commented 5 years ago

I was referring to allow malformed json. Still thinking it's not a good idea. The goal of those method was to dump() / set the correct content-type. We will have to guess if a string is a json object or if it should be dump to json. And it's easy to use .request() to send weird/broken requests.