Skip to content

Commit

Permalink
Handle application/json data input (port of #546 to 2.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed May 4, 2018
1 parent 8d15022 commit e33e200
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/Raven/Middleware/RequestInterfaceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
99 changes: 97 additions & 2 deletions tests/Middleware/RequestInterfaceMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +21,8 @@

class RequestInterfaceMiddlewareTest extends TestCase
{
private static $inputData;

public function testInvokeWithNoRequest()
{
$configuration = new Configuration();
Expand All @@ -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);
Expand All @@ -65,13 +67,20 @@ public function testInvoke($requestData, $expectedValue)
};

$middleware = new RequestInterfaceMiddleware();
if ($inputData) {
$this->mockReadFromPhpInput($inputData);
}

$middleware($event, $callback, $request);

$this->assertEquals(1, $invokationCount);
}

public function invokeDataProvider()
{
$validData = ['json_test' => 'json_data'];
$invalidData = '{"binary_json":"' . pack('NA3CC', 3, 'aBc', 0x0D, 0x0A) . '"}';

return [
[
[
Expand Down Expand Up @@ -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(<<<EOPHP
namespace Raven\Middleware;
function file_get_contents()
{
return \\$self::getInputData();
}
EOPHP
);
}
}

0 comments on commit e33e200

Please sign in to comment.