Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def encode_params(params, content_type):
if params is NoDefault:
return ''
if isinstance(params, dict) or hasattr(params, 'items'):
params = list(params.items())
if isinstance(params, (list, tuple)):
if content_type:
content_type = content_type.lower()
if 'charset=' in content_type:
charset = content_type.split('charset=')[1]
charset = charset.strip('; ').lower()
encoded_params = []
for k, v in params:
if isinstance(v, six.text_type):
v = v.encode(charset)
encoded_params.append((k, v))
params = encoded_params
params = urlencode(params, doseq=True)
def get(self, name, index=None, default=utils.NoDefault):
"""
Get the named/indexed field object, or ``default`` if no field is
found. Throws an AssertionError if no field is found and no ``default``
was given.
"""
fields = self.fields.get(name)
if fields is None:
if default is utils.NoDefault:
raise AssertionError(
"No fields found matching %r (and no default given)"
% name)
return default
if index is None:
return self[name]
return fields[index]
def _gen_request(self, method, url, params=utils.NoDefault,
headers=None, extra_environ=None, status=None,
upload_files=None, expect_errors=False,
content_type=None):
"""
Do a generic request.
"""
environ = self._make_environ(extra_environ)
inline_uploads = []
# this supports OrderedDict
if isinstance(params, dict) or hasattr(params, 'items'):
params = list(params.items())
if isinstance(params, (list, tuple)):
def test_json_method_request_calls(self):
from webtest.utils import NoDefault
# no params
self.assertEqual(self.mock.foo_json('url', params=NoDefault, c='c'),
('FOO', 'url', {'content_type': 'application/json',
'c': 'c',
'params': NoDefault,
'upload_files': None}))
# params dumped to json
self.assertEqual(self.mock.foo_json('url', params={'a': 'b'}, c='c'),
('FOO', 'url', {'content_type': 'application/json',
'c': 'c',
'params': json.dumps({'a': 'b'}),
'upload_files': None}))
def make_wrapper(self, url, method, content_type, params=NoDefault, **kw):
"""
Helper method for generating wrapper method.
"""
if params is not NoDefault:
params = dumps(params, cls=self.JSONEncoder)
kw.update(
params=params,
content_type=content_type,
upload_files=None,
)
wrapper = self._gen_request(method, url, **kw)
subst = dict(lmethod=method.lower(), method=method)
wrapper.__name__ = str('%(lmethod)s_json_api' % subst)
return wrapper
def test_nodefault(self):
from webtest.utils import NoDefault
self.assertEqual(repr(NoDefault), '')
def test_json_method_request_calls(self):
from webtest.utils import NoDefault
# no params
self.assertEqual(self.mock.foo_json('url', params=NoDefault, c='c'),
('FOO', 'url', {'content_type': 'application/json',
'c': 'c',
'params': NoDefault,
'upload_files': None}))
# params dumped to json
self.assertEqual(self.mock.foo_json('url', params={'a': 'b'}, c='c'),
('FOO', 'url', {'content_type': 'application/json',
'c': 'c',
'params': json.dumps({'a': 'b'}),
'upload_files': None}))
def wrapper(self, url, params=NoDefault, **kw):
kw.setdefault('content_type', 'application/json')
if params is not NoDefault:
params = dumps(params, cls=self.JSONEncoder)
kw.update(
params=params,
upload_files=None,
)
return self._gen_request(method, url, **kw)