Skip to content
Permalink
Browse files

Make IntegrationTestCase::_buildRequest return an array.

This is a breaking change to the protected method, but I feel its
necessary in order to make other things easier. For example, getting all
headers out of a Cake\Request is not possible right now. By using the
array return value we can easily access all the different aspects of the
simulated request.
  • Loading branch information...
markstory committed Jun 15, 2016
1 parent 0ef9d47 commit 43f8ed3fa19e95380a9c7da8a6f9cb4a0a1c4c8a
@@ -393,7 +393,7 @@ protected function _sendRequest($url, $method, $data = [])
try {
$request = $this->_buildRequest($url, $method, $data);
$response = $dispatcher->execute($request);
$this->_requestSession = $request->session();
$this->_requestSession = $request['session'];
$this->_response = $response;
} catch (PHPUnit_Exception $e) {
throw $e;
@@ -455,7 +455,7 @@ protected function _handleError($exception)
* @param string|array $url The URL
* @param string $method The HTTP method
* @param array|null $data The request data.
* @return \Cake\Network\Request The built request.
* @return array The request context
*/
protected function _buildRequest($url, $method, $data)
{
@@ -486,7 +486,7 @@ protected function _buildRequest($url, $method, $data)
$env['REQUEST_METHOD'] = $method;
$props['environment'] = $env;
$props = Hash::merge($props, $this->_request);
return new Request($props);
return $props;
}
/**
@@ -68,19 +68,13 @@ public function execute($request)
$server = new Server($app);
// TODO How to handle passing all headers.
// The Request doesn't expose a way to read all headers values.
// TODO How to pass session data? PSR7 requests don't handle sessions..
// TODO pass php://input stream, base, webroot
$serverData = [
'REQUEST_URI' => $request->here,
'REQUEST_METHOD' => $request->method(),
];
$psrRequest = ServerRequestFactory::fromGlobals(
array_merge($_SERVER, $serverData),
$request->query,
$request->data(),
$request->cookies
array_merge($_SERVER, $request['environment'], ['REQUEST_URI' => $request['url']]),
$request['query'],
$request['post'],
$request['cookies']
);
$response = $server->run($psrRequest);
return ResponseTransformer::toCake($response);
@@ -40,11 +40,12 @@ public function __construct($test)
/**
* Run a request and get the response.
*
* @param \Cake\Network\Request $request The request to execute.
* @param array $request The request context to execute.
* @return \Cake\Network\Response The generated response.
*/
public function execute($request)
{
$request = new Request($request);
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->eventManager()->on(
@@ -66,13 +66,13 @@ public function testRequestBuilding()
$this->session(['User' => ['id' => 1, 'username' => 'mark']]);
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertEquals('abc123', $request->header('X-CSRF-Token'));
$this->assertEquals('tasks/add', $request->url);
$this->assertArrayHasKey('split_token', $request->cookies);
$this->assertEquals('def345', $request->cookies['split_token']);
$this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
$this->assertEquals('foo', $request->env('PHP_AUTH_USER'));
$this->assertEquals('bar', $request->env('PHP_AUTH_PW'));
$this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
$this->assertEquals('/tasks/add', $request['url']);
$this->assertArrayHasKey('split_token', $request['cookies']);
$this->assertEquals('def345', $request['cookies']['split_token']);
$this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
$this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
$this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
}
/**
@@ -85,18 +85,18 @@ public function testRequestBuildingCsrfTokens()
$this->enableCsrfToken();
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertArrayHasKey('csrfToken', $request->cookies);
$this->assertArrayHasKey('_csrfToken', $request->data);
$this->assertSame($request->cookies['csrfToken'], $request->data['_csrfToken']);
$this->assertArrayHasKey('csrfToken', $request['cookies']);
$this->assertArrayHasKey('_csrfToken', $request['post']);
$this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
$this->cookie('csrfToken', '');
$request = $this->_buildRequest('/tasks/add', 'POST', [
'_csrfToken' => 'fale',
'title' => 'First post'
]);
$this->assertSame('', $request->cookies['csrfToken']);
$this->assertSame('fale', $request->data['_csrfToken']);
$this->assertSame('', $request['cookies']['csrfToken']);
$this->assertSame('fale', $request['post']['_csrfToken']);
}
/**
@@ -109,10 +109,14 @@ public function testEnableCsrfMultipleRequests()
$this->enableCsrfToken();
$first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
$this->assertSame($first->cookies['csrfToken'], $second->data['_csrfToken'], 'Csrf token should match cookie');
$this->assertSame(
$first->data['_csrfToken'],
$second->data['_csrfToken'],
$first['cookies']['csrfToken'],
$second['post']['_csrfToken'],
'Csrf token should match cookie'
);
$this->assertSame(
$first['post']['_csrfToken'],
$second['post']['_csrfToken'],
'Tokens should be consistent per test method'
);
}
@@ -128,8 +132,8 @@ public function testEnableCsrfPredeterminedCookie()
$value = 'I am a teapot';
$this->cookie('csrfToken', $value);
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertSame($value, $request->cookies['csrfToken'], 'Csrf token should match cookie');
$this->assertSame($value, $request->data['_csrfToken'], 'Tokens should match');
$this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
$this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
}
/**
@@ -141,8 +145,8 @@ public function testRequestBuildingQueryParameters()
{
$request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
$this->assertEquals('/tasks/view?archived=yes', $request->here());
$this->assertEquals('yes', $request->query('archived'));
$this->assertEquals('/tasks/view', $request['url']);
$this->assertEquals('yes', $request['query']['archived']);
}
/**
@@ -155,7 +159,7 @@ public function testCookieEncrypted()
Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
$this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
$request = $this->_buildRequest('/tasks/view', 'GET', []);
$this->assertStringStartsWith('Q2FrZQ==.', $request->cookies['KeyOfCookie']);
$this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
}
/**
@@ -188,8 +192,58 @@ public function testGetHttpServer()
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Network\Response', $this->_response);
$this->assertEquals('This is a test', $this->_response->body());
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get query string data
*
* @return void
*/
public function testQueryStringHttpServer()
{
$this->useHttpServer(true);
$this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
$this->get('/request_action/params_pass?q=query');
$this->assertResponseOk();
$this->assertResponseContains('"q":"query"');
$this->assertResponseContains('"contentType":"text\/plain"');
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get cookies
*
* @return void
*/
public function testGetCookiesHttpServer()
{
$this->useHttpServer(true);
$this->configRequest(['cookies' => ['split_test' => 'abc']]);
$this->get('/request_action/cookie_pass');
$this->assertResponseOk();
$this->assertResponseContains('"split_test":"abc"');
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get post data
*
* @return void
*/
public function testPostDataHttpServer()
{
$this->useHttpServer(true);
$this->post('/request_action/post_pass', ['title' => 'value']);
$data = json_decode($this->_response->body());
$this->assertEquals('value', $data->title);
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test sending requests stores references to controller/view/layout.
*
@@ -216,9 +270,8 @@ public function testRequestSetsProperties()
*/
public function testRequestSetsPropertiesHttpServer()
{
$this->markTestIncomplete('not done');
DispatcherFactory::clear();
$this->useHttpServer(true);
DispatcherFactory::clear();
$this->post('/posts/index');
$this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
@@ -232,16 +285,6 @@ public function testRequestSetsPropertiesHttpServer()
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test that the PSR7 requests get post, cookies, and other request data passed along.
*
* @return void
*/
public function testPsrRequestData()
{
$this->markTestIncomplete('not done');
}
/**
* Assert that the stored template doesn't change when cells are rendered.
*
@@ -34,6 +34,10 @@ public function bootstrap()
public function middleware($middleware)
{
$middleware->push(new RoutingMiddleware());
$middleware->push(function ($req, $res, $next) {
$res = $next($req, $res);
return $res->withHeader('X-Middleware', 'true');
});
return $middleware;
}
}
@@ -134,7 +134,7 @@ public function params_pass()
'params' => $this->request->params,
'query' => $this->request->query,
'url' => $this->request->url,
'contentType' => $this->request->env('CONTENT_TYPE'),
'contentType' => $this->request->contentType(),
]));
return $this->response;
}

0 comments on commit 43f8ed3

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