From e1f4d53ab5b67b4af06e35f2b81a233dc988a093 Mon Sep 17 00:00:00 2001 From: Ryan White Date: Mon, 12 Feb 2018 11:45:23 -0500 Subject: [PATCH] Updated tests for neggative test case --- lib/Raven/Client.php | 2 +- test/Raven/Tests/ClientTest.php | 46 +++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/Raven/Client.php b/lib/Raven/Client.php index c8c1aafb4f..7640a9b443 100644 --- a/lib/Raven/Client.php +++ b/lib/Raven/Client.php @@ -759,7 +759,7 @@ protected function get_http_data() // instead of a mapping which goes against the defined Sentry spec if (!empty($_POST)) { $result['data'] = $_POST; - } elseif (isset($_SERVER['CONTENT_TYPE']) && stripos($_SERVER['CONTENT_TYPE'], 'application/json') === 0) { + } elseif (PHP_VERSION_ID > 50600 && isset($_SERVER['CONTENT_TYPE']) && stripos($_SERVER['CONTENT_TYPE'], 'application/json') === 0) { $raw_data = $this->getInputStream() ?: false; if ($raw_data !== false) { $result['data'] = (array) json_decode($raw_data, true) ?: null; diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index 2a99dd4e3d..0c63e189f5 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -65,7 +65,7 @@ public function get_user_data() public function setInputStream($input) { - static::$input_stream = isset($_SERVER['CONTENT_TYPE']) ? json_encode($input) : 'test=foo'; + static::$input_stream = isset($_SERVER['CONTENT_TYPE']) ? $input : false; } protected static function getInputStream() @@ -885,11 +885,53 @@ public function testGetHttpDataApplicationJson() ); $client = new Dummy_Raven_Client(); - $client->setInputStream(array('json_test' => 'json_data')); + $client->setInputStream(json_encode(array('json_test' => 'json_data'))); $this->assertEquals($expected, $client->get_http_data()); } + /** + * Test showing that invalid json will be discarded from data collection. + */ + public function testGetHttpDataApplicationInvalidJson() + { + $_SERVER = array( + 'REDIRECT_STATUS' => '200', + 'CONTENT_TYPE' => 'application/json', + 'CONTENT_LENGTH' => '99', + 'HTTP_HOST' => 'getsentry.com', + 'HTTP_ACCEPT' => 'text/html', + 'HTTP_ACCEPT_CHARSET' => 'utf-8', + 'HTTP_COOKIE' => 'cupcake: strawberry', + 'SERVER_PORT' => '443', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REQUEST_METHOD' => 'POST', + 'REQUEST_URI' => '/welcome/', + 'SCRIPT_NAME' => '/index.php', + ); + + $expected = array( + 'request' => array( + 'method' => 'POST', + 'url' => 'https://getsentry.com/welcome/', + 'query_string' => '', + 'data' => null, + 'headers' => array( + 'Host' => 'getsentry.com', + 'Accept' => 'text/html', + 'Accept-Charset' => 'utf-8', + 'Cookie' => 'cupcake: strawberry', + 'Content-Type' => 'application/json', + 'Content-Length' => '99', + ), + ) + ); + + $client = new Dummy_Raven_Client(); + $client->setInputStream('{"binary_json":"'.pack("NA3CC", 3, "aBc", 0x0D, 0x0A).'"}'); + $this->assertEquals($expected, $client->get_http_data()); + } + /** * @covers Raven_Client::user_context * @covers Raven_Client::get_user_data