<s> """<STR_LIT>""" <EOL> import urllib <EOL> import json <EOL> import pytest <EOL> from marshmallow import fields <EOL> from marshmallow . compat import PY2 <EOL> from webargs import ValidationError <EOL> pytestmark = pytest . mark . skipif ( not PY2 , reason = '<STR_LIT>' ) <EOL> import webtest <EOL> if PY2 : <EOL> import webapp2 <EOL> from webargs . webapp2parser import parser <EOL> hello_args = { <EOL> '<STR_LIT:name>' : fields . Str ( missing = '<STR_LIT>' ) , <EOL> } <EOL> hello_multiple = { <EOL> '<STR_LIT:name>' : fields . List ( fields . Str ( ) ) , <EOL> } <EOL> hello_validate = { <EOL> '<STR_LIT>' : fields . Int ( validate = lambda n : n != <NUM_LIT:3> , <EOL> error_messages = { '<STR_LIT>' : "<STR_LIT>" } ) , <EOL> } <EOL> def test_parse_querystring_args ( ) : <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' ) <EOL> assert parser . parse ( hello_args , req = request ) == { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> def test_parse_querystring_multiple ( ) : <EOL> expected = { '<STR_LIT:name>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' ) <EOL> assert parser . parse ( hello_multiple , req = request ) == expected <EOL> def test_parse_form ( ) : <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = expected ) <EOL> assert parser . parse ( hello_args , req = request ) == expected <EOL> def test_parse_form_multiple ( ) : <EOL> expected = { '<STR_LIT:name>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = urllib . urlencode ( expected , doseq = True ) ) <EOL> assert parser . parse ( hello_multiple , req = request ) == expected <EOL> def test_parsing_form_default ( ) : <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = '<STR_LIT>' ) <EOL> assert parser . parse ( hello_args , req = request ) == { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> def test_parse_json ( ) : <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = json . dumps ( expected ) , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> assert parser . parse ( hello_args , req = request ) == expected <EOL> def test_parse_json_with_vendor_media_type ( ) : <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = json . dumps ( expected ) , <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert parser . parse ( hello_args , req = request ) == expected <EOL> def test_parse_json_default ( ) : <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = '<STR_LIT>' , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> assert parser . parse ( hello_args , req = request ) == { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> def test_parsing_cookies ( ) : <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> response = webapp2 . Response ( ) <EOL> response . set_cookie ( '<STR_LIT:name>' , expected [ '<STR_LIT:name>' ] ) <EOL> request = webapp2 . Request . blank ( '<STR_LIT:/>' , headers = { '<STR_LIT>' : response . headers [ '<STR_LIT>' ] } ) <EOL> assert parser . parse ( hello_args , req = request , locations = ( '<STR_LIT>' , ) ) == expected <EOL> def test_parsing_headers ( ) : <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request = webapp2 . Request . blank ( '<STR_LIT:/>' , headers = expected ) <EOL> assert parser . parse ( hello_args , req = request , locations = ( '<STR_LIT>' , ) ) == expected <EOL> def test_parse_files ( ) : <EOL> """<STR_LIT>""" <EOL> class Handler ( webapp2 . RequestHandler ) : <EOL> @ parser . use_args ( { '<STR_LIT>' : fields . List ( fields . Field ( ) ) } , locations = ( '<STR_LIT>' , ) ) <EOL> def post ( self , args ) : <EOL> self . response . content_type = '<STR_LIT:application/json>' <EOL> _value = lambda f : f . getvalue ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> data = dict ( ( i . filename , _value ( i . file ) ) for i in args [ '<STR_LIT>' ] ) <EOL> self . response . write ( json . dumps ( data ) ) <EOL> app = webapp2 . WSGIApplication ( [ ( '<STR_LIT:/>' , Handler ) ] ) <EOL> testapp = webtest . TestApp ( app ) <EOL> payload = [ ( '<STR_LIT>' , '<STR_LIT>' , b'<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' , b'<STR_LIT>' ) ] <EOL> res = testapp . post ( '<STR_LIT:/>' , upload_files = payload ) <EOL> assert res . json == { '<STR_LIT>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def test_exception_on_validation_error ( ) : <EOL> request = webapp2 . Request . blank ( '<STR_LIT:/>' , POST = { '<STR_LIT>' : '<STR_LIT:3>' } ) <EOL> with pytest . raises ( ValidationError ) : <EOL> parser . parse ( hello_validate , req = request ) <EOL> def test_validation_error_with_message ( ) : <EOL> request = webapp2 . Request . blank ( '<STR_LIT:/>' , POST = { '<STR_LIT>' : '<STR_LIT:3>' } ) <EOL> with pytest . raises ( ValidationError ) as exc : <EOL> parser . parse ( hello_validate , req = request ) <EOL> assert "<STR_LIT>" in exc . value <EOL> def test_default_app_request ( ) : <EOL> """<STR_LIT>""" <EOL> expected = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request = webapp2 . Request . blank ( '<STR_LIT>' , POST = expected ) <EOL> app = webapp2 . WSGIApplication ( [ ] ) <EOL> app . set_globals ( app , request ) <EOL> assert parser . parse ( hello_args ) == expected </s>
