Skip to content
This repository was archived by the owner on Jun 11, 2018. It is now read-only.

Commit 2373768

Browse files
committed
Include request.POST, cope/test for multipart/form-data
1 parent 78917a7 commit 2373768

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

opbeat/contrib/django/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ def get_data_from_request(self, request):
116116
raw_data = request.raw_post_data
117117
data = raw_data if raw_data else request.POST
118118
except Exception as exc:
119-
# assume we had a partial read:
120-
data = '<unavailable ({0}: {1})>'.format(
121-
exc.__class__.__name__, exc)
119+
# Assume we had a partial read, or multipart/form-data:
120+
data = '<unavailable ({0}: {1})>\nrequest.POST: {2}'.format(
121+
exc.__class__.__name__, exc, request.POST)
122122
else:
123123
data = None
124124

tests/contrib/django/django_tests.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from django.http import QueryDict
2020
from django.template import TemplateSyntaxError
2121
from django.test import TestCase
22-
from django.test.client import Client as _TestClient
22+
from django.test.client import Client as _TestClient, FakePayload
2323
from django.test.client import ClientHandler as _TestClientHandler
2424
from django.test.utils import override_settings
2525

@@ -522,7 +522,8 @@ def test_raw_post_data_partial_read(self):
522522
expected_exception = 'RawPostDataException'
523523
else:
524524
expected_exception = 'Exception'
525-
self.assertEquals(http['data'], "<unavailable ({0}: You cannot access body after reading from request's data stream)>".format(expected_exception))
525+
self.assertEquals(http['data'], "<unavailable ({0}: You cannot access body after reading from request's data stream)>\nrequest.POST: {1}".format(expected_exception, request.POST))
526+
self.assertEquals(request.POST, {})
526527

527528
def test_post_data(self):
528529
request = WSGIRequest(environ={
@@ -602,7 +603,7 @@ def test_disallowed_hosts_error_django_18(self):
602603
self.assertEqual(event['http']['url'], None)
603604

604605
# This test only applies to Django 1.3+
605-
def test_request_capture(self):
606+
def test_request_capture_partial_read(self):
606607
if django.VERSION[:2] < (1, 3):
607608
return
608609
request = WSGIRequest(environ={
@@ -627,7 +628,8 @@ def test_request_capture(self):
627628
expected_exception = 'RawPostDataException'
628629
else:
629630
expected_exception = 'Exception'
630-
self.assertEquals(http['data'], "<unavailable ({0}: You cannot access body after reading from request's data stream)>".format(expected_exception))
631+
self.assertEquals(http['data'], "<unavailable ({0}: You cannot access body after reading from request's data stream)>\nrequest.POST: {1}".format(expected_exception, request.POST))
632+
# self.assertEquals(http['data'], '{}')
631633
self.assertTrue('headers' in http)
632634
headers = http['headers']
633635
self.assertTrue('Content-Type' in headers, headers.keys())
@@ -638,6 +640,52 @@ def test_request_capture(self):
638640
self.assertTrue('SERVER_PORT' in env, env.keys())
639641
self.assertEquals(env['SERVER_PORT'], '80')
640642

643+
def test_request_capture_multipart_formdata(self):
644+
if django.VERSION[:2] < (1, 3):
645+
return
646+
647+
payload = FakePayload("\r\n".join([
648+
'--boundary',
649+
'Content-Disposition: form-data; name="name"',
650+
'',
651+
'value',
652+
'--boundary--'
653+
'']))
654+
request = WSGIRequest({
655+
'REQUEST_METHOD': 'POST',
656+
'SERVER_NAME': 'testserver',
657+
'SERVER_PORT': '80',
658+
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
659+
'CONTENT_LENGTH': len(payload),
660+
'wsgi.input': payload})
661+
662+
# Read POST data to trigger exception when accessing request.body.
663+
self.assertEqual(request.POST, {'name': ['value']})
664+
665+
self.opbeat.capture('Message', message='foo', request=request)
666+
667+
self.assertEquals(len(self.opbeat.events), 1)
668+
event = self.opbeat.events.pop(0)
669+
670+
self.assertTrue('http' in event)
671+
http = event['http']
672+
self.assertEquals(http['method'], 'POST')
673+
if django.VERSION >= (1, 7):
674+
expected_exception = 'RawPostDataException'
675+
else:
676+
expected_exception = 'Exception'
677+
self.assertEquals(http['data'], "<unavailable ({0}: You cannot access body after reading from request's data stream)>\nrequest.POST: {1}".format(expected_exception, request.POST))
678+
# self.assertEquals(http['data'], '{}')
679+
self.assertTrue('headers' in http)
680+
headers = http['headers']
681+
self.assertTrue('Content-Type' in headers, headers.keys())
682+
self.assertEquals(headers['Content-Type'], 'multipart/form-data; boundary=boundary')
683+
env = http['env']
684+
self.assertTrue('SERVER_NAME' in env, env.keys())
685+
self.assertEquals(env['SERVER_NAME'], 'testserver')
686+
self.assertTrue('SERVER_PORT' in env, env.keys())
687+
self.assertEquals(env['SERVER_PORT'], '80')
688+
641689
def test_transaction_metrics(self):
642690
self.opbeat.instrumentation_store.get_all() # clear the store
643691
with self.settings(MIDDLEWARE_CLASSES=[

0 commit comments

Comments
 (0)