Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Creates an integration test to attempt to recreate the problem reported
in zendframework/zend-expressive#371. It sets up the following
conditions:

- A request method that can provide body content
- A request body with JSON content
- A `Content-Type` request header with the value `application/json;charset=utf-8`

It then passes the request to the `BodyParams` middleware, and validates
that the request received by the delegate has the parsed contents.

Unfortunately... it does not validate the issue reported, as the test
passes.
  • Loading branch information
weierophinney committed May 8, 2017
1 parent 8192720 commit 240e412
Showing 1 changed file with 79 additions and 17 deletions.
96 changes: 79 additions & 17 deletions test/BodyParams/BodyParamsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public function setUp()
$this->body->rewind();
}

private function mockDelegate(callable $callback)
{
$delegate = $this->prophesize(DelegateInterface::class);

$delegate
->process(Argument::type(ServerRequestInterface::class))
->will(function ($args) use ($callback) {
$request = $args[0];
return $callback($request);
});

return $delegate;
}

private function mockDelegateToNeverTrigger()
{
$delegate = $this->prophesize(DelegateInterface::class);

$delegate
->process(Argument::type(ServerRequestInterface::class))
->shouldNotBeCalled();

return $delegate;
}

public function jsonProvider()
{
return [
Expand Down Expand Up @@ -181,28 +206,65 @@ public function testThrowsMalformedRequestBodyExceptionWhenRequestBodyIsNotValid
);
}

private function mockDelegate(callable $callback)
public function jsonBodyRequests()
{
$delegate = $this->prophesize(DelegateInterface::class);

$delegate
->process(Argument::type(ServerRequestInterface::class))
->will(function ($args) use ($callback) {
$request = $args[0];
return $callback($request);
});

return $delegate;
return [
'POST' => ['POST'],
'PUT' => ['PUT'],
'PATCH' => ['PATCH'],
'DELETE' => ['DELETE'],
];
}

private function mockDelegateToNeverTrigger()
/**
* @dataProvider jsonBodyRequests
* @param string $method
*/
public function testParsesJsonBodyWhenExpected($method)
{
$delegate = $this->prophesize(DelegateInterface::class);
$stream = fopen('php://memory', 'wb+');
fwrite($stream, json_encode(['foo' => 'bar']));
$body = new Stream($stream);

$serverRequest = new ServerRequest(
[],
[],
'',
$method,
$body,
['Content-type' => 'application/json;charset=utf-8']
);

$delegate
->process(Argument::type(ServerRequestInterface::class))
->shouldNotBeCalled();
$delegateTriggered = false;

return $delegate;
$result = $this->bodyParams->process(
$serverRequest,
$this->mockDelegate(function (ServerRequestInterface $request) use ($serverRequest, &$delegateTriggered) {
$delegateTriggered = true;

$this->assertNotSame(
$request,
$serverRequest,
'Request passed to delegate is the same as the one passed to BodyParamsMiddleware and should not be'
);

$this->assertSame(
json_encode(['foo' => 'bar']),
$request->getAttribute('rawBody'),
'Request passed to delegate does not contain expected rawBody contents'
);

$this->assertSame(
['foo' => 'bar'],
$request->getParsedBody(),
'Request passed to delegate does not contain expected parsed body'
);

return new Response();
})->reveal()
);

$this->assertInstanceOf(Response::class, $result);
$this->assertTrue($delegateTriggered);
}
}

0 comments on commit 240e412

Please sign in to comment.