[Pkg-owncloud-commits] [php-sabredav] 59/80: Now throwing an exception when no system supplied a correct http status.
David Prévot
taffit at moszumanska.debian.org
Thu Jan 7 02:56:35 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit 3deeb3f1c532ed72d359fd17680a78e7141d1ae1
Author: Evert Pot <me at evertpot.com>
Date: Mon Jan 4 23:16:04 2016 -0500
Now throwing an exception when no system supplied a correct http status.
Fixes #684
---
lib/DAV/Server.php | 3 ++
tests/Sabre/DAV/ServerEventsTest.php | 45 ++++++++++----------
tests/Sabre/DAV/ServerPropsTest.php | 81 ++++++++++++++++++------------------
tests/Sabre/DAV/ServerSimpleTest.php | 61 ++++++++++++++++-----------
4 files changed, 105 insertions(+), 85 deletions(-)
diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php
index 5e70622..b376528 100644
--- a/lib/DAV/Server.php
+++ b/lib/DAV/Server.php
@@ -471,6 +471,9 @@ class Server extends EventEmitter {
if (!$this->emit('afterMethod:' . $method, [$request, $response])) return;
if (!$this->emit('afterMethod', [$request, $response])) return;
+ if ($response->getStatus() === null) {
+ throw new Exception('No subsystem set a valid HTTP status code. Something must have interrupted the request without providing further detail.');
+ }
if ($sendResponse) {
$this->sapi->sendResponse($response);
$this->emit('afterResponse', [$request, $response]);
diff --git a/tests/Sabre/DAV/ServerEventsTest.php b/tests/Sabre/DAV/ServerEventsTest.php
index 354e689..6ac20d2 100644
--- a/tests/Sabre/DAV/ServerEventsTest.php
+++ b/tests/Sabre/DAV/ServerEventsTest.php
@@ -1,6 +1,7 @@
<?php
namespace Sabre\DAV;
+
use Sabre\HTTP;
require_once 'Sabre/DAV/AbstractServer.php';
@@ -13,11 +14,11 @@ class ServerEventsTest extends AbstractServer {
function testAfterBind() {
- $this->server->on('afterBind', [$this,'afterBindHandler']);
+ $this->server->on('afterBind', [$this, 'afterBindHandler']);
$newPath = 'afterBind';
$this->tempPath = '';
- $this->server->createFile($newPath,'body');
+ $this->server->createFile($newPath, 'body');
$this->assertEquals($newPath, $this->tempPath);
}
@@ -30,15 +31,15 @@ class ServerEventsTest extends AbstractServer {
function testAfterResponse() {
- $mock = $this->getMock('stdClass', array('afterResponseCallback'));
+ $mock = $this->getMock('stdClass', ['afterResponseCallback']);
$mock->expects($this->once())->method('afterResponseCallback');
$this->server->on('afterResponse', [$mock, 'afterResponseCallback']);
- $this->server->httpRequest = HTTP\Sapi::createFromServerArray(array(
+ $this->server->httpRequest = HTTP\Sapi::createFromServerArray([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/test.txt',
- ));
+ ]);
$this->server->exec();
@@ -46,19 +47,19 @@ class ServerEventsTest extends AbstractServer {
function testBeforeBindCancel() {
- $this->server->on('beforeBind', [$this,'beforeBindCancelHandler']);
- $this->assertFalse($this->server->createFile('bla','body'));
+ $this->server->on('beforeBind', [$this, 'beforeBindCancelHandler']);
+ $this->assertFalse($this->server->createFile('bla', 'body'));
// Also testing put()
- $req = HTTP\Sapi::createFromServerArray(array(
+ $req = HTTP\Sapi::createFromServerArray([
'REQUEST_METHOD' => 'PUT',
- 'REQUEST_URI' => '/barbar',
- ));
+ 'REQUEST_URI' => '/barbar',
+ ]);
$this->server->httpRequest = $req;
$this->server->exec();
- $this->assertEquals('',$this->server->httpResponse->status);
+ $this->assertEquals(500, $this->server->httpResponse->getStatus());
}
@@ -72,10 +73,10 @@ class ServerEventsTest extends AbstractServer {
$this->server->on('exception', [$this, 'exceptionHandler']);
- $req = HTTP\Sapi::createFromServerArray(array(
+ $req = HTTP\Sapi::createFromServerArray([
'REQUEST_METHOD' => 'GET',
- 'REQUEST_URI' => '/not/exisitng',
- ));
+ 'REQUEST_URI' => '/not/exisitng',
+ ]);
$this->server->httpRequest = $req;
$this->server->exec();
@@ -94,24 +95,26 @@ class ServerEventsTest extends AbstractServer {
$k = 1;
$this->server->on('method', function($request, $response) use (&$k) {
- $k+=1;
+ $k += 1;
return false;
});
$this->server->on('method', function($request, $response) use (&$k) {
- $k+=2;
+ $k += 2;
return false;
});
- $this->server->invokeMethod(
- new HTTP\Request('BLABLA', '/'),
- new HTTP\Response(),
- false
- );
+ try {
+ $this->server->invokeMethod(
+ new HTTP\Request('BLABLA', '/'),
+ new HTTP\Response(),
+ false
+ );
+ } catch (Exception $e) {}
$this->assertEquals(2, $k);
diff --git a/tests/Sabre/DAV/ServerPropsTest.php b/tests/Sabre/DAV/ServerPropsTest.php
index 6d7a8b9..253200b 100644
--- a/tests/Sabre/DAV/ServerPropsTest.php
+++ b/tests/Sabre/DAV/ServerPropsTest.php
@@ -1,6 +1,7 @@
<?php
namespace Sabre\DAV;
+
use Sabre\HTTP;
require_once 'Sabre/HTTP/ResponseMock.php';
@@ -16,7 +17,7 @@ class ServerPropsTest extends AbstractServer {
function setUp() {
- if (file_exists(SABRE_TEMPDIR.'../.sabredav')) unlink(SABRE_TEMPDIR.'../.sabredav');
+ if (file_exists(SABRE_TEMPDIR . '../.sabredav')) unlink(SABRE_TEMPDIR . '../.sabredav');
parent::setUp();
file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
mkdir(SABRE_TEMPDIR . '/col');
@@ -28,7 +29,7 @@ class ServerPropsTest extends AbstractServer {
function tearDown() {
parent::tearDown();
- if (file_exists(SABRE_TEMPDIR.'../.locksdb')) unlink(SABRE_TEMPDIR.'../.locksdb');
+ if (file_exists(SABRE_TEMPDIR . '../.locksdb')) unlink(SABRE_TEMPDIR . '../.locksdb');
}
@@ -46,24 +47,24 @@ class ServerPropsTest extends AbstractServer {
$this->sendRequest("");
$this->assertEquals(207, $this->response->status);
- $this->assertEquals(array(
+ $this->assertEquals([
'X-Sabre-Version' => [Version::VERSION],
- 'Content-Type' => ['application/xml; charset=utf-8'],
- 'DAV' => ['1, 3, extended-mkcol, 2'],
- 'Vary' => ['Brief,Prefer'],
- ),
+ 'Content-Type' => ['application/xml; charset=utf-8'],
+ 'DAV' => ['1, 3, extended-mkcol, 2'],
+ 'Vary' => ['Brief,Prefer'],
+ ],
$this->response->getHeaders()
);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
+ $xml->registerXPathNamespace('d', 'urn:DAV');
list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
- $this->assertEquals('/',(string)$data,'href element should have been /');
+ $this->assertEquals('/', (string)$data, 'href element should have been /');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
- $this->assertEquals(1,count($data));
+ $this->assertEquals(1, count($data));
}
@@ -72,24 +73,24 @@ class ServerPropsTest extends AbstractServer {
$this->sendRequest("", '/test2.txt', []);
$this->assertEquals(207, $this->response->status);
- $this->assertEquals(array(
+ $this->assertEquals([
'X-Sabre-Version' => [Version::VERSION],
- 'Content-Type' => ['application/xml; charset=utf-8'],
- 'DAV' => ['1, 3, extended-mkcol, 2'],
- 'Vary' => ['Brief,Prefer'],
- ),
+ 'Content-Type' => ['application/xml; charset=utf-8'],
+ 'DAV' => ['1, 3, extended-mkcol, 2'],
+ 'Vary' => ['Brief,Prefer'],
+ ],
$this->response->getHeaders()
);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
+ $xml->registerXPathNamespace('d', 'urn:DAV');
list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
- $this->assertEquals('/test2.txt',(string)$data,'href element should have been /test2.txt');
+ $this->assertEquals('/test2.txt', (string)$data, 'href element should have been /test2.txt');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength');
- $this->assertEquals(1,count($data));
+ $this->assertEquals(1, count($data));
}
@@ -104,27 +105,27 @@ class ServerPropsTest extends AbstractServer {
$this->sendRequest($xml);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
+ $xml->registerXPathNamespace('d', 'urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
- $this->assertEquals(2,count($data),'We expected two \'d:lockentry\' tags');
+ $this->assertEquals(2, count($data), 'We expected two \'d:lockentry\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
- $this->assertEquals(2,count($data),'We expected two \'d:lockscope\' tags');
+ $this->assertEquals(2, count($data), 'We expected two \'d:lockscope\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
- $this->assertEquals(2,count($data),'We expected two \'d:locktype\' tags');
+ $this->assertEquals(2, count($data), 'We expected two \'d:locktype\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
- $this->assertEquals(1,count($data),'We expected a \'d:shared\' tag');
+ $this->assertEquals(1, count($data), 'We expected a \'d:shared\' tag');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
- $this->assertEquals(1,count($data),'We expected a \'d:exclusive\' tag');
+ $this->assertEquals(1, count($data), 'We expected a \'d:exclusive\' tag');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
- $this->assertEquals(2,count($data),'We expected two \'d:write\' tags');
+ $this->assertEquals(2, count($data), 'We expected two \'d:write\' tags');
}
function testLockDiscovery() {
@@ -138,12 +139,12 @@ class ServerPropsTest extends AbstractServer {
$this->sendRequest($xml);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
+ $xml->registerXPathNamespace('d', 'urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
- $this->assertEquals(1,count($data),'We expected a \'d:lockdiscovery\' tag');
+ $this->assertEquals(1, count($data), 'We expected a \'d:lockdiscovery\' tag');
}
@@ -157,24 +158,24 @@ class ServerPropsTest extends AbstractServer {
</d:propfind>';
$this->sendRequest($xml);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
- $pathTests = array(
+ $xml->registerXPathNamespace('d', 'urn:DAV');
+ $pathTests = [
'/d:multistatus',
'/d:multistatus/d:response',
'/d:multistatus/d:response/d:propstat',
'/d:multistatus/d:response/d:propstat/d:status',
'/d:multistatus/d:response/d:propstat/d:prop',
'/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
- );
- foreach($pathTests as $test) {
- $this->assertTrue(count($xml->xpath($test))==true,'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
+ ];
+ foreach ($pathTests as $test) {
+ $this->assertTrue(count($xml->xpath($test)) == true, 'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
}
$val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
- $this->assertEquals(1,count($val),$body);
- $this->assertEquals('HTTP/1.1 404 Not Found',(string)$val[0]);
+ $this->assertEquals(1, count($val), $body);
+ $this->assertEquals('HTTP/1.1 404 Not Found', (string)$val[0]);
}
@@ -190,7 +191,7 @@ class ServerPropsTest extends AbstractServer {
$result = $this->server->xml->parse($body);
$this->assertEquals([
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
+ '{http://sabredav.org/NS/test}someprop' => 'somevalue',
'{http://sabredav.org/NS/test}someprop2' => null,
'{http://sabredav.org/NS/test}someprop3' => null,
], $result->properties);
diff --git a/tests/Sabre/DAV/ServerSimpleTest.php b/tests/Sabre/DAV/ServerSimpleTest.php
index baf0199..66dde9d 100644
--- a/tests/Sabre/DAV/ServerSimpleTest.php
+++ b/tests/Sabre/DAV/ServerSimpleTest.php
@@ -53,7 +53,7 @@ class ServerSimpleTest extends AbstractServer{
'Accept-Ranges' => ['bytes'],
'Content-Length' => ['0'],
'X-Sabre-Version' => [Version::VERSION],
- ],$this->response->getHeaders());
+ ], $this->response->getHeaders());
$this->assertEquals(200, $this->response->status);
$this->assertEquals('', $this->response->body);
@@ -74,7 +74,7 @@ class ServerSimpleTest extends AbstractServer{
'Accept-Ranges' => ['bytes'],
'Content-Length' => ['0'],
'X-Sabre-Version' => [Version::VERSION],
- ],$this->response->getHeaders());
+ ], $this->response->getHeaders());
$this->assertEquals(200, $this->response->status);
$this->assertEquals('', $this->response->body);
@@ -95,7 +95,7 @@ class ServerSimpleTest extends AbstractServer{
$this->assertEquals([
'X-Sabre-Version' => [Version::VERSION],
'Content-Type' => ['application/xml; charset=utf-8'],
- ],$this->response->getHeaders());
+ ], $this->response->getHeaders());
$this->assertEquals(501, $this->response->status);
@@ -112,7 +112,7 @@ class ServerSimpleTest extends AbstractServer{
$request = HTTP\Sapi::createFromServerArray($serverVars);
$this->server->setBaseUri('/blabla/');
- $this->assertEquals('/blabla/',$this->server->getBaseUri());
+ $this->assertEquals('/blabla/', $this->server->getBaseUri());
$this->server->httpRequest = ($request);
$this->server->exec();
@@ -141,7 +141,7 @@ class ServerSimpleTest extends AbstractServer{
'/foo/bar/' => '/foo/bar/',
];
- foreach($tests as $test=>$result) {
+ foreach ($tests as $test => $result) {
$this->server->setBaseUri($test);
$this->assertEquals($result, $this->server->getBaseUri());
@@ -160,17 +160,17 @@ class ServerSimpleTest extends AbstractServer{
$this->server->setBaseUri('/root/');
- foreach($uris as $uri) {
+ foreach ($uris as $uri) {
- $this->assertEquals('somepath',$this->server->calculateUri($uri));
+ $this->assertEquals('somepath', $this->server->calculateUri($uri));
}
$this->server->setBaseUri('/root');
- foreach($uris as $uri) {
+ foreach ($uris as $uri) {
- $this->assertEquals('somepath',$this->server->calculateUri($uri));
+ $this->assertEquals('somepath', $this->server->calculateUri($uri));
}
@@ -188,25 +188,25 @@ class ServerSimpleTest extends AbstractServer{
$this->server->setBaseUri('/root/');
- foreach($uris as $uri) {
+ foreach ($uris as $uri) {
- $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
+ $this->assertEquals("\xc3\xa0fo\xc3\xb3", $this->server->calculateUri($uri));
}
$this->server->setBaseUri('/root');
- foreach($uris as $uri) {
+ foreach ($uris as $uri) {
- $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
+ $this->assertEquals("\xc3\xa0fo\xc3\xb3", $this->server->calculateUri($uri));
}
$this->server->setBaseUri('/');
- foreach($uris as $uri) {
+ foreach ($uris as $uri) {
- $this->assertEquals("root/\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
+ $this->assertEquals("root/\xc3\xa0fo\xc3\xb3", $this->server->calculateUri($uri));
}
@@ -363,18 +363,18 @@ class ServerSimpleTest extends AbstractServer{
function testTriggerException() {
$serverVars = [
- 'REQUEST_URI' => '/',
+ 'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'FOO',
];
$httpRequest = HTTP\Sapi::createFromServerArray($serverVars);
$this->server->httpRequest = $httpRequest;
- $this->server->on('beforeMethod', [$this,'exceptionTrigger']);
+ $this->server->on('beforeMethod', [$this, 'exceptionTrigger']);
$this->server->exec();
$this->assertEquals([
'Content-Type' => ['application/xml; charset=utf-8'],
- ],$this->response->getHeaders());
+ ], $this->response->getHeaders());
$this->assertEquals(500, $this->response->status);
@@ -419,7 +419,7 @@ class ServerSimpleTest extends AbstractServer{
$request = HTTP\Sapi::createFromServerArray($serverVars);
$this->server->httpRequest = ($request);
$this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>');
- $this->server->on('report', [$this,'reportHandler']);
+ $this->server->on('report', [$this, 'reportHandler']);
$this->server->exec();
$this->assertEquals([
@@ -429,15 +429,15 @@ class ServerSimpleTest extends AbstractServer{
$this->response->getHeaders()
);
- $this->assertEquals(418, $this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body);
+ $this->assertEquals(418, $this->response->status, 'We got an incorrect status back. Full response body follows: ' . $this->response->body);
}
function reportHandler($reportName, $result, $path) {
- if ($reportName=='{http://www.rooftopsolutions.nl/NS}myreport') {
+ if ($reportName == '{http://www.rooftopsolutions.nl/NS}myreport') {
$this->server->httpResponse->setStatus(418);
- $this->server->httpResponse->setHeader('testheader','testvalue');
+ $this->server->httpResponse->setHeader('testheader', 'testvalue');
return false;
}
else return;
@@ -446,7 +446,7 @@ class ServerSimpleTest extends AbstractServer{
function testGetPropertiesForChildren() {
- $result = $this->server->getPropertiesForChildren('',[
+ $result = $this->server->getPropertiesForChildren('', [
'{DAV:}getcontentlength',
]);
@@ -455,7 +455,20 @@ class ServerSimpleTest extends AbstractServer{
'dir/' => [],
];
- $this->assertEquals($expected,$result);
+ $this->assertEquals($expected, $result);
+
+ }
+
+ /**
+ * There are certain cases where no HTTP status may be set. We need to
+ * intercept these and set it to a default error message.
+ */
+ function testNoHTTPSTatusSet() {
+
+ $this->server->on('method:GET', function() { return false; }, 1);
+ $this->server->httpRequest = new HTTP\Request('GET', '/');
+ $this->server->exec();
+ $this->assertEquals(500, $this->response->getStatus());
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list