mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
bf5a46901c
commit
29943f9049
@ -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
|
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.
|
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
|
## Submit
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ use SilverStripe\Control\Cookie_Backend;
|
|||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Control\HTTPResponse;
|
use SilverStripe\Control\HTTPResponse;
|
||||||
|
use SilverStripe\Control\HTTPResponse_Exception;
|
||||||
use SilverStripe\Control\Session;
|
use SilverStripe\Control\Session;
|
||||||
use SilverStripe\Core\Extensible;
|
use SilverStripe\Core\Extensible;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
@ -100,7 +101,7 @@ class TestSession
|
|||||||
$url,
|
$url,
|
||||||
null,
|
null,
|
||||||
$session ?: $this->session,
|
$session ?: $this->session,
|
||||||
null,
|
'GET',
|
||||||
null,
|
null,
|
||||||
$headers,
|
$headers,
|
||||||
$cookies ?: $this->cookies
|
$cookies ?: $this->cookies
|
||||||
@ -123,6 +124,7 @@ class TestSession
|
|||||||
* @param string $body
|
* @param string $body
|
||||||
* @param array $cookies
|
* @param array $cookies
|
||||||
* @return HTTPResponse
|
* @return HTTPResponse
|
||||||
|
* @throws HTTPResponse_Exception
|
||||||
*/
|
*/
|
||||||
public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null)
|
public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null)
|
||||||
{
|
{
|
||||||
@ -135,7 +137,7 @@ class TestSession
|
|||||||
$url,
|
$url,
|
||||||
$data,
|
$data,
|
||||||
$session ?: $this->session,
|
$session ?: $this->session,
|
||||||
null,
|
'POST',
|
||||||
$body,
|
$body,
|
||||||
$headers,
|
$headers,
|
||||||
$cookies ?: $this->cookies
|
$cookies ?: $this->cookies
|
||||||
@ -147,6 +149,47 @@ class TestSession
|
|||||||
return $this->lastResponse;
|
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.
|
* Submit the form with the given HTML ID, filling it out with the given data.
|
||||||
* Acts on the most recent response.
|
* Acts on the most recent response.
|
||||||
|
Loading…
Reference in New Issue
Block a user