API TestSession request methods now use the correct HTTP method (#8987)

* API TestSession request methods now use the correct HTTP method

* DOCS Update requests section in Functional Testing to reflect API change
This commit is contained in:
Garion Herman 2020-02-14 16:01:06 +13:00 committed by GitHub
parent bf5a46901c
commit 29943f9049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -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.
<div class="notice" markdown="1">
**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.
</div>
## 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

View File

@ -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.