Skip to content

Commit

Permalink
Add event to allow inspecting and changing multipart responses
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Nov 27, 2018
1 parent 09ed9f1 commit e5366d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/DAV/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,8 @@ public function getResourceTypeForNode(INode $node)
*/
public function generateMultiStatus($fileProperties, $strip404s = false)
{
$this->emit('beforeMultiStatus', [&$fileProperties]);

$w = $this->xml->getWriter();
$w->openMemory();
$w->contextUri = $this->baseUri;
Expand Down
38 changes: 38 additions & 0 deletions tests/Sabre/DAV/ServerEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ServerEventsTest extends AbstractServer

private $exception;

private $fileProperties;

public function testAfterBind()
{
$this->server->on('afterBind', [$this, 'afterBindHandler']);
Expand Down Expand Up @@ -113,4 +115,40 @@ public function testMethod()
// Fun fact, PHP 7.1 changes the order when sorting-by-callback.
$this->assertTrue($k >= 2 && $k <= 3);
}

public function multiStatusHandler(&$fileProperties)
{
$this->fileProperties = $fileProperties;
$fileProperties = array_slice($fileProperties, 0, 1);
}

public function testBeforeMultiStatus()
{

$this->server->on('beforeMultiStatus', [$this, 'multiStatusHandler']);

$response = $this->server->generateMultiStatus([
[
'href' => 'foo',
'200' => [
'd:getcontentlength' => 10,
],
],
[
'href' => 'bar',
'200' => [
'd:getcontentlength' => 11,
],
],
]);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $response);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');

$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength');
$this->assertCount(1, $data);

$this->assertCount(2, $this->fileProperties);
}
}

0 comments on commit e5366d0

Please sign in to comment.