Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 16b90e3

Browse files
committed
Erroneous responses shouldn't have a body
Add the stream with the response only on valid requests
1 parent 8b15569 commit 16b90e3

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/Endpoint.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function serve(
5050
ResponseInterface $response
5151
): ResponseInterface {
5252
$statusCode = StatusCode::OK;
53+
$responseData = null;
5354

5455
try {
5556
// Process and build the response
@@ -73,25 +74,26 @@ public function serve(
7374

7475
$this->log($e, $uuid, $e->getContext());
7576

76-
$responseData = '';
7777
$statusCode = StatusCode::INTERNAL_SERVER_ERROR;
7878
} catch (Throwable $e) {
7979
$uuid = $this->uuidFactory->uuid4();
8080

8181
$this->log($e, $uuid);
8282

83-
$responseData = '';
8483
$statusCode = StatusCode::INTERNAL_SERVER_ERROR;
8584
}
8685

87-
return $response
88-
->withHeader('Content-Type', 'application/json')
89-
->withStatus($statusCode)
90-
->withBody(
86+
if ($responseData !== null) {
87+
$response = $response->withBody(
9188
$this->streamFactory->createStream(
9289
(string) json_encode($responseData)
9390
)
9491
);
92+
}
93+
94+
return $response
95+
->withHeader('Content-Type', 'application/json')
96+
->withStatus($statusCode);
9597
}
9698

9799
/**

tests/EndpointTest.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function testServeCatchesGenericException(): void
240240

241241
$this->createResponseExpectations(
242242
$response,
243-
'',
243+
null,
244244
StatusCode::INTERNAL_SERVER_ERROR
245245
);
246246

@@ -287,7 +287,7 @@ public function testServeCatchesInternalException(): void
287287

288288
$this->createResponseExpectations(
289289
$response,
290-
'',
290+
null,
291291
StatusCode::INTERNAL_SERVER_ERROR
292292
);
293293

@@ -361,15 +361,22 @@ public function testProcessWorksWithMiddleware(): void
361361

362362
private function createResponseExpectations(
363363
MockInterface $response,
364-
string|array $responseData,
364+
string|array|null $responseData,
365365
int $statusCode
366366
): void {
367-
$stream = Mockery::mock(StreamInterface::class);
367+
if ($responseData !== null) {
368+
$stream = Mockery::mock(StreamInterface::class);
368369

369-
$this->streamFactory->shouldReceive('createStream')
370-
->with(json_encode($responseData))
371-
->once()
372-
->andReturn($stream);
370+
$this->streamFactory->shouldReceive('createStream')
371+
->with(json_encode($responseData))
372+
->once()
373+
->andReturn($stream);
374+
375+
$response->shouldReceive('withBody')
376+
->with($stream)
377+
->once()
378+
->andReturnSelf();
379+
}
373380

374381
$response->shouldReceive('withHeader')
375382
->with('Content-Type', 'application/json')
@@ -379,9 +386,5 @@ private function createResponseExpectations(
379386
->with($statusCode)
380387
->once()
381388
->andReturnSelf();
382-
$response->shouldReceive('withBody')
383-
->with($stream)
384-
->once()
385-
->andReturnSelf();
386389
}
387390
}

0 commit comments

Comments
 (0)