Skip to content

Commit

Permalink
Add tests and fix multi-value headers not being transformed.
Browse files Browse the repository at this point in the history
I've also reworked how headers + server data are merged. The original
intent was to allow headers set in the request to overwrite server data,
which is still the case. These changes also allow headers not set in the
server environment to be transformed as well.

Refs #9967
  • Loading branch information
markstory committed Jan 5, 2017
1 parent 18e74ac commit 0e0195f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/Http/RequestTransformer.php
Expand Up @@ -40,15 +40,12 @@ class RequestTransformer
public static function toCake(PsrRequest $request)
{
$post = $request->getParsedBody();
$server = $request->getServerParams();
$headers = $request->getHeaders();

foreach ($headers as $k => $value) {
$headers = [];
foreach ($request->getHeaders() as $k => $value) {
$name = sprintf('HTTP_%s', strtoupper(str_replace('-', '_', $k)));
if (isset($server[$name])) {
$server[$name] = $value[0];
}
$headers[$name] = implode(',', $value);
}
$server = $headers + $request->getServerParams();

$files = static::getFiles($request);
if (!empty($files)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/Http/RequestTransformerTest.php
Expand Up @@ -87,12 +87,18 @@ public function testToCakeHeadersAndEnvironment()
'SERVER_PORT' => 443,
];
$psr = ServerRequestFactory::fromGlobals($server);
$psr = $psr->withHeader('Api-Token', 'abc123')
->withAddedHeader('X-thing', 'one')
->withAddedHeader('X-thing', 'two');

$cake = RequestTransformer::toCake($psr);
$this->assertEmpty($cake->query);
$this->assertEmpty($cake->data);
$this->assertEmpty($cake->cookie);

$this->assertSame('application/json', $cake->header('accept'));
$this->assertSame('abc123', $cake->header('Api-Token'));
$this->assertSame('one,two', $cake->header('X-thing'));
$this->assertSame('PATCH', $cake->method());
$this->assertSame('https', $cake->scheme());
$this->assertSame(443, $cake->port());
Expand Down

0 comments on commit 0e0195f

Please sign in to comment.