@@ -279,6 +279,27 @@ def check(content):
279279 check ('x' * (maxline - 1 ) + '\r ' )
280280 check ('x' * (maxline - 1 ) + '\r ' + 'y' * (maxline - 1 ))
281281
282+ def test_fieldstorage_multipart_w3c (self ):
283+ # Test basic FieldStorage multipart parsing (W3C sample)
284+ env = {
285+ 'REQUEST_METHOD' : 'POST' ,
286+ 'CONTENT_TYPE' : 'multipart/form-data; boundary={}' .format (BOUNDARY_W3 ),
287+ 'CONTENT_LENGTH' : str (len (POSTDATA_W3 ))}
288+ fp = BytesIO (POSTDATA_W3 .encode ('latin-1' ))
289+ fs = cgi .FieldStorage (fp , environ = env , encoding = "latin-1" )
290+ self .assertEqual (len (fs .list ), 2 )
291+ self .assertEqual (fs .list [0 ].name , 'submit-name' )
292+ self .assertEqual (fs .list [0 ].value , 'Larry' )
293+ self .assertEqual (fs .list [1 ].name , 'files' )
294+ files = fs .list [1 ].value
295+ self .assertEqual (len (files ), 2 )
296+ expect = [{'name' : None , 'filename' : 'file1.txt' , 'value' : b'... contents of file1.txt ...' },
297+ {'name' : None , 'filename' : 'file2.gif' , 'value' : b'...contents of file2.gif...' }]
298+ for x in range (len (files )):
299+ for k , exp in expect [x ].items ():
300+ got = getattr (files [x ], k )
301+ self .assertEqual (got , exp )
302+
282303 _qs_result = {
283304 'key1' : 'value1' ,
284305 'key2' : ['value2x' , 'value2y' ],
@@ -428,6 +449,31 @@ def test_parse_header(self):
428449-----------------------------721837373350705526688164684
429450"""
430451
452+ # http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
453+ BOUNDARY_W3 = "AaB03x"
454+ POSTDATA_W3 = """--AaB03x
455+ Content-Disposition: form-data; name="submit-name"
456+
457+ Larry
458+ --AaB03x
459+ Content-Disposition: form-data; name="files"
460+ Content-Type: multipart/mixed; boundary=BbC04y
461+
462+ --BbC04y
463+ Content-Disposition: file; filename="file1.txt"
464+ Content-Type: text/plain
465+
466+ ... contents of file1.txt ...
467+ --BbC04y
468+ Content-Disposition: file; filename="file2.gif"
469+ Content-Type: image/gif
470+ Content-Transfer-Encoding: binary
471+
472+ ...contents of file2.gif...
473+ --BbC04y--
474+ --AaB03x--
475+ """
476+
431477
432478def test_main ():
433479 run_unittest (CgiTests )
0 commit comments