Skip to content

Commit

Permalink
Updated tests for neggative test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan White committed Feb 12, 2018
1 parent 9acb4f4 commit e1f4d53
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Raven/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 44 additions & 2 deletions test/Raven/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1f4d53

Please sign in to comment.