-
-
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 request/response mixin #1984
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c4139e0
JSON support for the Flask test client
adambyrtek 6c5ef2b
Use `content_type` kwarg instead of manipulating headers
adambyrtek b099999
Use proper exception type and update changelog
adambyrtek ca547f0
JSON response tests and first draft of code that passes
adambyrtek c9ef500
Mixin for JSON decoding code shared between request/response
adambyrtek 23de586
Remove redundant `cache` flag
adambyrtek 539569e
Update the testing documentation
adambyrtek f0f458e
Alternative solution for lack of response caching
adambyrtek f0d3b71
Updates after code review
adambyrtek 1df2788
Use app_ctx instead of request_ctx to access the app
adambyrtek 5575faa
Update documentation to use the getter only once
adambyrtek 5ebdd5d
Documentation updates
adambyrtek 8661183
Remove _missing sentinel and update docs
adambyrtek 62b0b66
testing: Make json a keyword arg
untitaker 5c4fa7e
Remove already defined method
untitaker 136a833
Bugfix: EnvironBuilder doesn't take `json`
untitaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,3 +353,35 @@ independently of the session backend used:: | |
Note that in this case you have to use the ``sess`` object instead of the | ||
:data:`flask.session` proxy. The object however itself will provide the | ||
same interface. | ||
|
||
|
||
Testing JSON APIs | ||
----------------- | ||
|
||
.. versionadded:: 1.0 | ||
|
||
Flask has great support for JSON, and is a popular choice for building REST | ||
APIs. Testing both JSON requests and responses using the test client is very | ||
convenient:: | ||
|
||
from flask import jsonify | ||
|
||
@app.route('/api/auth') | ||
def auth(): | ||
json_data = request.get_json() | ||
email = json_data['email'] | ||
password = json_data['password'] | ||
|
||
return jsonify(token=generate_token(email, password)) | ||
|
||
with app.test_client() as c: | ||
email = '[email protected]' | ||
password = 'secret' | ||
resp = c.post('/api/auth', json={'login': email, 'password': password}) | ||
|
||
json_data = resp.get_json() | ||
assert verify_token(email, json_data['token']) | ||
|
||
Note that if the ``json`` argument is provided then the test client will put | ||
JSON-serialized data in the request body, and also set the | ||
``Content-Type: application/json`` HTTP header. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
Sorry, something went wrong.