From e33e200c1205d4d12a7a3c5e04d809194ee68953 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Thu, 3 May 2018 16:54:58 +0200 Subject: [PATCH] Handle application/json data input (port of #546 to 2.x) --- .../Middleware/RequestInterfaceMiddleware.php | 5 + .../RequestInterfaceMiddlewareTest.php | 99 ++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/lib/Raven/Middleware/RequestInterfaceMiddleware.php b/lib/Raven/Middleware/RequestInterfaceMiddleware.php index a82091bcc..b656acb7a 100644 --- a/lib/Raven/Middleware/RequestInterfaceMiddleware.php +++ b/lib/Raven/Middleware/RequestInterfaceMiddleware.php @@ -53,6 +53,11 @@ public function __invoke(Event $event, callable $next, ServerRequestInterface $r if ($request->hasHeader('REMOTE_ADDR')) { $requestData['env']['REMOTE_ADDR'] = $request->getHeaderLine('REMOTE_ADDR'); } + + if (in_array('application/json', $request->getHeader('Content-Type'))) { + $inputData = file_get_contents('php://input'); + $requestData['data'] = json_decode($inputData, true); + } $event = $event->withRequest($requestData); diff --git a/tests/Middleware/RequestInterfaceMiddlewareTest.php b/tests/Middleware/RequestInterfaceMiddlewareTest.php index a4e74875a..864f00a02 100644 --- a/tests/Middleware/RequestInterfaceMiddlewareTest.php +++ b/tests/Middleware/RequestInterfaceMiddlewareTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Raven\Tests\Breadcrumbs; +namespace Raven\Tests\Middleware; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; @@ -21,6 +21,8 @@ class RequestInterfaceMiddlewareTest extends TestCase { + private static $inputData; + public function testInvokeWithNoRequest() { $configuration = new Configuration(); @@ -42,7 +44,7 @@ public function testInvokeWithNoRequest() /** * @dataProvider invokeDataProvider */ - public function testInvoke($requestData, $expectedValue) + public function testInvoke(array $requestData, array $expectedValue, $inputData = null) { $configuration = new Configuration(); $event = new Event($configuration); @@ -65,6 +67,10 @@ public function testInvoke($requestData, $expectedValue) }; $middleware = new RequestInterfaceMiddleware(); + if ($inputData) { + $this->mockReadFromPhpInput($inputData); + } + $middleware($event, $callback, $request); $this->assertEquals(1, $invokationCount); @@ -72,6 +78,9 @@ public function testInvoke($requestData, $expectedValue) public function invokeDataProvider() { + $validData = ['json_test' => 'json_data']; + $invalidData = '{"binary_json":"' . pack('NA3CC', 3, 'aBc', 0x0D, 0x0A) . '"}'; + return [ [ [ @@ -117,6 +126,92 @@ public function invokeDataProvider() ], ], ], + [ + [ + 'uri' => 'http://www.example.com/foo', + 'method' => 'POST', + 'cookies' => [], + 'headers' => [ + 'Content-Type' => ['application/json'], + 'Host' => ['www.example.com'], + 'REMOTE_ADDR' => ['127.0.0.1'], + ], + ], + [ + 'url' => 'http://www.example.com/foo', + 'method' => 'POST', + 'cookies' => [], + 'data' => $validData, + 'headers' => [ + 'Content-Type' => ['application/json'], + 'Host' => ['www.example.com'], + 'REMOTE_ADDR' => ['127.0.0.1'], + ], + 'env' => [ + 'REMOTE_ADDR' => '127.0.0.1', + ], + ], + json_encode($validData), + ], + [ + [ + 'uri' => 'http://www.example.com/foo', + 'method' => 'POST', + 'cookies' => [], + 'headers' => [ + 'Content-Type' => ['application/json'], + 'Host' => ['www.example.com'], + 'REMOTE_ADDR' => ['127.0.0.1'], + ], + ], + [ + 'url' => 'http://www.example.com/foo', + 'method' => 'POST', + 'cookies' => [], + 'data' => null, + 'headers' => [ + 'Content-Type' => ['application/json'], + 'Host' => ['www.example.com'], + 'REMOTE_ADDR' => ['127.0.0.1'], + ], + 'env' => [ + 'REMOTE_ADDR' => '127.0.0.1', + ], + ], + $invalidData, + ], ]; } + + /** + * @return string + */ + public static function getInputData() + { + return self::$inputData; + } + + /** + * @param string $inputData + */ + private function mockReadFromPhpInput($inputData) + { + self::$inputData = $inputData; + if (function_exists('Raven\Middleware\file_get_contents')) { + return; + } + + $self = \get_class($this); + + eval(<<