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

JSON support for the Flask test client #1408

Closed
wants to merge 3 commits into from

Conversation

adambyrtek
Copy link
Contributor

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 and request.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.

@@ -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.


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.

@untitaker
Copy link
Contributor

LGTM, please add a changelog entry and proper documentation.

@adambyrtek
Copy link
Contributor Author

@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 FlaskClient besides the Werkzeug one. The user guide for testing doesn't cover JSON requests, and I didn't want to completely change it.

@davidism
Copy link
Member

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.

@adambyrtek
Copy link
Contributor Author

@davidism Yeah, that's my next patch, but since this is my first contribution to Flask I decided to submit this piece by piece.

@untitaker
Copy link
Contributor

@adambyrtek I think it only makes sense to do this on one of them -- Flask's test client is a subclass of Werkzeug's.

@adambyrtek
Copy link
Contributor Author

@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:

response = client.post('/url', json=json_data})
assert response.json == {'foo': 'bar'}

However, I'm open to any suggestions on how to improve the documentation for this.

@untitaker
Copy link
Contributor

Ahh, I see.

@untitaker
Copy link
Contributor

Ok, currently this is missing documentation -- AFAICT there's no way to discover this functionality from the online docs.

@untitaker
Copy link
Contributor

Otherwise, 👍

@adambyrtek
Copy link
Contributor Author

@untitaker Sure, I'll try to incorporate this in the testing section.

@adambyrtek
Copy link
Contributor Author

Moved to #1416.

@adambyrtek adambyrtek deleted the json-test-client branch April 4, 2015 15:35
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants