00001
00002
00003 from __future__ import unicode_literals
00004 import re
00005 import json
00006 from .compat import unittest
00007 from webtest import utils
00008
00009
00010 class NoDefaultTest(unittest.TestCase):
00011
00012 def test_nodefault(self):
00013 from webtest.utils import NoDefault
00014 self.assertEquals(repr(NoDefault), '<NoDefault>')
00015
00016
00017 class encode_paramsTest(unittest.TestCase):
00018
00019 def test_encode_params_None(self):
00020 self.assertEquals(utils.encode_params(None, None), None)
00021
00022 def test_encode_params_NoDefault(self):
00023 self.assertEquals(utils.encode_params(utils.NoDefault, None), '')
00024
00025 def test_encode_params_dict_or_list(self):
00026 self.assertEquals(utils.encode_params({'foo': 'bar'}, None),
00027 utils.encode_params([('foo', 'bar')], None))
00028
00029 def test_encode_params_no_charset(self):
00030
00031 self.assertEquals(utils.encode_params({'foo': 'bar'}, None), 'foo=bar')
00032
00033 self.assertEquals(utils.encode_params({'foo': 'bar'}, 'ba'), 'foo=bar')
00034
00035 def test_encode_params_charset_utf8(self):
00036
00037 self.assertEquals(utils.encode_params({'f': '€'}, ' CHARset=uTF-8; '),
00038 'f=%E2%82%AC')
00039
00040
00041 class make_patternTest(unittest.TestCase):
00042
00043 def call_FUT(self, obj):
00044 from webtest.utils import make_pattern
00045 return make_pattern(obj)
00046
00047 def test_make_pattern_None(self):
00048 self.assertEquals(self.call_FUT(None), None)
00049
00050 def test_make_pattern_regex(self):
00051 regex = re.compile(r'foobar')
00052 self.assertEquals(self.call_FUT(regex), regex.search)
00053
00054 def test_make_pattern_function(self):
00055 func = lambda x: x
00056 self.assertEquals(self.call_FUT(func), func)
00057
00058 def test_make_pattern_bytes(self):
00059
00060
00061 self.assertEqual(self.call_FUT('a')('a').string, 'a')
00062
00063 def test_make_pattern_invalid(self):
00064 self.assertRaises(ValueError, self.call_FUT, 0)
00065
00066
00067 class stringifyTest(unittest.TestCase):
00068
00069 def test_stringify_text(self):
00070 self.assertEquals(utils.stringify("foo"), "foo")
00071
00072 def test_stringify_binary(self):
00073 self.assertEquals(utils.stringify(b"foo"), "foo")
00074
00075 def test_stringify_other(self):
00076 self.assertEquals(utils.stringify(123), "123")
00077
00078
00079 class json_methodTest(unittest.TestCase):
00080
00081 class MockTestApp(object):
00082 """Mock TestApp used to test the json_object decorator."""
00083 from webtest.utils import json_method
00084 JSONEncoder = json.JSONEncoder
00085 foo_json = json_method('FOO')
00086
00087 def _gen_request(self, method, url, **kw):
00088 return (method, url, kw)
00089
00090 mock = MockTestApp()
00091
00092 def test_json_method_request_calls(self):
00093 from webtest.utils import NoDefault
00094
00095 self.assertEquals(self.mock.foo_json('url', params=NoDefault, c='c'),
00096 ('FOO', 'url', {'content_type': 'application/json',
00097 'c': 'c',
00098 'params': NoDefault,
00099 'upload_files': None}))
00100
00101 self.assertEquals(self.mock.foo_json('url', params={'a': 'b'}, c='c'),
00102 ('FOO', 'url', {'content_type': 'application/json',
00103 'c': 'c',
00104 'params': json.dumps({'a': 'b'}),
00105 'upload_files': None}))
00106
00107 def test_json_method_doc(self):
00108 self.assertIn('FOO request', self.mock.foo_json.__doc__)
00109 self.assertIn('TestApp.foo', self.mock.foo_json.__doc__)
00110
00111 def test_json_method_name(self):
00112 self.assertEqual(self.mock.foo_json.__name__, 'foo_json')