Skip to content

Commit cd9d5b0

Browse files
committed
Fix etags
1 parent b900322 commit cd9d5b0

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ This assumes that you have an existing project created with `silverstripe/cms` i
5252
```php
5353
<?php
5454

55-
use emteknetnz\RestApi\RestApiEndpoint;
55+
use emteknetnz\RestApi\Controllers\RestApiEndpoint;
5656
use SilverStripe\CMS\Model\SiteTree;
5757

5858
class MySiteTreeEndpoint extends RestApiEndpoint
5959
{
6060
private static array $api_config = [
61-
RestApiEndpoint::PATH = 'api/pages';
61+
RestApiEndpoint::PATH => 'api/pages',
6262
RestApiEndpoint::DATA_CLASS => SiteTree::class,
6363
RestApiEndpoint::ACCESS => RestApiEndpoint::PUBLIC,
6464
RestApiEndpoint::FIELDS => [
@@ -69,6 +69,7 @@ class MySiteTreeEndpoint extends RestApiEndpoint
6969
],
7070
];
7171
}
72+
7273
```
7374

7475
Run `https://mysite.test/dev/build?flush=1`

src/Controllers/RestApiEndpoint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ private function apiOptions(): HTTPResponse
260260
$response = $this->getResponse();
261261
$response->setStatusCode(204);
262262
$response->addHeader('Allow', $this->allowedOptions());
263+
$response->setBody('');
263264
return $response;
264265
}
265266

@@ -953,7 +954,7 @@ private function jsonResponse(array $data, int $code = 200, array $headers = [])
953954
// https://www.rfc-editor.org/rfc/rfc7232#section-2.3
954955
$etag = $response->getHeader('ETag');
955956
if (!$etag) {
956-
$etag = sprintf('"%s"', md5($response->getBody() ?? ''));
957+
$etag = sprintf('"%s"', md5($body));
957958
$response->addHeader('ETag', $etag);
958959
}
959960
}

tests/Controllers/RestApiEndpointTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,14 +2228,17 @@ public function testETag()
22282228
{
22292229
$this->setConfig(self::CALL_CAN_METHODS, self::NONE);
22302230
// double quotes inside single quotes are intentional
2231-
$expected = '"d41d8cd98f00b204e9800998ecf8427e"';
2231+
$expectedViewOperation = '"6cd52dbfaf406fcece186c5a9004e677"';
2232+
$expectedBlankBody = '"d41d8cd98f00b204e9800998ecf8427e"';
22322233
$taskID = TestTask::get()->first()->ID;
2233-
$this->assertSame($expected, $this->req('GET', $taskID)->getHeader('ETag'));
2234-
$this->assertSame($expected, $this->req('HEAD', $taskID)->getHeader('ETag'));
2234+
$this->assertSame($expectedViewOperation, $this->req('GET', $taskID)->getHeader('ETag'));
2235+
$this->assertSame($expectedViewOperation, $this->req('HEAD', $taskID)->getHeader('ETag'));
22352236
$this->assertSame(null, $this->req('POST')->getHeader('ETag'));
22362237
$this->assertSame(null, $this->req('PATCH', $taskID, null, null, ['title' => 'changed'])->getHeader('ETag'));
22372238
$this->assertSame(null, $this->req('DELETE', $taskID)->getHeader('ETag'));
2238-
$this->assertSame(null, $this->req('OPTIONS')->getHeader('ETag'));
2239+
$this->assertSame(null, $this->req('PUT')->getHeader('ETag'));
2240+
// OPTIONS gets an etag added by ChangeDetectionMiddleware
2241+
$this->assertSame($expectedBlankBody, $this->req('OPTIONS')->getHeader('ETag'));
22392242
}
22402243

22412244
public function testExtensionHooks(): void

0 commit comments

Comments
 (0)