diff --git a/docs/en/02_Developer_Guides/06_Testing/01_Functional_Testing.md b/docs/en/02_Developer_Guides/06_Testing/01_Functional_Testing.md index 0799e453c..a6285c0ab 100644 --- a/docs/en/02_Developer_Guides/06_Testing/01_Functional_Testing.md +++ b/docs/en/02_Developer_Guides/06_Testing/01_Functional_Testing.md @@ -26,6 +26,18 @@ $page = $this->post($url); Performs a POST request on $url and retrieves the [HTTPResponse](api:SilverStripe\Control\HTTPResponse). This also changes the current page to the value of the response. +
+**Compatibility Notice:** Previous versions of SilverStripe would send a GET request if `post()` was called with no POST variables supplied in the second argument. +SilverStripe 4.6 and later always sends a POST request for consistency. +
+ +## Other Requests +```php +$page = $this->sendRequest('PUT', $url); +``` + +Performs a request on $url with the HTTP method provided (useful for PUT, PATCH, DELETE, etc.). This also changes the current page to the value of the response. + ## Submit diff --git a/src/Dev/TestSession.php b/src/Dev/TestSession.php index a27142632..2ea596d72 100644 --- a/src/Dev/TestSession.php +++ b/src/Dev/TestSession.php @@ -8,6 +8,7 @@ use SilverStripe\Control\Cookie_Backend; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Control\Session; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injector; @@ -100,7 +101,7 @@ class TestSession $url, null, $session ?: $this->session, - null, + 'GET', null, $headers, $cookies ?: $this->cookies @@ -123,6 +124,7 @@ class TestSession * @param string $body * @param array $cookies * @return HTTPResponse + * @throws HTTPResponse_Exception */ public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) { @@ -135,7 +137,7 @@ class TestSession $url, $data, $session ?: $this->session, - null, + 'POST', $body, $headers, $cookies ?: $this->cookies @@ -147,6 +149,47 @@ class TestSession return $this->lastResponse; } + /** + * Submit a request of any type + * + * @uses Director::test() + * @param string $method + * @param string $url + * @param array $data + * @param array $headers + * @param Session $session + * @param string $body + * @param array $cookies + * @return HTTPResponse + * @throws HTTPResponse_Exception + */ + public function sendRequest($method, $url, $data, $headers = null, $session = null, $body = null, $cookies = null) + { + $this->extend('updateRequestURL', $method, $url, $data, $headers, $session, $body, $cookies); + + $headers = (array) $headers; + if ($this->lastUrl && !isset($headers['Referer'])) { + $headers['Referer'] = $this->lastUrl; + } + + $this->lastResponse = Director::test( + $url, + $data, + $session ?: $this->session, + $method, + $body, + $headers, + $cookies ?: $this->cookies + ); + + $this->lastUrl = $url; + if (!$this->lastResponse) { + user_error("Director::test($url) returned null", E_USER_WARNING); + } + + return $this->lastResponse; + } + /** * Submit the form with the given HTML ID, filling it out with the given data. * Acts on the most recent response.