Skip to content

Commit

Permalink
refs matomo-org/matomo-log-analytics#57 return a http 400 if no reque…
Browse files Browse the repository at this point in the history
…st is set
  • Loading branch information
tsteur committed Apr 13, 2015
1 parent bd9efbb commit 2945b1c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/Tracker/Response.php
Expand Up @@ -74,6 +74,7 @@ public function outputResponse(Tracker $tracker)
$this->outputApiResponse($tracker);
Common::printDebug("Logging disabled, display transparent logo");
} elseif (!$tracker->hasLoggedRequests()) {
Common::sendResponseCode(400);
Common::printDebug("Empty request => Piwik page");
echo "<a href='/'>Piwik</a> is a free/libre web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data.";
} else {
Expand Down
58 changes: 58 additions & 0 deletions tests/PHPUnit/Framework/Constraint/HttpResponseText.php
@@ -0,0 +1,58 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Framework\Constraint;

class HttpResponseText extends \PHPUnit_Framework_Constraint
{
private $actualCode;

/**
* @param string $value Expected response text.
*/
public function __construct($value)
{
parent::__construct();
$this->value = $value;
}

/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
* @param mixed $other Value or object to evaluate.
* @return bool
*/
public function matches($other)
{
$options = array(
CURLOPT_URL => $other,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 1,
CURLOPT_RETURNTRANSFER => true
);

$ch = curl_init();
curl_setopt_array($ch, $options);
$response = @curl_exec($ch);
curl_close($ch);

$this->actualCode = $response;

return $this->value === $this->actualCode;
}

/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
return 'does not return response text ' . $this->exporter->export($this->value) . ' it is ' . $this->actualCode;
}
}?>
6 changes: 6 additions & 0 deletions tests/PHPUnit/Framework/TestCase/SystemTestCase.php
Expand Up @@ -17,6 +17,7 @@
use Piwik\DbHelper;
use Piwik\ReportRenderer;
use Piwik\Tests\Framework\Constraint\ResponseCode;
use Piwik\Tests\Framework\Constraint\HttpResponseText;
use Piwik\Tests\Framework\TestRequest\ApiTestConfig;
use Piwik\Tests\Framework\TestRequest\Collection;
use Piwik\Tests\Framework\TestRequest\Response;
Expand Down Expand Up @@ -596,6 +597,11 @@ protected function skipWhenPhp53()
}
}

public function assertHttpResponseText($expectedResponseCode, $url, $message = '')
{
self::assertThat($url, new HttpResponseText($expectedResponseCode), $message);
}

public function assertResponseCode($expectedResponseCode, $url, $message = '')
{
self::assertThat($url, new ResponseCode($expectedResponseCode), $message);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPUnit/System/TrackerResponseTest.php
Expand Up @@ -97,10 +97,10 @@ public function test_response_ShouldSend400ResponseCode_IfInvalidRequestParamete
public function test_response_ShouldReturnPiwikMessage_InCaseOfEmptyRequest()
{
$url = Fixture::getTrackerUrl();
$response = file_get_contents($url);
$this->assertResponseCode(400, $url);

$expected = "<a href='/'>Piwik</a> is a free/libre web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data.";
$this->assertEquals($expected, $response);
$this->assertHttpResponseText($expected, $url);
}

}

0 comments on commit 2945b1c

Please sign in to comment.