Skip to content

Commit

Permalink
Fix overwritten headers in Swoole response (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
piku235 authored Oct 8, 2021
1 parent 46d813f commit 0dab557
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"require": {
"symfony/runtime": "^5.3 || ^6.0"
},
"conflict": {
"ext-swoole": "<4.6.0"
},
"require-dev": {
"illuminate/http": "^8.48",
"swoole/ide-helper": "^4.6",
Expand Down
4 changes: 1 addition & 3 deletions src/SymfonyHttpBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public static function convertSwooleRequest(Request $request): SymfonyRequest
public static function reflectSymfonyResponse(SymfonyResponse $sfResponse, Response $response): void
{
foreach ($sfResponse->headers->all() as $name => $values) {
foreach ($values as $value) {
$response->header($name, $value);
}
$response->header($name, $values);
}

$response->status($sfResponse->getStatusCode());
Expand Down
14 changes: 12 additions & 2 deletions tests/Unit/SymfonyHttpBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swoole\Http\Request;
use Swoole\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
Expand Down Expand Up @@ -59,13 +60,22 @@ public function testThatSwooleRequestIsConverted(): void

public function testThatSymfonyResponseIsReflected(): void
{
$fooCookie = (string) new Cookie('foo', '123');
$barCookie = (string) new Cookie('bar', '234');

$sfResponse = $this->createMock(SymfonyResponse::class);
$sfResponse->headers = new HeaderBag(['X-Test' => 'Swoole-Runtime']);
$sfResponse->headers = new HeaderBag([
'X-Test' => 'Swoole-Runtime',
'Set-Cookie' => [$fooCookie, $barCookie],
]);
$sfResponse->expects(self::once())->method('getStatusCode')->willReturn(201);
$sfResponse->expects(self::once())->method('getContent')->willReturn('Test');

$response = $this->createMock(Response::class);
$response->expects(self::once())->method('header')->with('x-test', 'Swoole-Runtime');
$response->expects(self::exactly(2))->method('header')->withConsecutive(
['x-test', ['Swoole-Runtime']],
['set-cookie', [$fooCookie, $barCookie]]
);
$response->expects(self::once())->method('status')->with(201);
$response->expects(self::once())->method('end')->with('Test');

Expand Down

0 comments on commit 0dab557

Please # to comment.