This repository has been archived by the owner on Mar 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jakejohns/feature/json-decoder
Add JsonDecoder middleware
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace Relay\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
|
||
class JsonDecoder | ||
{ | ||
public function __invoke(Request $request, Response $response, callable $next) | ||
{ | ||
$type = $request->getHeader('Content-Type'); | ||
$method = $request->getMethod(); | ||
|
||
if ('GET' != $method | ||
&& ! empty($type) | ||
&& 'application/json' == strtolower($type[0]) | ||
) { | ||
$body = (string) $request->getBody(); | ||
$request = $request->withParsedBody(json_decode($body)); | ||
} | ||
|
||
return $next($request, $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Relay\Middleware; | ||
|
||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequestFactory; | ||
use Zend\Diactoros\Stream; | ||
|
||
|
||
class JsonDecoderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
protected $data = ['foo', 'bar']; | ||
|
||
|
||
public function requestProvider() | ||
{ | ||
return [ | ||
['GET' , 'application/json', []], | ||
['GET' , null, []], | ||
['POST' , 'application/json', $this->data], | ||
['POST' , null, []], | ||
['PUT' , 'application/json', $this->data], | ||
['PUT' , null, []], | ||
['PATCH' , 'application/json', $this->data], | ||
['PATCH' , null, []], | ||
['other' , 'application/json', $this->data], | ||
['other' , null, []] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider requestProvider | ||
*/ | ||
public function testParser($method, $contentType, $expected) | ||
{ | ||
$json = json_encode($this->data); | ||
|
||
$stream = new Stream('php://temp', 'wb+'); | ||
$stream->write($json); | ||
|
||
$jsonDecoder = new JsonDecoder(); | ||
|
||
$response = new Response(); | ||
$request = ServerRequestFactory::fromGlobals() | ||
->withMethod($method) | ||
->withBody($stream); | ||
|
||
if ($contentType) { | ||
$request = $request->withHeader('Content-Type', $contentType); | ||
} | ||
|
||
$parsedRequest = $jsonDecoder( | ||
$request, | ||
$response, | ||
function ($request, $response) { | ||
return $request; | ||
} | ||
); | ||
|
||
$this->assertEquals($expected, $parsedRequest->getParsedBody()); | ||
} | ||
} |