Browse Source

Merge pull request #3618 from cakephp/3.0-requestaction-environment

3.0 requestaction environment
José Lorenzo Rodríguez 11 years ago
parent
commit
6e363d39f1

+ 20 - 13
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.
@@ -98,14 +109,6 @@ trait RequestActionTrait {
 			['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 +129,17 @@ trait RequestActionTrait {
 			}
 		}
 
-		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();
 

+ 19 - 2
tests/TestCase/Routing/RequestActionTraitTest.php

@@ -225,8 +225,7 @@ class RequestActionTraitTest extends TestCase {
 			array('post' => $_POST)
 		);
 		$result = json_decode($result, true);
-		$expected = $_POST;
-		$this->assertEquals($expected, $result);
+		$this->assertEquals($_POST, $result);
 	}
 
 /**
@@ -307,6 +306,24 @@ class RequestActionTraitTest extends TestCase {
 	}
 
 /**
+ * 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
  *
  * @return void

+ 2 - 1
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -104,7 +104,8 @@ class RequestActionController extends AppController {
 		$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'),
 		]));
 	}