def test_POST_after_body_read_and_stream_read(self): """ POST should be populated even if body is read first, and then the stream is read second. """ payload = b"name=value" request = WSGIRequest( {"REQUEST_METHOD": "POST", "CONTENT_LENGTH": len(payload), "wsgi.input": BytesIO(payload)} ) raw_data = request.body self.assertEqual(request.read(1), b"n") self.assertEqual(request.POST, {"name": ["value"]})
def test_read_after_value(self): """ Reading from request is allowed after accessing request contents as POST or body. """ payload = b"name=value" request = WSGIRequest( {"REQUEST_METHOD": "POST", "CONTENT_LENGTH": len(payload), "wsgi.input": BytesIO(payload)} ) self.assertEqual(request.POST, {"name": ["value"]}) self.assertEqual(request.body, b"name=value") self.assertEqual(request.read(), b"name=value")
def test_value_after_read(self): """ Construction of POST or body is not allowed after reading from request. """ payload = b"name=value" request = WSGIRequest( {"REQUEST_METHOD": "POST", "CONTENT_LENGTH": len(payload), "wsgi.input": BytesIO(payload)} ) self.assertEqual(request.read(2), b"na") self.assertRaises(Exception, lambda: request.body) self.assertEqual(request.POST, {})
def test_POST_after_body_read_and_stream_read_multipart(self): """ POST should be populated even if body is read first, and then the stream is read second. Using multipart/form-data instead of urlencoded. """ payload = "\r\n".join( ["--boundary", 'Content-Disposition: form-data; name="name"', "", "value", "--boundary--" ""] ).encode("utf-8") request = WSGIRequest( { "REQUEST_METHOD": "POST", "CONTENT_TYPE": "multipart/form-data; boundary=boundary", "CONTENT_LENGTH": len(payload), "wsgi.input": BytesIO(payload), } ) raw_data = request.body # Consume enough data to mess up the parsing: self.assertEqual(request.read(13), b"--boundary\r\nC") self.assertEqual(request.POST, {"name": ["value"]})
def test_stream(self): payload = b"name=value" request = WSGIRequest( {"REQUEST_METHOD": "POST", "CONTENT_LENGTH": len(payload), "wsgi.input": BytesIO(payload)} ) self.assertEqual(request.read(), b"name=value")