Skip to content

JSON support for the Flask test client#1408

Closed
adambyrtek wants to merge 3 commits into
pallets:masterfrom
adambyrtek:json-test-client
Closed

JSON support for the Flask test client#1408
adambyrtek wants to merge 3 commits into
pallets:masterfrom
adambyrtek:json-test-client

Conversation

@adambyrtek
Copy link
Copy Markdown
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.

Comment thread flask/testing.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread flask/testing.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

@untitaker
Copy link
Copy Markdown
Contributor

LGTM, please add a changelog entry and proper documentation.

@adambyrtek
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Ahh, I see.

@untitaker
Copy link
Copy Markdown
Contributor

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

@untitaker
Copy link
Copy Markdown
Contributor

Otherwise, 👍

@adambyrtek
Copy link
Copy Markdown
Contributor Author

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

@adambyrtek
Copy link
Copy Markdown
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