<s> from test . test_support import run_unittest , check_warnings <EOL> import cgi <EOL> import os <EOL> import sys <EOL> import tempfile <EOL> import unittest <EOL> from collections import namedtuple <EOL> class HackedSysModule : <EOL> argv = [ ] <EOL> stdin = sys . stdin <EOL> cgi . sys = HackedSysModule ( ) <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> class ComparableException : <EOL> def __init__ ( self , err ) : <EOL> self . err = err <EOL> def __str__ ( self ) : <EOL> return str ( self . err ) <EOL> def __cmp__ ( self , anExc ) : <EOL> if not isinstance ( anExc , Exception ) : <EOL> return - <NUM_LIT:1> <EOL> x = cmp ( self . err . __class__ , anExc . __class__ ) <EOL> if x != <NUM_LIT:0> : <EOL> return x <EOL> return cmp ( self . err . args , anExc . args ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . err , attr ) <EOL> def do_test ( buf , method ) : <EOL> env = { } <EOL> if method == "<STR_LIT:GET>" : <EOL> fp = None <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:GET>' <EOL> env [ '<STR_LIT>' ] = buf <EOL> elif method == "<STR_LIT:POST>" : <EOL> fp = StringIO ( buf ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:POST>' <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = str ( len ( buf ) ) <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % method <EOL> try : <EOL> return cgi . parse ( fp , env , strict_parsing = <NUM_LIT:1> ) <EOL> except StandardError , err : <EOL> return ComparableException ( err ) <EOL> parse_strict_test_cases = [ <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT:&>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT:;>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT:=>" , { } ) , <EOL> ( "<STR_LIT>" , { } ) , <EOL> ( "<STR_LIT>" , { } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT>' : [ '<STR_LIT:a>' ] } ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:b>' : [ '<STR_LIT:a>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT>' : [ '<STR_LIT:a>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:a>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:a>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , ValueError ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:a>' : [ '<STR_LIT>' ] , '<STR_LIT:b>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:a>' : [ '<STR_LIT>' , '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:x>' : [ '<STR_LIT:1>' ] , '<STR_LIT:y>' : [ '<STR_LIT>' ] , '<STR_LIT:z>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:x>' : [ '<STR_LIT:1>' ] , '<STR_LIT:y>' : [ '<STR_LIT>' ] , '<STR_LIT:z>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT:x>' : [ '<STR_LIT:1>' ] , '<STR_LIT:y>' : [ '<STR_LIT>' ] , '<STR_LIT:z>' : [ '<STR_LIT>' ] } ) , <EOL> ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:r>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } ) , <EOL> ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:100>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:1>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } ) <EOL> ] <EOL> def first_elts ( list ) : <EOL> return map ( lambda x : x [ <NUM_LIT:0> ] , list ) <EOL> def first_second_elts ( list ) : <EOL> return map ( lambda p : ( p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) , list ) <EOL> def gen_result ( data , environ ) : <EOL> fake_stdin = StringIO ( data ) <EOL> fake_stdin . seek ( <NUM_LIT:0> ) <EOL> form = cgi . FieldStorage ( fp = fake_stdin , environ = environ ) <EOL> result = { } <EOL> for k , v in dict ( form ) . items ( ) : <EOL> result [ k ] = isinstance ( v , list ) and form . getlist ( k ) or v . value <EOL> return result <EOL> class CgiTests ( unittest . TestCase ) : <EOL> def test_escape ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , cgi . escape ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( "<STR_LIT>" , cgi . escape ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( "<STR_LIT>" , cgi . escape ( '<STR_LIT>' , True ) ) <EOL> def test_strict ( self ) : <EOL> for orig , expect in parse_strict_test_cases : <EOL> d = do_test ( orig , "<STR_LIT:GET>" ) <EOL> self . assertEqual ( d , expect , "<STR_LIT>" % repr ( orig ) ) <EOL> d = do_test ( orig , "<STR_LIT:POST>" ) <EOL> self . assertEqual ( d , expect , "<STR_LIT>" % repr ( orig ) ) <EOL> env = { '<STR_LIT>' : orig } <EOL> fcd = cgi . FormContentDict ( env ) <EOL> sd = cgi . SvFormContentDict ( env ) <EOL> fs = cgi . FieldStorage ( environ = env ) <EOL> if isinstance ( expect , dict ) : <EOL> self . assertEqual ( len ( expect ) , len ( fcd ) ) <EOL> self . assertItemsEqual ( expect . keys ( ) , fcd . keys ( ) ) <EOL> self . assertItemsEqual ( expect . values ( ) , fcd . values ( ) ) <EOL> self . assertItemsEqual ( expect . items ( ) , fcd . items ( ) ) <EOL> self . assertEqual ( fcd . get ( "<STR_LIT>" , "<STR_LIT:default>" ) , "<STR_LIT:default>" ) <EOL> self . assertEqual ( len ( sd ) , len ( fs ) ) <EOL> self . assertItemsEqual ( sd . keys ( ) , fs . keys ( ) ) <EOL> self . assertEqual ( fs . getvalue ( "<STR_LIT>" , "<STR_LIT:default>" ) , "<STR_LIT:default>" ) <EOL> for key in expect . keys ( ) : <EOL> expect_val = expect [ key ] <EOL> self . assertTrue ( fcd . has_key ( key ) ) <EOL> self . assertItemsEqual ( fcd [ key ] , expect [ key ] ) <EOL> self . assertEqual ( fcd . get ( key , "<STR_LIT:default>" ) , fcd [ key ] ) <EOL> self . assertTrue ( fs . has_key ( key ) ) <EOL> if len ( expect_val ) > <NUM_LIT:1> : <EOL> single_value = <NUM_LIT:0> <EOL> else : <EOL> single_value = <NUM_LIT:1> <EOL> try : <EOL> val = sd [ key ] <EOL> except IndexError : <EOL> self . assertFalse ( single_value ) <EOL> self . assertEqual ( fs . getvalue ( key ) , expect_val ) <EOL> else : <EOL> self . assertTrue ( single_value ) <EOL> self . assertEqual ( val , expect_val [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( fs . getvalue ( key ) , expect_val [ <NUM_LIT:0> ] ) <EOL> self . assertItemsEqual ( sd . getlist ( key ) , expect_val ) <EOL> if single_value : <EOL> self . assertItemsEqual ( sd . values ( ) , <EOL> first_elts ( expect . values ( ) ) ) <EOL> self . assertItemsEqual ( sd . items ( ) , <EOL> first_second_elts ( expect . items ( ) ) ) <EOL> def test_weird_formcontentdict ( self ) : <EOL> env = { '<STR_LIT>' : "<STR_LIT>" } <EOL> expect = { '<STR_LIT:x>' : <NUM_LIT:1> , '<STR_LIT:y>' : <NUM_LIT> , '<STR_LIT:z>' : '<STR_LIT>' , '<STR_LIT:1>' : '<STR_LIT>' } <EOL> d = cgi . InterpFormContentDict ( env ) <EOL> for k , v in expect . items ( ) : <EOL> self . assertEqual ( d [ k ] , v ) <EOL> for k , v in d . items ( ) : <EOL> self . assertEqual ( expect [ k ] , v ) <EOL> self . assertItemsEqual ( expect . values ( ) , d . values ( ) ) <EOL> def test_log ( self ) : <EOL> cgi . log ( "<STR_LIT>" ) <EOL> cgi . logfp = StringIO ( ) <EOL> cgi . initlog ( "<STR_LIT:%s>" , "<STR_LIT>" ) <EOL> cgi . log ( "<STR_LIT:%s>" , "<STR_LIT>" ) <EOL> self . assertEqual ( cgi . logfp . getvalue ( ) , "<STR_LIT>" ) <EOL> if os . path . exists ( "<STR_LIT>" ) : <EOL> cgi . logfp = None <EOL> cgi . logfile = "<STR_LIT>" <EOL> cgi . initlog ( "<STR_LIT:%s>" , "<STR_LIT>" ) <EOL> cgi . log ( "<STR_LIT>" ) <EOL> def test_fieldstorage_readline ( self ) : <EOL> class TestReadlineFile : <EOL> def __init__ ( self , file ) : <EOL> self . file = file <EOL> self . numcalls = <NUM_LIT:0> <EOL> def readline ( self , size = None ) : <EOL> self . numcalls += <NUM_LIT:1> <EOL> if size : <EOL> return self . file . readline ( size ) <EOL> else : <EOL> return self . file . readline ( ) <EOL> def __getattr__ ( self , name ) : <EOL> file = self . __dict__ [ '<STR_LIT:file>' ] <EOL> a = getattr ( file , name ) <EOL> if not isinstance ( a , int ) : <EOL> setattr ( self , name , a ) <EOL> return a <EOL> f = TestReadlineFile ( tempfile . TemporaryFile ( ) ) <EOL> f . write ( '<STR_LIT:x>' * <NUM_LIT> * <NUM_LIT> ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> env = { '<STR_LIT>' : '<STR_LIT>' } <EOL> fs = cgi . FieldStorage ( fp = f , environ = env ) <EOL> self . assertGreater ( f . numcalls , <NUM_LIT:2> ) <EOL> def test_fieldstorage_invalid ( self ) : <EOL> fs = cgi . FieldStorage ( ) <EOL> self . assertFalse ( fs ) <EOL> self . assertRaises ( TypeError , bool ( fs ) ) <EOL> self . assertEqual ( list ( fs ) , list ( fs . keys ( ) ) ) <EOL> fs . list . append ( namedtuple ( '<STR_LIT>' , '<STR_LIT:name>' ) ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( fs ) <EOL> def test_fieldstorage_multipart ( self ) : <EOL> env = { '<STR_LIT>' : '<STR_LIT:POST>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> postdata = """<STR_LIT>""" <EOL> fs = cgi . FieldStorage ( fp = StringIO ( postdata ) , environ = env ) <EOL> self . assertEqual ( len ( fs . list ) , <NUM_LIT:4> ) <EOL> expect = [ { '<STR_LIT:name>' : '<STR_LIT:id>' , '<STR_LIT:filename>' : None , '<STR_LIT:value>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:title>' , '<STR_LIT:filename>' : None , '<STR_LIT:value>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:file>' , '<STR_LIT:filename>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:filename>' : None , '<STR_LIT:value>' : '<STR_LIT>' } ] <EOL> for x in range ( len ( fs . list ) ) : <EOL> for k , exp in expect [ x ] . items ( ) : <EOL> got = getattr ( fs . list [ x ] , k ) <EOL> self . assertEqual ( got , exp ) <EOL> def test_fieldstorage_multipart_maxline ( self ) : <EOL> maxline = <NUM_LIT:1> << <NUM_LIT:16> <EOL> self . maxDiff = None <EOL> def check ( content ) : <EOL> data = """<STR_LIT>""" . replace ( '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ) % content <EOL> environ = { <EOL> '<STR_LIT>' : str ( len ( data ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> } <EOL> self . assertEqual ( gen_result ( data , environ ) , { '<STR_LIT>' : content } ) <EOL> check ( '<STR_LIT:x>' * ( maxline - <NUM_LIT:1> ) ) <EOL> check ( '<STR_LIT:x>' * ( maxline - <NUM_LIT:1> ) + '<STR_LIT:\r>' ) <EOL> check ( '<STR_LIT:x>' * ( maxline - <NUM_LIT:1> ) + '<STR_LIT:\r>' + '<STR_LIT:y>' * ( maxline - <NUM_LIT:1> ) ) <EOL> _qs_result = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def testQSAndUrlEncode ( self ) : <EOL> data = "<STR_LIT>" <EOL> environ = { <EOL> '<STR_LIT>' : str ( len ( data ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> } <EOL> v = gen_result ( data , environ ) <EOL> self . assertEqual ( self . _qs_result , v ) <EOL> def testQSAndFormData ( self ) : <EOL> data = """<STR_LIT>""" <EOL> environ = { <EOL> '<STR_LIT>' : str ( len ( data ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> } <EOL> v = gen_result ( data , environ ) <EOL> self . assertEqual ( self . _qs_result , v ) <EOL> def testQSAndFormDataFile ( self ) : <EOL> data = """<STR_LIT>""" <EOL> environ = { <EOL> '<STR_LIT>' : str ( len ( data ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> } <EOL> result = self . _qs_result . copy ( ) <EOL> result . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> v = gen_result ( data , environ ) <EOL> self . assertEqual ( result , v ) <EOL> def test_deprecated_parse_qs ( self ) : <EOL> with check_warnings ( ( '<STR_LIT>' <EOL> '<STR_LIT>' , PendingDeprecationWarning ) ) : <EOL> self . assertEqual ( { '<STR_LIT:a>' : [ '<STR_LIT>' ] , '<STR_LIT:B>' : [ '<STR_LIT>' ] , '<STR_LIT:b>' : [ '<STR_LIT>' ] } , <EOL> cgi . parse_qs ( '<STR_LIT>' ) ) <EOL> def test_deprecated_parse_qsl ( self ) : <EOL> with check_warnings ( ( '<STR_LIT>' <EOL> '<STR_LIT>' , PendingDeprecationWarning ) ) : <EOL> self . assertEqual ( [ ( '<STR_LIT:a>' , '<STR_LIT>' ) , ( '<STR_LIT:b>' , '<STR_LIT>' ) , ( '<STR_LIT:B>' , '<STR_LIT>' ) ] , <EOL> cgi . parse_qsl ( '<STR_LIT>' ) ) <EOL> def test_parse_header ( self ) : <EOL> self . assertEqual ( <EOL> cgi . parse_header ( "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , { } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , { } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT:filename>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT:filename>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT:filename>" : "<STR_LIT>" , "<STR_LIT:size>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( <EOL> cgi . parse_header ( '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT:filename>" : '<STR_LIT>' } ) ) <EOL> def test_main ( ) : <EOL> run_unittest ( CgiTests ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
