Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Add support for cookies (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
imefisto authored and pachico committed Sep 21, 2018
1 parent 3147815 commit 7436675
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
],
"require": {
"php": "^7.0"
"php": "^7.0",
"dflydev/fig-cookies": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
Expand Down
24 changes: 24 additions & 0 deletions src/Bridge/RequestTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Slim\Http;
use swoole_http_request;
use Dflydev\FigCookies\Cookie;
use Dflydev\FigCookies\FigRequestCookies;

class RequestTransformer implements RequestTransformerInterface
{
Expand Down Expand Up @@ -43,9 +45,31 @@ public function toSlim(swoole_http_request $request): Http\Request
$slimRequest = $this->handleUploadedFiles($request, $slimRequest);
}

$slimRequest = $this->copyCookies($request, $slimRequest);

return $this->copyBody($request, $slimRequest);
}

/**
* @param swoole_http_request $request
* @param Http\Request $slimRequest
*
* @return Http\Request
*/
private function copyCookies(swoole_http_request $request, Http\Request $slimRequest): Http\Request
{
if (empty($request->cookie)) {
return $slimRequest;
}

foreach ($request->cookie as $name => $value) {
$cookie = Cookie::create($name, $value);
$slimRequest = FigRequestCookies::set($slimRequest, $cookie);
}

return $slimRequest;
}

/**
* @param swoole_http_request $request
* @param Http\Request $slimRequest
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Bridge/RequestTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Pachico\SlimSwoole\Bridge;
use Slim\Http;
use Dflydev\FigCookies\Cookies;
use Dflydev\FigCookies\FigRequestCookies;

class RequestTransformerTest extends \Pachico\SlimSwooleUnitTest\AbstractTestCase
{
Expand Down Expand Up @@ -132,4 +134,21 @@ public function testUploadedFilesAreCopiedProperty()
$this->assertSame($output->getUploadedFiles()['name2']->getError(), 0);
$this->assertSame($output->getUploadedFiles()['name2']->getSize(), 88);
}

public function testCookiesAreCopiedProperly()
{
$this->swooleRequest->cookie = [
'some-cookie-1' => 'some-value-1',
'some-cookie-2' => 'some-value-2',
'some-cookie-3' => 'some-value-3',
];

// Act
$output = $this->sut->toSlim($this->swooleRequest);

// Assert
$cookies = Cookies::fromRequest($output)->getAll();
$this->assertEquals(count($cookies), 3);
$this->assertEquals(FigRequestCookies::get($output, 'some-cookie-2')->getValue(), 'some-value-2');
}
}

0 comments on commit 7436675

Please sign in to comment.