@@ -854,8 +854,8 @@ async def test_writer_serialize_with_content_encoding_gzip(buf, stream,
854
854
await writer .write (stream )
855
855
headers , message = bytes (buf ).split (b'\r \n \r \n ' , 1 )
856
856
857
- assert (b'--:\r \n Content-Encoding: gzip \r \n '
858
- b'Content-Type: text/plain; charset=utf-8 ' == headers )
857
+ assert (b'--:\r \n Content-Type: text/plain; charset=utf-8 \r \n '
858
+ b'Content-Encoding: gzip ' == headers )
859
859
860
860
decompressor = zlib .decompressobj (wbits = 16 + zlib .MAX_WBITS )
861
861
data = decompressor .decompress (message .split (b'\r \n ' )[0 ])
@@ -869,8 +869,8 @@ async def test_writer_serialize_with_content_encoding_deflate(buf, stream,
869
869
await writer .write (stream )
870
870
headers , message = bytes (buf ).split (b'\r \n \r \n ' , 1 )
871
871
872
- assert (b'--:\r \n Content-Encoding: deflate \r \n '
873
- b'Content-Type: text/plain; charset=utf-8 ' == headers )
872
+ assert (b'--:\r \n Content-Type: text/plain; charset=utf-8 \r \n '
873
+ b'Content-Encoding: deflate ' == headers )
874
874
875
875
thing = b'\x0b \xc9 \xcc MU(\xc9 W\x08 J\xcd I\xac P\x04 \x00 \r \n --:--\r \n '
876
876
assert thing == message
@@ -883,8 +883,8 @@ async def test_writer_serialize_with_content_encoding_identity(buf, stream,
883
883
await writer .write (stream )
884
884
headers , message = bytes (buf ).split (b'\r \n \r \n ' , 1 )
885
885
886
- assert (b'--:\r \n Content-Encoding: identity \r \n '
887
- b'Content-Type: application/octet-stream \r \n '
886
+ assert (b'--:\r \n Content-Type: application/octet-stream \r \n '
887
+ b'Content-Encoding: identity \r \n '
888
888
b'Content-Length: 16' == headers )
889
889
890
890
assert thing == message .split (b'\r \n ' )[0 ]
@@ -902,8 +902,8 @@ async def test_writer_with_content_transfer_encoding_base64(buf, stream,
902
902
await writer .write (stream )
903
903
headers , message = bytes (buf ).split (b'\r \n \r \n ' , 1 )
904
904
905
- assert (b'--:\r \n Content-Transfer-Encoding: base64 \r \n '
906
- b'Content-Type: text/plain; charset=utf-8 ' ==
905
+ assert (b'--:\r \n Content-Type: text/plain; charset=utf-8 \r \n '
906
+ b'Content-Transfer-Encoding: base64 ' ==
907
907
headers )
908
908
909
909
assert b'VGltZSB0byBSZWxheCE=' == message .split (b'\r \n ' )[0 ]
@@ -916,8 +916,8 @@ async def test_writer_content_transfer_encoding_quote_printable(buf, stream,
916
916
await writer .write (stream )
917
917
headers , message = bytes (buf ).split (b'\r \n \r \n ' , 1 )
918
918
919
- assert (b'--:\r \n Content-Transfer-Encoding: quoted-printable \r \n '
920
- b'Content-Type: text/plain; charset=utf-8 ' == headers )
919
+ assert (b'--:\r \n Content-Type: text/plain; charset=utf-8 \r \n '
920
+ b'Content-Transfer-Encoding: quoted-printable ' == headers )
921
921
922
922
assert (b'=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D1=82,'
923
923
b' =D0=BC=D0=B8=D1=80!' == message .split (b'\r \n ' )[0 ])
@@ -1049,6 +1049,95 @@ async def test_write_preserves_content_disposition(
1049
1049
)
1050
1050
assert message == b'foo\r \n --:--\r \n '
1051
1051
1052
+ async def test_preserve_content_disposition_header (self , buf , stream ):
1053
+ """
1054
+ https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
1055
+ """
1056
+ with open (__file__ , 'rb' ) as fobj :
1057
+ with aiohttp .MultipartWriter ('form-data' , boundary = ':' ) as writer :
1058
+ part = writer .append (
1059
+ fobj ,
1060
+ headers = {
1061
+ CONTENT_DISPOSITION : 'attachments; filename="bug.py"' ,
1062
+ CONTENT_TYPE : 'text/python' ,
1063
+ }
1064
+ )
1065
+ content_length = part .size
1066
+ await writer .write (stream )
1067
+
1068
+ assert part .headers [CONTENT_TYPE ] == 'text/python'
1069
+ assert part .headers [CONTENT_DISPOSITION ] == (
1070
+ 'attachments; filename="bug.py"'
1071
+ )
1072
+
1073
+ headers , _ = bytes (buf ).split (b'\r \n \r \n ' , 1 )
1074
+
1075
+ assert headers == (
1076
+ b'--:\r \n '
1077
+ b'Content-Type: text/python\r \n '
1078
+ b'Content-Disposition: attachments; filename="bug.py"\r \n '
1079
+ b'Content-Length: %s'
1080
+ b'' % (str (content_length ).encode (),)
1081
+ )
1082
+
1083
+ async def test_set_content_disposition_override (self , buf , stream ):
1084
+ """
1085
+ https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
1086
+ """
1087
+ with open (__file__ , 'rb' ) as fobj :
1088
+ with aiohttp .MultipartWriter ('form-data' , boundary = ':' ) as writer :
1089
+ part = writer .append (
1090
+ fobj ,
1091
+ headers = {
1092
+ CONTENT_DISPOSITION : 'attachments; filename="bug.py"' ,
1093
+ CONTENT_TYPE : 'text/python' ,
1094
+ }
1095
+ )
1096
+ content_length = part .size
1097
+ await writer .write (stream )
1098
+
1099
+ assert part .headers [CONTENT_TYPE ] == 'text/python'
1100
+ assert part .headers [CONTENT_DISPOSITION ] == (
1101
+ 'attachments; filename="bug.py"'
1102
+ )
1103
+
1104
+ headers , _ = bytes (buf ).split (b'\r \n \r \n ' , 1 )
1105
+
1106
+ assert headers == (
1107
+ b'--:\r \n '
1108
+ b'Content-Type: text/python\r \n '
1109
+ b'Content-Disposition: attachments; filename="bug.py"\r \n '
1110
+ b'Content-Length: %s'
1111
+ b'' % (str (content_length ).encode (),)
1112
+ )
1113
+
1114
+ async def test_reset_content_disposition_header (self , buf , stream ):
1115
+ """
1116
+ https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
1117
+ """
1118
+ with open (__file__ , 'rb' ) as fobj :
1119
+ with aiohttp .MultipartWriter ('form-data' , boundary = ':' ) as writer :
1120
+ part = writer .append (fobj )
1121
+
1122
+ content_length = part .size
1123
+
1124
+ assert CONTENT_DISPOSITION in part .headers
1125
+
1126
+ part .set_content_disposition ('attachments' , filename = 'bug.py' )
1127
+
1128
+ await writer .write (stream )
1129
+
1130
+ headers , _ = bytes (buf ).split (b'\r \n \r \n ' , 1 )
1131
+
1132
+ assert headers == (
1133
+ b'--:\r \n '
1134
+ b'Content-Type: text/x-python\r \n '
1135
+ b'Content-Disposition:'
1136
+ b' attachments; filename="bug.py"; filename*=utf-8\' \' bug.py\r \n '
1137
+ b'Content-Length: %s'
1138
+ b'' % (str (content_length ).encode (),)
1139
+ )
1140
+
1052
1141
1053
1142
async def test_async_for_reader () -> None :
1054
1143
data = [
0 commit comments