Skip to content
Permalink
Browse files

Fix cookies not being passed for requested actions.

This also makes the request handler component pass cookies when
requesting actions to simulate redirects for AJAX requests.
  • Loading branch information...
ndm2
ndm2 committed Dec 7, 2015
1 parent 492cb4b commit 717e60d741d75630a5f89ab21a5ba0d5f6f262d9
@@ -271,7 +271,8 @@ public function beforeRedirect(Event $event, $url, Response $response)
'environment' => [
'REQUEST_METHOD' => 'GET'
],
'query' => $query
'query' => $query,
'cookies' => $request->cookies
]));
$response->statusCode(200);
return $response;
@@ -139,6 +139,9 @@ public function requestAction($url, array $extra = [])
if (isset($extra['query'])) {
$params['query'] = $extra['query'];
}
if (isset($extra['cookies'])) {
$params['cookies'] = $extra['cookies'];
}
if (isset($extra['environment'])) {
$params['environment'] = $extra['environment'] + $_SERVER + $_ENV;
}
@@ -941,6 +941,39 @@ public function testAjaxRedirectAsRequestActionWithQueryString()
$this->assertEquals('y?ish', $data['x']);
}
/**
* Test that AJAX requests involving redirects handle cookie data
*
* @return void
* @triggers Controller.beforeRedirect $this->Controller
*/
public function testAjaxRedirectAsRequestActionWithCookieData()
{
Configure::write('App.namespace', 'TestApp');
Router::connect('/:controller/:action');
$event = new Event('Controller.beforeRedirect', $this->Controller);
$this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
$this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
$this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$cookies = [
'foo' => 'bar'
];
$this->Controller->request->cookies = $cookies;
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
'/request_action/cookie_pass',
$this->Controller->response
);
$data = json_decode($response, true);
$this->assertEquals($cookies, $data);
}
/**
* Tests that AJAX requests involving redirects don't let the status code bleed through.
*
@@ -358,6 +358,24 @@ public function testRequestActionGetParameters()
$this->assertEquals('value', $result['query']['get']);
}
/**
* Test that requestAction handles cookies correctly.
*
* @return void
*/
public function testRequestActionCookies()
{
$cookies = [
'foo' => 'bar'
];
$result = $this->object->requestAction(
'/request_action/cookie_pass',
['cookies' => $cookies]
);
$result = json_decode($result, true);
$this->assertEquals($cookies, $result);
}
/**
* Test that environment overrides can be set.
*
@@ -108,6 +108,17 @@ public function query_pass()
return $this->response;
}
/**
* cookie pass, testing cookie passing
*
* @return \Cake\Network\Response
*/
public function cookie_pass()
{
$this->response->body(json_encode($this->request->cookies));
return $this->response;
}
/**
* test param passing and parsing.
*

0 comments on commit 717e60d

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