Skip to content
Permalink
Browse files

Allow request bodies to be transformed.

This is useful for API based applications.
  • Loading branch information...
markstory committed Jun 18, 2016
1 parent 3af2377 commit 93a8836cf13a387f9f77667fdd1eb771b76aeaed
Showing with 25 additions and 0 deletions.
  1. +7 −0 src/Http/RequestTransformer.php
  2. +18 −0 tests/TestCase/Http/RequestTransformerTest.php
@@ -48,6 +48,12 @@ public static function toCake(PsrRequest $request)
$post = Hash::merge($post, $files);
}
$input = null;
$stream = $request->getBody();
if ($stream->getSize()) {
$input = $stream->getContents();
}
return new CakeRequest([
'query' => $request->getQueryParams(),
'post' => $post,
@@ -58,6 +64,7 @@ public static function toCake(PsrRequest $request)
'base' => $request->getAttribute('base', ''),
'webroot' => $request->getAttribute('webroot', '/'),
'session' => $request->getAttribute('session', null),
'input' => $input,
]);
}
@@ -20,6 +20,7 @@
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\Stream;
/**
* Test for RequestTransformer
@@ -276,4 +277,21 @@ public function testToCakeSession()
$this->assertSame($session, $cake->session());
}
/**
* Test transforming request bodies
*
* @return void
*/
public function testToCakeRequestBody()
{
$psr = ServerRequestFactory::fromGlobals();
$stream = new Stream('php://memory', 'rw');
$stream->write('{"hello":"world"}');
$stream->rewind();
$psr = $psr->withBody($stream);
$cake = RequestTransformer::toCake($psr);
$this->assertSame(['hello' => 'world'], $cake->input('json_decode', true));
}
}

0 comments on commit 93a8836

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