@@ -131,3 +131,96 @@ def testPostRequestWithQueryWithSemicolon2(self):
131131 self .assertEqual (fs .getlist ('a' ), ['1' ])
132132 self .assertEqual (fs .getlist ('b' ), ['2' , '3' ])
133133 self .assertEqual (fs .getlist ('c' ), ['3' ])
134+
135+ def testPostRequestWithoutContentLength (self ):
136+ # see https://github.com/python/cpython/issues/71964
137+ fs = FieldStorage (
138+ fp = BytesIO (b'{"test":123}' ),
139+ environ = {'REQUEST_METHOD' : 'POST' ,
140+ 'CONTENT_TYPE' : 'application/json' })
141+ self .assertEqual (fs .headers , {
142+ 'content-type' : 'application/json' })
143+ self .assertEqual (fs .type , 'application/json' )
144+ self .assertEqual (fs .length , - 1 )
145+ self .assertEqual (fs .bytes_read , 12 )
146+ assert fs .file .read () == '{"test":123}'
147+
148+ def testPostRequestWithContentLengthAndContentDispositionInline (self ):
149+ # see https://github.com/python/cpython/issues/71964
150+ fs = FieldStorage (
151+ fp = BytesIO (b'{"test":123}' ),
152+ headers = {'content-length' : 12 , 'content-disposition' : 'inline' ,
153+ 'content-type' : 'application/json' },
154+ environ = {'REQUEST_METHOD' : 'POST' })
155+ self .assertEqual (fs .headers , {
156+ 'content-type' : 'application/json' , 'content-length' : 12 ,
157+ 'content-disposition' : 'inline' })
158+ self .assertEqual (fs .disposition , 'inline' )
159+ self .assertIsNone (fs .filename )
160+ self .assertEqual (fs .type , 'application/json' )
161+ self .assertEqual (fs .length , 12 )
162+ self .assertEqual (fs .bytes_read , 12 )
163+ self .assertEqual (fs .file .read (), '{"test":123}' )
164+
165+ def testPostRequestWithContentLengthAndContentDispositionAttachment (self ):
166+ # not affected by https://github.com/python/cpython/issues/71964
167+ fs = FieldStorage (
168+ fp = BytesIO (b'{"test":123}' ),
169+ headers = {'content-length' : 12 ,
170+ 'content-disposition' : 'attachment; filename="foo.json"' ,
171+ 'content-type' : 'application/json' },
172+ environ = {'REQUEST_METHOD' : 'POST' })
173+ self .assertEqual (fs .headers , {
174+ 'content-type' : 'application/json' , 'content-length' : 12 ,
175+ 'content-disposition' : 'attachment; filename="foo.json"' })
176+ self .assertEqual (fs .disposition , 'attachment' )
177+ self .assertEqual (fs .filename , 'foo.json' )
178+ self .assertEqual (fs .type , 'application/json' )
179+ self .assertEqual (fs .length , 12 )
180+ self .assertEqual (fs .bytes_read , 12 )
181+ self .assertEqual (fs .file .read (), b'{"test":123}' )
182+
183+ def testPostRequestWithContentLengthButWithoutContentDisposition (self ):
184+ # see https://github.com/python/cpython/issues/71964
185+ fs = FieldStorage (fp = BytesIO (b'{"test":123}' ), environ = {
186+ 'CONTENT_LENGTH' : 12 , 'REQUEST_METHOD' : 'POST' ,
187+ 'CONTENT_TYPE' : 'application/json' })
188+ self .assertEqual (fs .headers , {
189+ 'content-type' : 'application/json' , 'content-length' : 12 })
190+ self .assertEqual (fs .disposition , '' )
191+ self .assertEqual (fs .type , 'application/json' )
192+ self .assertEqual (fs .length , 12 )
193+ self .assertEqual (fs .bytes_read , 12 )
194+ self .assertEqual (fs .file .read (), '{"test":123}' )
195+
196+ def testPostRequestWithUtf8BinaryData (self ):
197+ text = 'The \u2603 by Raymond Briggs'
198+ content = text .encode ('utf-8' )
199+ length = len (content )
200+ fs = FieldStorage (fp = BytesIO (content ), environ = {
201+ 'CONTENT_LENGTH' : length , 'REQUEST_METHOD' : 'POST' ,
202+ 'CONTENT_TYPE' : 'application/octet-stream' })
203+ self .assertEqual (fs .headers , {
204+ 'content-type' : 'application/octet-stream' ,
205+ 'content-length' : length })
206+ self .assertEqual (fs .type , 'application/octet-stream' )
207+ self .assertEqual (fs .length , length )
208+ self .assertEqual (fs .bytes_read , length )
209+ self .assertEqual (fs .file .read (), text )
210+
211+ def testPostRequestWithNonUtf8BinaryData (self ):
212+ # see https://github.com/WebwareForPython/w4py3/issues/14
213+ content = b'\xfe \xff \xc0 '
214+ with self .assertRaises (UnicodeDecodeError ):
215+ content .decode ('utf-8' )
216+ length = len (content )
217+ fs = FieldStorage (fp = BytesIO (content ), environ = {
218+ 'CONTENT_LENGTH' : length , 'REQUEST_METHOD' : 'POST' ,
219+ 'CONTENT_TYPE' : 'application/octet-stream' })
220+ self .assertEqual (fs .headers , {
221+ 'content-type' : 'application/octet-stream' ,
222+ 'content-length' : length })
223+ self .assertEqual (fs .type , 'application/octet-stream' )
224+ self .assertEqual (fs .length , length )
225+ self .assertEqual (fs .bytes_read , length )
226+ self .assertEqual (fs .file .read (), content )
0 commit comments