Skip to content

Commit

Permalink
Merge pull request #13230 from cakephp/issue-13204
Browse files Browse the repository at this point in the history
Handle host & scheme in array URLs
  • Loading branch information
markstory committed May 14, 2019
2 parents ff1b648 + 75b1d02 commit cedcb68
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/TestSuite/IntegrationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use Exception;
use LogicException;
use PHPUnit\Exception as PhpunitException;
use Zend\Diactoros\Uri;

/**
* A trait intended to make integration tests of your controllers easier.
Expand Down Expand Up @@ -610,7 +611,7 @@ protected function _buildRequest($url, $method, $data)
];
$session = Session::create($sessionConfig);
$session->write($this->_session);
list($url, $query) = $this->_url($url);
list($url, $query, $hostInfo) = $this->_url($url);
$tokenUrl = $url;

if ($query) {
Expand Down Expand Up @@ -638,6 +639,12 @@ protected function _buildRequest($url, $method, $data)
'QUERY_STRING' => $query,
'REQUEST_URI' => $url,
];
if (!empty($hostInfo['ssl'])) {
$env['HTTPS'] = 'on';
}
if (isset($hostInfo['host'])) {
$env['HTTP_HOST'] = $hostInfo['host'];
}
if (isset($this->_request['headers'])) {
foreach ($this->_request['headers'] as $k => $v) {
$name = strtoupper(str_replace('-', '_', $k));
Expand Down Expand Up @@ -717,23 +724,23 @@ protected function _castToString($data)
* Creates a valid request url and parameter array more like Request::_url()
*
* @param string|array $url The URL
* @return array Qualified URL and the query parameters
* @return array Qualified URL, the query parameters, and host data
*/
protected function _url($url)
{
// re-create URL in ServerRequest's context so
// query strings are encoded as expected
$request = new ServerRequest(['url' => $url]);
$url = $request->getRequestTarget();
$uri = new Uri($url);
$path = $uri->getPath();
$query = $uri->getQuery();

$query = '';

$path = parse_url($url, PHP_URL_PATH);
if (strpos($url, '?') !== false) {
$query = parse_url($url, PHP_URL_QUERY);
$hostData = [];
if ($uri->getHost()) {
$hostData['host'] = $uri->getHost();
}
if ($uri->getScheme()) {
$hostData['ssl'] = $uri->getScheme() === 'https';
}

return [$path, $query];
return [$path, $query, $hostData];
}

/**
Expand Down
21 changes: 20 additions & 1 deletion tests/TestCase/TestSuite/IntegrationTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function testGetUsingApplicationWithPluginRoutes()
*/
public function testGetUsingApplicationWithDefaultRoutes()
{
// first clean routes to have Router::$initailized === false
// first clean routes to have Router::$initialized === false
Router::reload();

$this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);
Expand Down Expand Up @@ -684,6 +684,25 @@ public function testArrayUrls()
$this->assertEquals('value', $this->viewVariable('test'));
}

/**
* Test array URL with host
*
* @return void
*/
public function testArrayUrlWithHost()
{
$this->useHttpServer(true);
$this->get([
'controller' => 'Posts',
'action' => 'hostData',
'_host' => 'app.example.org',
'_ssl' => true,
]);
$this->assertResponseOk();
$this->assertResponseContains('"isSsl":true');
$this->assertResponseContains('"host":"app.example.org"');
}

/**
* Test array URLs with an empty router.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Controller/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ public function header()
return $this->getResponse()->withHeader('X-Cake', 'custom header');
}

public function hostData()
{
$data = [
'host' => $this->request->host(),
'isSsl' => $this->request->is('ssl'),
];

return $this->getResponse()->withStringBody(json_encode($data));
}

public function empty_response()
{
return $this->getResponse()->withStringBody('');
Expand Down

0 comments on commit cedcb68

Please sign in to comment.