This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21a91b0
commit eaaf296
Showing
1 changed file
with
93 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see https://github.com/zendframework/zend-expressive for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Expressive\Helper; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequest; | ||
use Zend\Diactoros\Stream; | ||
use Zend\Expressive\Helper\BodyParamsMiddleware; | ||
|
||
class BodyParamsMiddlewareTest extends TestCase | ||
{ | ||
/** | ||
* @var Stream | ||
*/ | ||
protected $body; | ||
|
||
/** | ||
* @var BodyParamsMiddleware | ||
*/ | ||
protected $bodyParams; | ||
|
||
public function setUp() | ||
{ | ||
$this->bodyParams = new BodyParamsMiddleware(); | ||
|
||
$stream = fopen('php://memory', 'r+'); | ||
fwrite($stream, json_encode(['foo' => 'bar'])); | ||
|
||
$this->body = new Stream($stream); | ||
$this->body->rewind(); | ||
} | ||
|
||
public function testParsesRawBodyAndPreservesRawBodyInRequestAttribute() | ||
{ | ||
$serverRequest = new ServerRequest([], [], '', 'PUT', $this->body, ['Content-type' => 'application/json']); | ||
|
||
$this->bodyParams->__invoke( | ||
$serverRequest, | ||
new Response(), | ||
function ($request, $response) use (&$serverRequest) { | ||
$serverRequest = $request; | ||
|
||
return $response; | ||
} | ||
); | ||
|
||
$this->assertSame( | ||
json_encode(['foo' => 'bar']), | ||
$serverRequest->getAttribute('rawBody') | ||
); | ||
$this->assertSame(['foo' => 'bar'], $serverRequest->getParsedBody()); | ||
} | ||
|
||
public function notApplicableProvider() | ||
{ | ||
return [ | ||
['GET', 'application/json'], | ||
['HEAD', 'application/json'], | ||
['OPTIONS', 'application/json'], | ||
['POST', 'application/x-www-form-urlencoded'], | ||
['DELETE', 'this-isnt-a-real-content-type'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider notApplicableProvider | ||
*/ | ||
public function testRequestIsUnchangedWhenBodyParamsMiddlewareIsNotApplicable($method, $contentType) | ||
{ | ||
$originalRequest = new ServerRequest([], [], '', $method, $this->body, ['Content-type' => $contentType]); | ||
$finalRequest = null; | ||
|
||
$this->bodyParams->__invoke( | ||
$originalRequest, | ||
new Response(), | ||
function ($request, $response) use (&$finalRequest) { | ||
$finalRequest = $request; | ||
|
||
return $response; | ||
} | ||
); | ||
|
||
$this->assertSame($originalRequest, $finalRequest); | ||
} | ||
} |