From d8f0b8ccbd11ff64cbc5729955a4086a4a98cf49 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 31 May 2014 21:32:34 -0400 Subject: [PATCH 1/3] Use fewer lines to assign post/query params. --- src/Routing/RequestActionTrait.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Routing/RequestActionTrait.php b/src/Routing/RequestActionTrait.php index 37669e72a06..3912002309b 100644 --- a/src/Routing/RequestActionTrait.php +++ b/src/Routing/RequestActionTrait.php @@ -98,14 +98,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) { @@ -126,13 +118,14 @@ 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']; } + unset($extra['post'], $extra['query']); $params['session'] = isset($extra['session']) ? $extra['session'] : new Session(); From b0c0af006fafa7f5c0f8fe5e6961986510f51436 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 31 May 2014 21:41:45 -0400 Subject: [PATCH 2/3] Allow requestAction() to set environment values. Use the environment extra key to provide environment/header value overrides. Refs #3566 --- src/Routing/RequestActionTrait.php | 5 ++++- .../Routing/RequestActionTraitTest.php | 21 +++++++++++++++++-- .../Controller/RequestActionController.php | 3 ++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Routing/RequestActionTrait.php b/src/Routing/RequestActionTrait.php index 3912002309b..bc37ff6d81f 100644 --- a/src/Routing/RequestActionTrait.php +++ b/src/Routing/RequestActionTrait.php @@ -125,7 +125,10 @@ public function requestAction($url, array $extra = array()) { if (isset($extra['query'])) { $params['query'] = $extra['query']; } - unset($extra['post'], $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(); diff --git a/tests/TestCase/Routing/RequestActionTraitTest.php b/tests/TestCase/Routing/RequestActionTraitTest.php index f62d97950b6..6453fabfd1c 100644 --- a/tests/TestCase/Routing/RequestActionTraitTest.php +++ b/tests/TestCase/Routing/RequestActionTraitTest.php @@ -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); } /** @@ -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 * diff --git a/tests/test_app/TestApp/Controller/RequestActionController.php b/tests/test_app/TestApp/Controller/RequestActionController.php index 87de1177e2d..69a105f566d 100644 --- a/tests/test_app/TestApp/Controller/RequestActionController.php +++ b/tests/test_app/TestApp/Controller/RequestActionController.php @@ -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'), ])); } From 354dc4cf3d3c7482b5e6788a6833251e6ba0c673 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 31 May 2014 21:43:12 -0400 Subject: [PATCH 3/3] Add doc block example. --- src/Routing/RequestActionTrait.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Routing/RequestActionTrait.php b/src/Routing/RequestActionTrait.php index bc37ff6d81f..da5c8f92e0b 100644 --- a/src/Routing/RequestActionTrait.php +++ b/src/Routing/RequestActionTrait.php @@ -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.