MINOR: added cookies to Director::test()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@94684 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andreas Piening 2009-12-08 23:08:07 +00:00 committed by Sam Minnee
parent e3543daeca
commit 999995b83a
3 changed files with 17 additions and 12 deletions

View File

@ -170,12 +170,13 @@ class Director {
* Overwritten by $postVars['_method'] if present. * Overwritten by $postVars['_method'] if present.
* @param string $body The HTTP body * @param string $body The HTTP body
* @param array $headers HTTP headers with key-value pairs * @param array $headers HTTP headers with key-value pairs
* @param array $cookies to populate $_COOKIE
* @return SS_HTTPResponse * @return SS_HTTPResponse
* *
* @uses getControllerForURL() The rule-lookup logic is handled by this. * @uses getControllerForURL() The rule-lookup logic is handled by this.
* @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
*/ */
static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null) { static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null, $cookies = null) {
// These are needed so that calling Director::test() doesnt muck with whoever is calling it. // These are needed so that calling Director::test() doesnt muck with whoever is calling it.
// Really, it's some inapproriate coupling and should be resolved by making less use of statics // Really, it's some inapproriate coupling and should be resolved by making less use of statics
$oldStage = Versioned::current_stage(); $oldStage = Versioned::current_stage();
@ -216,7 +217,7 @@ class Director {
$_GET = (array)$getVars; $_GET = (array)$getVars;
$_POST = (array)$postVars; $_POST = (array)$postVars;
$_SESSION = $session ? $session->inst_getAll() : array(); $_SESSION = $session ? $session->inst_getAll() : array();
$_COOKIE = array(); $_COOKIE = (array) $cookies;
$_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring; $_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
$req = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body); $req = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);

View File

@ -86,20 +86,22 @@ class FunctionalTest extends SapphireTest {
/** /**
* Submit a get request * Submit a get request
* @uses Director::test()
*/ */
function get($url) { function get($url, $session = null, $headers = null, $cookies = null) {
$this->cssParser = null; $this->cssParser = null;
$response = $this->mainSession->get($url); $response = $this->mainSession->get($url, $session, $headers, $cookies);
if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) $response = $this->mainSession->followRedirection(); if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) $response = $this->mainSession->followRedirection();
return $response; return $response;
} }
/** /**
* Submit a post request * Submit a post request
* @uses Director::test()
*/ */
function post($url, $data) { function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) {
$this->cssParser = null; $this->cssParser = null;
$response = $this->mainSession->post($url, $data); $response = $this->mainSession->post($url, $data, $headers, $session, $body, $cookies);
if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) $response = $this->mainSession->followRedirection(); if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) $response = $this->mainSession->followRedirection();
return $response; return $response;
} }

View File

@ -40,9 +40,10 @@ class TestSession {
* Submit a get request * Submit a get request
* @uses Director::test() * @uses Director::test()
*/ */
function get($url) { function get($url, $session = null, $headers = null, $cookies = null) {
$headers = ($this->lastUrl) ? array('Referer'=>$this->lastUrl) : null; $headers = (array) $headers;
$this->lastResponse = Director::test($url, null, $this->session, null, null, $headers); if($this->lastUrl) $headers['Referer'] = $this->lastUrl;
$this->lastResponse = Director::test($url, null, $session ? $session : $this->session, null, null, $headers, $cookies);
$this->lastUrl = $url; $this->lastUrl = $url;
if(!$this->lastResponse) user_error("Director::test($url) returned null", E_USER_WARNING); if(!$this->lastResponse) user_error("Director::test($url) returned null", E_USER_WARNING);
return $this->lastResponse; return $this->lastResponse;
@ -52,9 +53,10 @@ class TestSession {
* Submit a post request * Submit a post request
* @uses Director::test() * @uses Director::test()
*/ */
function post($url, $data, $headers = null) { function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) {
$headers = ($this->lastUrl) ? array('Referer'=>$this->lastUrl) : null; $headers = (array) $headers;
$this->lastResponse = Director::test($url, $data, $this->session, null, null, $headers); if($this->lastUrl) $headers['Referer'] = $this->lastUrl;
$this->lastResponse = Director::test($url, $data, $session ? $session : $this->session, null, $body, $headers, $cookies);
$this->lastUrl = $url; $this->lastUrl = $url;
if(!$this->lastResponse) user_error("Director::test($url) returned null", E_USER_WARNING); if(!$this->lastResponse) user_error("Director::test($url) returned null", E_USER_WARNING);
return $this->lastResponse; return $this->lastResponse;