def test_stream(self): payload = FakePayload('name=value') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload }) self.assertEqual(request.read(), b'name=value')
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 = FakePayload('name=value') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload, }) request.body # evaluate 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 = FakePayload('name=value') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload, }) self.assertEqual(request.POST, {'name': ['value']}) self.assertEqual(request.body, b'name=value') self.assertEqual(request.read(), b'name=value')
def test_read_after_value(self): """ Reading from request is allowed after accessing request contents as data or body. """ for method in HTTP_METHODS_WITH_BODY: payload = FakePayload('{"name": "value"}') request = WSGIRequest({ 'REQUEST_METHOD': method, 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload }) self.assertEqual(request.data, {'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 = FakePayload('name=value') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload, }) self.assertEqual(request.read(2), b'na') with self.assertRaises(RawPostDataException): request.body self.assertEqual(request.POST, {})
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. """ for method in HTTP_METHODS_WITH_BODY: payload = FakePayload('{"name": "佚名"}') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload }) request.body # evaluate self.assertEqual(request.read(1), b'{') self.assertEqual(request.POST, {'name': '佚名'}) self.assertEqual(request.data, {'name': '佚名'})
def test_value_after_read(self): """ Construction of POST or body is not allowed after reading from request. """ for method in HTTP_METHODS_WITH_BODY: payload = FakePayload('{"name": "value"}') request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload }) self.assertEqual(request.read(2), b'{"') with self.assertRaises(RawPostDataException): request.body self.assertEqual(request.data, {})
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 = FakePayload("\r\n".join([ '--boundary', 'Content-Disposition: form-data; name="name"', '', 'value', '--boundary--' '' ])) request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary=boundary', 'CONTENT_LENGTH': len(payload), 'wsgi.input': payload, }) request.body # evaluate # Consume enough data to mess up the parsing: self.assertEqual(request.read(13), b'--boundary\r\nC') self.assertEqual(request.POST, {'name': ['value']})