Created
September 17, 2014 15:34
-
-
Save joshfriend/7a22f6281513a215f744 to your computer and use it in GitHub Desktop.
JSON client wrapper for Flask testing
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
#!/usr/bin/env python | |
import json | |
from flask.testing import FlaskClient | |
class JSONTestClient(FlaskClient): | |
"""Use by setting: | |
app.test_client_class = JSONTestClient | |
The app.test_client() will always use content-type 'applicaiton/json' and | |
will automatically encode python objects passed in to `data` parameter | |
""" | |
def open(self, *args, **kwargs): | |
if kwargs.get('data', None): | |
kwargs['data'] = json.dumps(kwargs['data']) | |
kwargs['content_type'] = 'application/json' | |
return super(FlaskClient, self).open(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment