Skip to content
Permalink
Browse files

Add tests and fix multi-value headers not being transformed.

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 0e0195f8b94f5b8e8e550d5316ae43797f0ffedf
Showing with 10 additions and 7 deletions.
  1. +4 −7 src/Http/RequestTransformer.php
  2. +6 −0 tests/TestCase/Http/RequestTransformerTest.php
@@ -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)) {
@@ -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());

0 comments on commit 0e0195f

Please sign in to comment.
You can’t perform that action at this time.