Skip to content

Commit 7f9c148

Browse files
committed
tests(state): more tests with HEAD verb
1 parent 60d7cfe commit 7f9c148

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/State/Provider/RangeHeaderProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\State\Provider;
1515

1616
use ApiPlatform\Metadata\CollectionOperationInterface;
17+
use ApiPlatform\Metadata\HttpOperation;
1718
use ApiPlatform\Metadata\Operation;
1819
use ApiPlatform\State\Pagination\Pagination;
1920
use ApiPlatform\State\ProviderInterface;
@@ -42,6 +43,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4243
if (
4344
!$request
4445
|| !$operation instanceof CollectionOperationInterface
46+
|| !$operation instanceof HttpOperation
4547
|| !\in_array($request->getMethod(), ['GET', 'HEAD'], true)
4648
|| !$request->headers->has('Range')
4749
) {

tests/State/ContentRangeHeaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,27 @@ public function testContentRangeWithNoShortNameFallsBackToItems(): void
211211
$this->assertSame('items', $response->headers->get('Accept-Ranges'));
212212
}
213213

214+
public function testHeadRequestReturnsContentRangeHeaders(): void
215+
{
216+
$operation = new GetCollection(shortName: 'Book');
217+
218+
$paginator = $this->prophesize(PaginatorInterface::class);
219+
$paginator->getCurrentPage()->willReturn(1.0);
220+
$paginator->getItemsPerPage()->willReturn(30.0);
221+
$paginator->count()->willReturn(30);
222+
$paginator->getTotalItems()->willReturn(201.0);
223+
224+
$respondProcessor = new RespondProcessor();
225+
$response = $respondProcessor->process('', $operation, context: [
226+
'request' => Request::create('/books', 'HEAD'),
227+
'original_data' => $paginator->reveal(),
228+
]);
229+
230+
$this->assertSame('book 0-29/201', $response->headers->get('Content-Range'));
231+
$this->assertSame('book', $response->headers->get('Accept-Ranges'));
232+
$this->assertEmpty($response->getContent());
233+
}
234+
214235
public function testStatus206WhenOperationStatusIsPartialContent(): void
215236
{
216237
$operation = new GetCollection(shortName: 'Book', status: 206);

tests/State/RangeHeaderProviderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ public function testIgnoresWrongUnit(): void
9191
$provider->provide(new GetCollection(shortName: 'Book'), [], ['request' => $request]);
9292
}
9393

94+
public function testHeadRequestWithRangeHeaderSetsFilters(): void
95+
{
96+
$decorated = $this->createStub(ProviderInterface::class);
97+
$decorated->method('provide')->willReturn([]);
98+
99+
$request = Request::create('/books', 'HEAD');
100+
$request->headers->set('Range', 'book=0-29');
101+
102+
$provider = new RangeHeaderProvider($decorated, new Pagination());
103+
$provider->provide(new GetCollection(shortName: 'Book'), [], ['request' => $request]);
104+
105+
$filters = $request->attributes->get('_api_filters');
106+
$this->assertSame(1, $filters['page']);
107+
$this->assertSame(30, $filters['itemsPerPage']);
108+
109+
$operation = $request->attributes->get('_api_operation');
110+
$this->assertSame(206, $operation->getStatus());
111+
}
112+
94113
public function testValidRangeSetsFiltersAndStatus206(): void
95114
{
96115
$decorated = $this->createStub(ProviderInterface::class);

0 commit comments

Comments
 (0)