def test_default(self): """ Default content parsers. """ interceptors = [body_params()] request = m( content_type='application/x-www-form-urlencoded; charset="utf-8"', character_encoding='utf-8', body=BytesIO( urllib.urlencode([(b'a', b'1'), (b'b', b'2'), (b'b', b'3'), (b'c', u'\N{SNOWMAN}'.encode('utf-8')), (b'd', b'')]))) context = empty_context.set(REQUEST, request) self.assertThat( execute(context, interceptors), succeeded( ContainsDict({ REQUEST: ContainsDict({ 'form_params': Equals( pmap({ u'a': u'1', u'b': [u'2', u'3'], u'c': u'\N{SNOWMAN}', u'd': True })) }) })))
def test_unhandled(self): """ If nothing can handle the content type, return the request untouched. """ interceptors = [body_params()] context = empty_context.set(REQUEST, self.request()) self.assertThat(execute(context, interceptors), succeeded(Equals(context)))
def test_custom(self): """ Custom content parsers. """ interceptors = [ body_params( default_parsers(json_options=dict(object_hook=lambda _: 42))) ] context = empty_context.set(REQUEST, self.request()) self.assertThat( execute(context, interceptors), succeeded( ContainsDict( {REQUEST: ContainsDict({'json_params': Equals(42)})})))
def test_default(self): """ Default content parsers. """ interceptors = [body_params()] context = empty_context.set(REQUEST, self.request()) self.assertThat( execute(context, interceptors), succeeded( ContainsDict({ REQUEST: ContainsDict( {'json_params': Equals(freeze(self.payload()))}) })))
def test_default(self): """ Default content parsers. """ interceptors = [body_params()] request = m( content_type= 'multipart/form-data; boundary=---------------------------114772229410704779042051621609', body=open_test_data('data/multipart_request')) context = empty_context.set(REQUEST, request) self.assertThat( execute(context, interceptors), succeeded( ContainsDict({ REQUEST: ContainsDict({ 'multipart_params': MatchesDict({ u'name': Multipart(content_length=Equals(8), name=Equals(u'name'), headers=MatchesDict({ u'Content-Disposition': Equals(u'form-data; name="name"'), }), body=Equals(b'Some One')), u'email': Multipart(content_length=Equals(16), name=Equals(u'email'), headers=MatchesDict({ u'Content-Disposition': Equals(u'form-data; name="email"'), }), body=Equals(b'*****@*****.**')), u'avatar': Multipart( content_length=Equals(869), content_type=Equals(u'image/png'), filename=Equals(u'smiley-cool.png'), name=Equals(u'avatar'), headers=MatchesDict({ u'Content-Type': Equals(u'image/png'), u'Content-Disposition': Equals( u'form-data; name="avatar"; filename="smiley-cool.png"' ), }), body=After( lambda x: hashlib.sha256(x).hexdigest(), Equals( b'25fbe073db80f71a13fb8e0a190a76c0fda494d18849fa6fa87ea5a0924baa07' ))), # XXX: This syntax isn't supported by the multipart # parser, multiple things with the same name are # overwritten. u'attachments[]': Always(), }), 'form_params': MatchesDict({ u'name': Equals(u'Some One'), u'email': Equals(u'*****@*****.**') }) }) })))