summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGael Pasgrimaud <gael@gawel.org>2014-04-16 20:58:59 +0200
committerGael Pasgrimaud <gael@gawel.org>2014-04-16 20:58:59 +0200
commitae46ba7be556c43365a88edba6af5ae45efb5d07 (patch)
treeff1886a16a599b49fb55dbec4db44afb66f476f0
parent5806c6865e717dd382d004c908fc9db34fe3a48c (diff)
downloadwebtest-ae46ba7be556c43365a88edba6af5ae45efb5d07.tar.gz
Fixed #92 You can now override TestApp.JSONEncoder to use a custom encoder
-rw-r--r--CHANGELOG.rst2
-rw-r--r--tests/test_utils.py5
-rw-r--r--webtest/app.py13
-rw-r--r--webtest/utils.py4
4 files changed, 15 insertions, 9 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 32a7b29..e232ae2 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -4,6 +4,8 @@ News
2.0.15 (unreleased)
-------------------
+- Fixed #92 You can now override TestApp.JSONEncoder to use a custom encoder
+
- Fixed #93 Support basic authentication
- Fixed #107 Explicit error message when WSGIProxy2 is not installer
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 0aa2a9e..da174a3 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -2,7 +2,7 @@
from __future__ import unicode_literals
import re
-from json import dumps
+import json
from .compat import unittest
from webtest import utils
@@ -81,6 +81,7 @@ class json_methodTest(unittest.TestCase):
class MockTestApp(object):
"""Mock TestApp used to test the json_object decorator."""
from webtest.utils import json_method
+ JSONEncoder = json.JSONEncoder
foo_json = json_method('FOO')
def _gen_request(self, method, url, **kw):
@@ -100,7 +101,7 @@ class json_methodTest(unittest.TestCase):
self.assertEquals(self.mock.foo_json('url', params={'a': 'b'}, c='c'),
('FOO', 'url', {'content_type': 'application/json',
'c': 'c',
- 'params': dumps({'a': 'b'}),
+ 'params': json.dumps({'a': 'b'}),
'upload_files': None}))
def test_json_method_doc(self):
diff --git a/webtest/app.py b/webtest/app.py
index 3a21643..6fd308f 100644
--- a/webtest/app.py
+++ b/webtest/app.py
@@ -9,13 +9,14 @@ Most interesting is TestApp
"""
from __future__ import unicode_literals
-import cgi
-import fnmatch
-import mimetypes
import os
-import random
import re
+import cgi
+import json
+import random
+import fnmatch
import warnings
+import mimetypes
from base64 import b64encode
@@ -116,6 +117,8 @@ class TestApp(object):
RequestClass = TestRequest
+ JSONEncoder = json.JSONEncoder
+
def __init__(self, app, extra_environ=None, relative_to=None,
use_unicode=True, cookiejar=None, parser_features=None):
if 'WEBTEST_TARGET_URL' in os.environ:
@@ -124,7 +127,7 @@ class TestApp(object):
if app.startswith('http'):
try:
from wsgiproxy import HostProxy
- except ImportError:
+ except ImportError: # pragma: no cover
raise ImportError((
'Using webtest with a real url requires WSGIProxy2. '
'Please install it with: '
diff --git a/webtest/utils.py b/webtest/utils.py
index b5d6d09..ac78bcd 100644
--- a/webtest/utils.py
+++ b/webtest/utils.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
-from json import dumps
import re
import six
+from json import dumps
from six.moves import html_parser
from webtest.compat import urlencode
@@ -32,7 +32,7 @@ def json_method(method):
def wrapper(self, url, params=NoDefault, **kw):
content_type = 'application/json'
if params is not NoDefault:
- params = dumps(params)
+ params = dumps(params, cls=self.JSONEncoder)
kw.update(
params=params,
content_type=content_type,