diff --git a/core/Tracker/Response.php b/core/Tracker/Response.php index 5258d65cd2b..3edd2b27c69 100644 --- a/core/Tracker/Response.php +++ b/core/Tracker/Response.php @@ -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 "Piwik is a free/libre web analytics that lets you keep control of your data."; } else { diff --git a/tests/PHPUnit/Framework/Constraint/HttpResponseText.php b/tests/PHPUnit/Framework/Constraint/HttpResponseText.php new file mode 100644 index 00000000000..54d70982624 --- /dev/null +++ b/tests/PHPUnit/Framework/Constraint/HttpResponseText.php @@ -0,0 +1,58 @@ +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; + } +}?> \ No newline at end of file diff --git a/tests/PHPUnit/Framework/TestCase/SystemTestCase.php b/tests/PHPUnit/Framework/TestCase/SystemTestCase.php index f0a1c0f9822..f8944bf880d 100755 --- a/tests/PHPUnit/Framework/TestCase/SystemTestCase.php +++ b/tests/PHPUnit/Framework/TestCase/SystemTestCase.php @@ -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; @@ -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); diff --git a/tests/PHPUnit/System/TrackerResponseTest.php b/tests/PHPUnit/System/TrackerResponseTest.php index 7bd0f7aef78..16ab4a4e010 100755 --- a/tests/PHPUnit/System/TrackerResponseTest.php +++ b/tests/PHPUnit/System/TrackerResponseTest.php @@ -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 = "Piwik is a free/libre web analytics that lets you keep control of your data."; - $this->assertEquals($expected, $response); + $this->assertHttpResponseText($expected, $url); } }