Skip to content

Instantly share code, notes, and snippets.

@joshfriend
Created September 17, 2014 15:34
Show Gist options
  • Save joshfriend/7a22f6281513a215f744 to your computer and use it in GitHub Desktop.
Save joshfriend/7a22f6281513a215f744 to your computer and use it in GitHub Desktop.
JSON client wrapper for Flask testing
#!/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