def test_raw_post_data_partial_read(self): if django.VERSION[:2] < (1, 3): return v = '{"foo": "bar"}' request = WSGIRequest( environ={ "wsgi.input": StringIO(v + "\r\n\r\n"), "REQUEST_METHOD": "POST", "SERVER_NAME": "testserver", "SERVER_PORT": "80", "CONTENT_TYPE": "application/octet-stream", "CONTENT_LENGTH": len(v), "ACCEPT": "application/json", } ) request.read(1) self.raven.capture("Message", message="foo", request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertTrue("sentry.interfaces.Http" in event) http = event["sentry.interfaces.Http"] self.assertEquals(http["method"], "POST") self.assertEquals(http["data"], "<unavailable>")
def test_request_capture(self): if django.VERSION[:2] < (1, 3): return request = WSGIRequest( environ={ 'wsgi.input': StringIO(), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'text/html', 'ACCEPT': 'text/html', }) request.read(1) self.raven.capture('Message', message='foo', request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertTrue('sentry.interfaces.Http' in event) http = event['sentry.interfaces.Http'] self.assertEquals(http['method'], 'POST') self.assertEquals(http['data'], '<unavailable>') self.assertTrue('headers' in http) headers = http['headers'] self.assertTrue('Content-Type' in headers, headers.keys()) self.assertEquals(headers['Content-Type'], 'text/html') env = http['env'] self.assertTrue('SERVER_NAME' in env, env.keys()) self.assertEquals(env['SERVER_NAME'], 'testserver') self.assertTrue('SERVER_PORT' in env, env.keys()) self.assertEquals(env['SERVER_PORT'], '80')
def test_request_capture(self): request = WSGIRequest( environ={ 'wsgi.input': StringIO(), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'text/html', 'ACCEPT': 'text/html', }) request.read(1) self.raven.capture('Message', message='foo', request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertTrue('sentry.interfaces.Http' in event) http = event['sentry.interfaces.Http'] self.assertEquals(http['method'], 'POST') self.assertEquals(http['data'], '<unavailable>') self.assertTrue('headers' in http) headers = http['headers'] self.assertTrue('Content-Type' in headers, headers.keys()) self.assertEquals(headers['Content-Type'], 'text/html') env = http['env'] self.assertTrue('SERVER_NAME' in env, env.keys()) self.assertEquals(env['SERVER_NAME'], 'testserver') self.assertTrue('SERVER_PORT' in env, env.keys()) self.assertEquals(env['SERVER_PORT'], '80')
def test_request_capture(self): if django.VERSION[:2] < (1, 3): return request = WSGIRequest( environ={ "wsgi.input": StringIO(), "REQUEST_METHOD": "POST", "SERVER_NAME": "testserver", "SERVER_PORT": "80", "CONTENT_TYPE": "text/html", "ACCEPT": "text/html", } ) request.read(1) self.raven.capture("Message", message="foo", request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertTrue("sentry.interfaces.Http" in event) http = event["sentry.interfaces.Http"] self.assertEquals(http["method"], "POST") self.assertEquals(http["data"], "<unavailable>") self.assertTrue("headers" in http) headers = http["headers"] self.assertTrue("Content-Type" in headers, headers.keys()) self.assertEquals(headers["Content-Type"], "text/html") env = http["env"] self.assertTrue("SERVER_NAME" in env, env.keys()) self.assertEquals(env["SERVER_NAME"], "testserver") self.assertTrue("SERVER_PORT" in env, env.keys()) self.assertEquals(env["SERVER_PORT"], "80")
def test_raw_post_data_partial_read(self): if django.VERSION[:2] < (1, 3): return v = '{"foo": "bar"}' request = WSGIRequest( environ={ 'wsgi.input': StringIO(v + '\r\n\r\n'), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/octet-stream', 'CONTENT_LENGTH': len(v), 'ACCEPT': 'application/json', }) request.read(1) self.raven.capture('Message', message='foo', request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertTrue('sentry.interfaces.Http' in event) http = event['sentry.interfaces.Http'] self.assertEquals(http['method'], 'POST') self.assertEquals(http['data'], '<unavailable>')
def test_request_capture(django_elasticapm_client): request = WSGIRequest(environ={ 'wsgi.input': compat.BytesIO(), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'text/html', 'ACCEPT': 'text/html', }) request.read(1) django_elasticapm_client.capture('Message', message='foo', request=request) assert len(django_elasticapm_client.events) == 1 event = django_elasticapm_client.events.pop(0)['errors'][0] assert 'request' in event['context'] request = event['context']['request'] assert request['method'] == 'POST' assert request['body'] == '<unavailable>' assert 'headers' in request headers = request['headers'] assert 'content-type' in headers, headers.keys() assert headers['content-type'] == 'text/html' env = request['env'] assert 'SERVER_NAME' in env, env.keys() assert env['SERVER_NAME'] == 'testserver' assert 'SERVER_PORT' in env, env.keys() assert env['SERVER_PORT'] == '80'
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_stream(self): payload = 'name=value' request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload) }) self.assertEqual(request.read(), 'name=value')
def test_POST_after_raw_post_data_read_and_stream_read(self): """ POST should be populated even if raw_post_data is read first, and then the stream is read second. """ request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')}) raw_data = request.raw_post_data self.assertEqual(request.read(1), u'n') self.assertEqual(request.POST, {u'name': [u'value']})
def test_read_after_value(self): """ Reading from request is allowed after accessing request contents as POST or raw_post_data. """ request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')}) self.assertEqual(request.POST, {u'name': [u'value']}) self.assertEqual(request.raw_post_data, 'name=value') self.assertEqual(request.read(), 'name=value')
def test_value_after_read(self): """ Construction of POST or raw_post_data is not allowed after reading from request. """ request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')}) self.assertEqual(request.read(2), 'na') self.assertRaises(Exception, lambda: request.raw_post_data) self.assertEqual(request.POST, {})
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_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_raw_post_data_partial_read(self): # This test only applies to Django 1.3+ v = '{"foo": "bar"}' request = WSGIRequest(environ={ 'wsgi.input': StringIO(v + '\r\n\r\n'), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/octet-stream', 'CONTENT_LENGTH': len(v), 'ACCEPT': 'application/json', }) request.read(1) self.raven.process(request=request) self.assertEquals(len(self.raven.events), 1) event = self.raven.events.pop(0) self.assertEquals(event['data']['POST'], '<unavailable>')
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_value_after_read(self): """ Construction of POST or body is not allowed after reading from request. """ payload = 'name=value' request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload)}) self.assertEqual(request.read(2), 'na') self.assertRaises(Exception, lambda: 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. """ payload = 'name=value' request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload)}) raw_data = request.body self.assertEqual(request.read(1), u'n') self.assertEqual(request.POST, {u'name': [u'value']})
def test_read_after_value(self): """ Reading from request is allowed after accessing request contents as POST or body. """ payload = 'name=value' request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload)}) self.assertEqual(request.POST, {u'name': [u'value']}) self.assertEqual(request.body, 'name=value') self.assertEqual(request.read(), '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 = 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 = 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') self.assertRaises(Exception, lambda: 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. """ payload = FakePayload('name=value') request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': len(payload), 'wsgi.input': 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 = 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_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_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') self.assertRaises(RawPostDataException, lambda: request.body) self.assertEqual(request.POST, {})
def test_raw_post_data_partial_read(django_elasticapm_client): v = compat.b('{"foo": "bar"}') request = WSGIRequest(environ={ 'wsgi.input': compat.BytesIO(v + compat.b('\r\n\r\n')), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': len(v), 'ACCEPT': 'application/json', }) request.read(1) django_elasticapm_client.capture('Message', message='foo', request=request) assert len(django_elasticapm_client.events) == 1 event = django_elasticapm_client.events.pop(0)['errors'][0] assert 'request' in event['context'] request = event['context']['request'] assert request['method'] == 'POST' assert request['body'] == '<unavailable>'
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_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_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_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") self.assertRaises(Exception, lambda: 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. """ payload = FakePayload("name=value") request = WSGIRequest( { "REQUEST_METHOD": "POST", "CONTENT_TYPE": "application/x-www-form-urlencoded", "CONTENT_LENGTH": len(payload), "wsgi.input": payload, } ) raw_data = request.body self.assertEqual(request.read(1), b"n") self.assertEqual(request.POST, {"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 = 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, } ) 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_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}) 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_POST_after_raw_post_data_read_and_stream_read_multipart(self): """ POST should be populated even if raw_post_data 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--' '' ]) request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary=boundary', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload) }) raw_data = request.raw_post_data # Consume enough data to mess up the parsing: self.assertEqual(request.read(13), u'--boundary\r\nC') self.assertEqual(request.POST, {u'name': [u'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 = 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"]})
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')
def test_stream(self): request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')}) self.assertEqual(request.read(), 'name=value')
def test_stream(self): request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value') }) self.assertEqual(request.read(), 'name=value')