Add an Etag header for the request

This commit is contained in:
Joas Schilling 2015-09-14 15:15:11 +02:00
Родитель ed1baac6b6
Коммит e750b63552
2 изменённых файлов: 19 добавлений и 3 удалений

Просмотреть файл

@ -77,7 +77,9 @@ class EndpointController extends Controller {
$data[] = $this->notificationToArray($notificationId, $notification);
}
return new JSONResponse($data);
$response = new JSONResponse($data);
$response->setETag($this->generateEtag($notifications));
return $response;
}
/**
@ -91,6 +93,16 @@ class EndpointController extends Controller {
return new Response();
}
/**
* Get an Etag for the notification ids
* @param array $notifications
* @return string
*/
protected function generateEtag(array $notifications) {
$ids = array_keys($notifications);
return md5(json_encode($ids));
}
/**
* @param int $notificationId
* @param INotification $notification

Просмотреть файл

@ -93,7 +93,7 @@ class EndpointControllerTest extends TestCase {
public function dataGet() {
return [
[
[], [],
[], md5(json_encode([])), [],
],
[
[
@ -104,6 +104,7 @@ class EndpointControllerTest extends TestCase {
->disableOriginalConstructor()
->getMock(),
],
md5(json_encode([1, 3])),
['$notification', '$notification'],
],
[
@ -112,6 +113,7 @@ class EndpointControllerTest extends TestCase {
->disableOriginalConstructor()
->getMock(),
],
md5(json_encode([42])),
['$notification'],
],
];
@ -120,9 +122,10 @@ class EndpointControllerTest extends TestCase {
/**
* @dataProvider dataGet
* @param array $notifications
* @param string $expectedETag
* @param array $expectedData
*/
public function testGet(array $notifications, array $expectedData) {
public function testGet(array $notifications, $expectedETag, array $expectedData) {
$controller = $this->getController([
'notificationToArray',
]);
@ -152,6 +155,7 @@ class EndpointControllerTest extends TestCase {
$response = $controller->get();
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
$this->assertSame($expectedETag, $response->getETag());
$this->assertSame($expectedData, $response->getData());
}