Browse Source

Add ETag to NavigationController

Signed-off-by: Julius Härtl <jus@bitgrid.net>
tags/v14.0.0beta1
Julius Härtl 6 years ago
parent
commit
723b8764d1
No account linked to committer's email address

+ 16
- 2
core/Controller/NavigationController.php View File

@@ -22,6 +22,7 @@
*/
namespace OC\Core\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\INavigationManager;
@@ -54,7 +55,14 @@ class NavigationController extends OCSController {
if ($absolute) {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}
return new DataResponse($navigation);

$etag = md5(json_encode($navigation));
if ($this->request->getHeader('If-None-Match') === $etag) {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
$response = new DataResponse($navigation);
$response->setEtag($etag);
return $response;
}

/**
@@ -69,7 +77,13 @@ class NavigationController extends OCSController {
if ($absolute) {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}
return new DataResponse($navigation);
$etag = md5(json_encode($navigation));
if ($this->request->getHeader('If-None-Match') === $etag) {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
$response = new DataResponse($navigation);
$response->setEtag($etag);
return $response;
}

/**

+ 31
- 0
tests/Core/Controller/NavigationControllerTest.php View File

@@ -23,6 +23,7 @@
namespace Tests\Core\Controller;

use OC\Core\Controller\NavigationController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\INavigationManager;
use OCP\IRequest;
@@ -126,4 +127,34 @@ class NavigationControllerTest extends TestCase {
}
}

public function testGetAppNavigationEtagMatch() {
$navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
$this->request->expects($this->once())
->method('getHeader')
->with('If-None-Match')
->willReturn(md5(json_encode($navigation)));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('link')
->willReturn($navigation);
$actual = $this->controller->getAppsNavigation();
$this->assertInstanceOf(DataResponse::class, $actual);
$this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
}

public function testGetSettingsNavigationEtagMatch() {
$navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
$this->request->expects($this->once())
->method('getHeader')
->with('If-None-Match')
->willReturn(md5(json_encode($navigation)));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('settings')
->willReturn($navigation);
$actual = $this->controller->getSettingsNavigation();
$this->assertInstanceOf(DataResponse::class, $actual);
$this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
}

}

Loading…
Cancel
Save