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 Apr 29, 2024
1 parent da8c1f2 commit a9fe8fc
Show file tree
Hide file tree
Showing 2 changed files with 39 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 @@ -1635,6 +1635,8 @@ public function getResourceTypeForNode(INode $node)
*/
public function generateMultiStatus($fileProperties, $strip404s = false)
{
$this->emit('beforeMultiStatus', [&$fileProperties]);

$w = $this->xml->getWriter();
if (self::$streamMultiStatus) {
return function () use ($fileProperties, $strip404s, $w) {
Expand Down
37 changes: 37 additions & 0 deletions tests/Sabre/DAV/ServerEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ServerEventsTest extends AbstractServerTestCase

private $exception;

private $fileProperties;

public function testAfterBindOfFile()
{
$this->server->on('afterBind', [$this, 'afterHandler']);
Expand Down Expand Up @@ -191,4 +193,39 @@ public function testMethod()
// Fun fact, PHP 7.1 changes the order when sorting-by-callback.
self::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 a9fe8fc

Please sign in to comment.