<s> import unittest <EOL> from urllib3 . filepost import encode_multipart_formdata , iter_fields <EOL> from urllib3 . fields import RequestField <EOL> from urllib3 . packages . six import b , u <EOL> BOUNDARY = '<STR_LIT>' <EOL> class TestIterfields ( unittest . TestCase ) : <EOL> def test_dict ( self ) : <EOL> for fieldname , value in iter_fields ( dict ( a = '<STR_LIT:b>' ) ) : <EOL> self . assertEqual ( ( fieldname , value ) , ( '<STR_LIT:a>' , '<STR_LIT:b>' ) ) <EOL> self . assertEqual ( <EOL> list ( sorted ( iter_fields ( dict ( a = '<STR_LIT:b>' , c = '<STR_LIT:d>' ) ) ) ) , <EOL> [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:d>' ) ] ) <EOL> def test_tuple_list ( self ) : <EOL> for fieldname , value in iter_fields ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) ] ) : <EOL> self . assertEqual ( ( fieldname , value ) , ( '<STR_LIT:a>' , '<STR_LIT:b>' ) ) <EOL> self . assertEqual ( <EOL> list ( iter_fields ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:d>' ) ] ) ) , <EOL> [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:d>' ) ] ) <EOL> class TestMultipartEncoding ( unittest . TestCase ) : <EOL> def test_input_datastructures ( self ) : <EOL> fieldsets = [ <EOL> dict ( k = '<STR_LIT:v>' , k2 = '<STR_LIT>' ) , <EOL> [ ( '<STR_LIT:k>' , '<STR_LIT:v>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> ] <EOL> for fields in fieldsets : <EOL> encoded , _ = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded . count ( b ( BOUNDARY ) ) , <NUM_LIT:3> ) <EOL> def test_field_encoding ( self ) : <EOL> fieldsets = [ <EOL> [ ( '<STR_LIT:k>' , '<STR_LIT:v>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> [ ( '<STR_LIT:k>' , b'<STR_LIT:v>' ) , ( u ( '<STR_LIT>' ) , b'<STR_LIT>' ) ] , <EOL> [ ( '<STR_LIT:k>' , b'<STR_LIT:v>' ) , ( u ( '<STR_LIT>' ) , '<STR_LIT>' ) ] , <EOL> ] <EOL> for fields in fieldsets : <EOL> encoded , content_type = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded , <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT>' <EOL> , fields ) <EOL> self . assertEqual ( content_type , <EOL> '<STR_LIT>' + str ( BOUNDARY ) ) <EOL> def test_filename ( self ) : <EOL> fields = [ ( '<STR_LIT:k>' , ( '<STR_LIT>' , b'<STR_LIT:v>' ) ) ] <EOL> encoded , content_type = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded , <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( content_type , <EOL> '<STR_LIT>' + str ( BOUNDARY ) ) <EOL> def test_textplain ( self ) : <EOL> fields = [ ( '<STR_LIT:k>' , ( '<STR_LIT>' , b'<STR_LIT:v>' ) ) ] <EOL> encoded , content_type = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded , <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( content_type , <EOL> '<STR_LIT>' + str ( BOUNDARY ) ) <EOL> def test_explicit ( self ) : <EOL> fields = [ ( '<STR_LIT:k>' , ( '<STR_LIT>' , b'<STR_LIT:v>' , '<STR_LIT>' ) ) ] <EOL> encoded , content_type = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded , <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( content_type , <EOL> '<STR_LIT>' + str ( BOUNDARY ) ) <EOL> def test_request_fields ( self ) : <EOL> fields = [ RequestField ( '<STR_LIT:k>' , b'<STR_LIT:v>' , filename = '<STR_LIT>' , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) ] <EOL> encoded , content_type = encode_multipart_formdata ( fields , boundary = BOUNDARY ) <EOL> self . assertEqual ( encoded , <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT:\r\n>' <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' + b ( BOUNDARY ) + b'<STR_LIT>' <EOL> ) </s>
