def test_wsgirequest_repr(self): request = WSGIRequest({"PATH_INFO": "/somepath/", "REQUEST_METHOD": "get", "wsgi.input": BytesIO(b"")}) request.GET = {"get-key": "get-value"} request.POST = {"post-key": "post-value"} request.COOKIES = {"post-key": "post-value"} request.META = {"post-key": "post-value"} self.assertEqual( repr(request), str_prefix( "<WSGIRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>" ), ) self.assertEqual(build_request_repr(request), repr(request)) self.assertEqual( build_request_repr( request, path_override="/otherpath/", GET_override={"a": "b"}, POST_override={"c": "d"}, COOKIES_override={"e": "f"}, META_override={"g": "h"}, ), str_prefix( "<WSGIRequest\npath:/otherpath/,\nGET:{%(_)s'a': %(_)s'b'},\nPOST:{%(_)s'c': %(_)s'd'},\nCOOKIES:{%(_)s'e': %(_)s'f'},\nMETA:{%(_)s'g': %(_)s'h'}>" ), )
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_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_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_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")