Skip to content
Permalink
Browse files

Add integration tests for PSR7 + request bodies.

  • Loading branch information...
markstory committed Jun 18, 2016
1 parent 93a8836 commit a8567c5d3845057832e024557d564491a5d1fcf1
@@ -21,6 +21,7 @@
use LogicException;
use ReflectionClass;
use ReflectionException;
use Zend\Diactoros\Stream;
/**
* Dispatches a request capturing the response for integration
@@ -67,14 +68,36 @@ public function execute($request)
);
$server = new Server($app);
$psrRequest = ServerRequestFactory::fromGlobals(
array_merge($_SERVER, $request['environment'], ['REQUEST_URI' => $request['url']]),
$request['query'],
$request['post'],
$request['cookies']
);
$psrRequest = $psrRequest->withAttribute('session', $request['session']);
$psrRequest = $this->_createRequest($request);
$response = $server->run($psrRequest);
return ResponseTransformer::toCake($response);
}
/**
* Create a PSR7 request from the request spec.
*
* @param array $spec The request spec.
* @return Psr\Http\Message\RequestInterface
*/
protected function _createRequest($spec)
{
if (isset($spec['input'])) {
$spec['post'] = [];
}
$request = ServerRequestFactory::fromGlobals(
array_merge($_SERVER, $spec['environment'], ['REQUEST_URI' => $spec['url']]),
$spec['query'],
$spec['post'],
$spec['cookies']
);
$request = $request->withAttribute('session', $spec['session']);
if (isset($spec['input'])) {
$stream = new Stream('php://memory', 'rw');
$stream->write($spec['input']);
$stream->rewind();
$request = $request->withBody($stream);
}
return $request;
}
}
@@ -229,7 +229,7 @@ public function testGetCookiesHttpServer()
}
/**
* Test that the PSR7 requests get post data
* Test that the PSR7 requests receive post data
*
* @return void
*/
@@ -243,6 +243,20 @@ public function testPostDataHttpServer()
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests receive encoded data.
*
* @return void
*/
public function testInputDataHttpServer()
{
$this->useHttpServer(true);
$this->post('/request_action/input_test', '{"hello":"world"}');
$this->assertSame('world', $this->_response->body());
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get cookies
*
@@ -166,6 +166,17 @@ public function session_test()
return $this->response;
}
/**
* Tests input data transmission
*
* @return \Cake\Network\Response
*/
public function input_test()
{
$this->response->body($this->request->input('json_decode')->hello);
return $this->response;
}
/**
* Tests exception handling
*

0 comments on commit a8567c5

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