<s> import unittest <EOL> from peppercorn . compat import PY3 <EOL> class TestParse ( unittest . TestCase ) : <EOL> def _callFUT ( self , fields ) : <EOL> from peppercorn import parse <EOL> return parse ( fields ) <EOL> def _makeEnviron ( self , kw = None ) : <EOL> if kw is None : <EOL> kw = { } <EOL> env = { <EOL> '<STR_LIT>' : '<STR_LIT:http>' , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT>' : '<STR_LIT:/>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> env . update ( kw ) <EOL> return env <EOL> def _makeMultipartFieldStorage ( self , fields ) : <EOL> from cgi import FieldStorage <EOL> ct , body = encode_multipart_formdata ( fields ) <EOL> kw = dict ( CONTENT_TYPE = ct , REQUEST_METHOD = '<STR_LIT:POST>' ) <EOL> if PY3 : <EOL> from io import BytesIO <EOL> fp = BytesIO ( body . encode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> from StringIO import StringIO <EOL> fp = StringIO ( body ) <EOL> environ = self . _makeEnviron ( kw ) <EOL> headers = { '<STR_LIT>' : "<STR_LIT>" % len ( body ) , "<STR_LIT>" : ct } <EOL> return FieldStorage ( fp = fp , environ = environ , keep_blank_values = <NUM_LIT:1> , <EOL> headers = headers ) <EOL> def _getFields ( self ) : <EOL> from peppercorn import START , END , MAPPING , SEQUENCE <EOL> fields = [ <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:title>' , '<STR_LIT>' ) , <EOL> ( START , '<STR_LIT>' % MAPPING ) , <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> ( START , '<STR_LIT>' % SEQUENCE ) , <EOL> ( START , '<STR_LIT>' % SEQUENCE ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' % SEQUENCE ) , <EOL> ( START , '<STR_LIT>' % SEQUENCE ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' % SEQUENCE ) , <EOL> ( END , '<STR_LIT>' % SEQUENCE ) , <EOL> ( END , '<STR_LIT>' % MAPPING ) , <EOL> ] <EOL> return fields <EOL> def _assertFieldsResult ( self , result ) : <EOL> self . assertEqual ( <EOL> result , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] , <EOL> } , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def test_bare ( self ) : <EOL> fields = self . _getFields ( ) <EOL> result = self . _callFUT ( fields ) <EOL> self . _assertFieldsResult ( result ) <EOL> def test_fieldstorage ( self ) : <EOL> fs = self . _makeMultipartFieldStorage ( self . _getFields ( ) ) <EOL> fields = [ ] <EOL> if fs . list : <EOL> for field in fs . list : <EOL> fields . append ( ( field . name , field . value ) ) <EOL> result = self . _callFUT ( fields ) <EOL> self . _assertFieldsResult ( result ) <EOL> def test_bad_start_marker ( self ) : <EOL> from peppercorn import START <EOL> fields = [ <EOL> ( START , '<STR_LIT>' ) , <EOL> ] <EOL> self . assertRaises ( ValueError , self . _callFUT , fields ) <EOL> def test_unnamed_start_marker ( self ) : <EOL> from peppercorn import START , END , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ] <EOL> result = self . _callFUT ( fields ) <EOL> self . assertEqual ( result , { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> def test_rename ( self ) : <EOL> from peppercorn import START , END , RENAME , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( START , '<STR_LIT>' + RENAME ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ] <EOL> result = self . _callFUT ( fields ) <EOL> self . assertEqual ( result , { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> def test_rename_no_subelements ( self ) : <EOL> from peppercorn import START , END , RENAME , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( START , '<STR_LIT>' + RENAME ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ] <EOL> result = self . _callFUT ( fields ) <EOL> self . assertEqual ( result , { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> def test_ignore ( self ) : <EOL> from peppercorn import START , END , IGNORE , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( START , '<STR_LIT>' + IGNORE ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ] <EOL> result = self . _callFUT ( fields ) <EOL> self . assertEqual ( result , { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> def test_excessive_end_markers ( self ) : <EOL> from peppercorn import START , END , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ( END , '<STR_LIT>' ) , <EOL> ] <EOL> self . assertRaises ( ValueError , self . _callFUT , fields ) <EOL> def test_insufficient_end_markers ( self ) : <EOL> from peppercorn import START , END , MAPPING <EOL> fields = [ <EOL> ( START , MAPPING ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> self . assertRaises ( ValueError , self . _callFUT , fields ) <EOL> def encode_multipart_formdata ( fields ) : <EOL> BOUNDARY = '<STR_LIT>' <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> L = [ ] <EOL> for ( key , value ) in fields : <EOL> L . append ( '<STR_LIT>' + BOUNDARY ) <EOL> L . append ( '<STR_LIT>' % key ) <EOL> L . append ( '<STR_LIT>' ) <EOL> L . append ( value ) <EOL> L . append ( '<STR_LIT>' + BOUNDARY + '<STR_LIT>' ) <EOL> L . append ( '<STR_LIT>' ) <EOL> body = CRLF . join ( L ) <EOL> content_type = '<STR_LIT>' % BOUNDARY <EOL> return content_type , body </s>
