-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
JSON support for the Flask test client #1408
Conversation
@@ -33,6 +34,18 @@ def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs): | |||
path = url.path | |||
if url.query: | |||
path += '?' + url.query | |||
|
|||
if json: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
da16e81
to
41b36f2
Compare
|
||
if json is not None: | ||
if 'data' in kwargs: | ||
raise RuntimeError('Client cannot provide both `json` and `data`') |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
LGTM, please add a changelog entry and proper documentation. |
80fb239
to
c8f1985
Compare
@untitaker Please take a second look. I've updated the changelog, but couldn't find a good place for documentation. There's no API reference for |
Maybe it would be good to extend the json support to Werkzeug's client/response as well. Then the documentation could go there along with what already exists. |
@davidism Yeah, that's my next patch, but since this is my first contribution to Flask I decided to submit this piece by piece. |
@adambyrtek I think it only makes sense to do this on one of them -- Flask's test client is a subclass of Werkzeug's. |
@untitaker Sorry, I think I misread the comment. Just to clarify, I don't intend to patch Werkzeug (since I believe JSON support is too high-level for that), but I'd like to extend the Flask response object in a separate pull request to allow:
However, I'm open to any suggestions on how to improve the documentation for this. |
Ahh, I see. |
Ok, currently this is missing documentation -- AFAICT there's no way to discover this functionality from the online docs. |
Otherwise, 👍 |
@untitaker Sure, I'll try to incorporate this in the testing section. |
Moved to #1416. |
This change adds support for JSON requests to the Flask test client, which makes testing JSON APIs much more convenient, and makes the arguments consistent with
request.data
andrequest.json
.Before:
response = client.post('/url', data=json.dumps(json_data), content_type='application/json')
After:
response = client.post('/url', json=json_data})
If this gets merged then I have another change to support
response.json
, which would make it easier to make assertions on JSON responses.