Skip to content

Commit

Permalink
Merge pull request #3618 from cakephp/3.0-requestaction-environment
Browse files Browse the repository at this point in the history
3.0 requestaction environment
  • Loading branch information
lorenzo committed Jun 3, 2014
2 parents 2273621 + 354dc4c commit 6e363d3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
33 changes: 20 additions & 13 deletions src/Routing/RequestActionTrait.php
Expand Up @@ -67,6 +67,17 @@ trait RequestActionTrait {
* ]);
* }}}
*
* ### Sending environment or header values
*
* By default actions dispatched with this method will use the global $_SERVER and $_ENV
* values. If you want to override those values for a request action, you can specify the values:
*
* {{{
* $vars = $this->requestAction('/articles/popular', [
* 'environment' => ['CONTENT_TYPE' => 'application/json']
* ]);
* }}}
*
* ### Transmitting the session
*
* By default actions dispatched with this method will use the standard session object.
Expand Down Expand Up @@ -98,14 +109,6 @@ public function requestAction($url, array $extra = array()) {
['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1],
$extra
);
$post = $query = [];
if (isset($extra['post'])) {
$post = $extra['post'];
}
if (isset($extra['query'])) {
$query = $extra['query'];
}
unset($extra['post'], $extra['query']);

$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
Expand All @@ -126,13 +129,17 @@ public function requestAction($url, array $extra = array()) {
}
}

if (!empty($post)) {
$params['post'] = $post;
$params['post'] = $params['query'] = [];
if (isset($extra['post'])) {
$params['post'] = $extra['post'];
}

if (!empty($query)) {
$params['query'] = $query;
if (isset($extra['query'])) {
$params['query'] = $extra['query'];
}
if (isset($extra['environment'])) {
$params['environment'] = $extra['environment'] + $_SERVER + $_ENV;
}
unset($extra['environment'], $extra['post'], $extra['query']);

$params['session'] = isset($extra['session']) ? $extra['session'] : new Session();

Expand Down
21 changes: 19 additions & 2 deletions tests/TestCase/Routing/RequestActionTraitTest.php
Expand Up @@ -225,8 +225,7 @@ public function testRequestActionNoPostPassing() {
array('post' => $_POST)
);
$result = json_decode($result, true);
$expected = $_POST;
$this->assertEquals($expected, $result);
$this->assertEquals($_POST, $result);
}

/**
Expand Down Expand Up @@ -306,6 +305,24 @@ public function testRequestActionGetParameters() {
$this->assertEquals('value', $result['query']['get']);
}

/**
* Test that environment overrides can be set.
*
* @return void
*/
public function testRequestActionEnvironment() {
$result = $this->object->requestAction('/request_action/params_pass');
$result = json_decode($result, true);
$this->assertEquals('', $result['contentType'], 'Original content type not found.');

$result = $this->object->requestAction(
'/request_action/params_pass',
['environment' => ['CONTENT_TYPE' => 'application/json']]
);
$result = json_decode($result, true);
$this->assertEquals('application/json', $result['contentType']);
}

/**
* Tests that it is possible to transmit the session for the request
*
Expand Down
Expand Up @@ -104,7 +104,8 @@ public function params_pass() {
$this->response->body(json_encode([
'params' => $this->request->params,
'query' => $this->request->query,
'url' => $this->request->url
'url' => $this->request->url,
'contentType' => $this->request->env('CONTENT_TYPE'),
]));
}

Expand Down

0 comments on commit 6e363d3

Please sign in to comment.