-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.x] Add methods for sending cookies with test requests #30101
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,13 @@ | |||||
|
||||||
trait MakesHttpRequests | ||||||
{ | ||||||
/** | ||||||
* Additional cookies for the request. | ||||||
* | ||||||
* @var array | ||||||
*/ | ||||||
protected $defaultCookies = []; | ||||||
|
||||||
/** | ||||||
* Additional headers for the request. | ||||||
* | ||||||
|
@@ -32,6 +39,13 @@ trait MakesHttpRequests | |||||
*/ | ||||||
protected $followRedirects = false; | ||||||
|
||||||
/** | ||||||
* Indicates whether cookies should be encrypted. | ||||||
* | ||||||
* @var bool | ||||||
*/ | ||||||
protected $encryptCookies = true; | ||||||
|
||||||
/** | ||||||
* Define additional headers to be sent with the request. | ||||||
* | ||||||
|
@@ -131,6 +145,33 @@ public function withMiddleware($middleware = null) | |||||
return $this; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Define additional cookies to be sent with the request. | ||||||
* | ||||||
* @param array $cookies | ||||||
* @return $this | ||||||
*/ | ||||||
public function withCookies(array $cookies) | ||||||
{ | ||||||
$this->defaultCookies = array_merge($this->defaultCookies, $cookies); | ||||||
|
||||||
return $this; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add a cookie to be sent with the request. | ||||||
* | ||||||
* @param string $name | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @param string $value | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @return $this | ||||||
*/ | ||||||
public function withCookie(string $name, string $value) | ||||||
{ | ||||||
$this->defaultCookies[$name] = $value; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Automatically follow any redirects returned from the response. | ||||||
* | ||||||
|
@@ -143,6 +184,18 @@ public function followingRedirects() | |||||
return $this; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Disable automatic encryption of cookie values. | ||||||
* | ||||||
* @return $this | ||||||
*/ | ||||||
public function disableCookieEncryption() | ||||||
{ | ||||||
$this->encryptCookies = false; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Set the referer header and previous URL session value in order to simulate a previous request. | ||||||
* | ||||||
|
@@ -166,8 +219,9 @@ public function from(string $url) | |||||
public function get($uri, array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('GET', $uri, [], [], [], $server); | ||||||
return $this->call('GET', $uri, [], $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -193,8 +247,9 @@ public function getJson($uri, array $headers = []) | |||||
public function post($uri, array $data = [], array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('POST', $uri, $data, [], [], $server); | ||||||
return $this->call('POST', $uri, $data, $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -221,8 +276,9 @@ public function postJson($uri, array $data = [], array $headers = []) | |||||
public function put($uri, array $data = [], array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('PUT', $uri, $data, [], [], $server); | ||||||
return $this->call('PUT', $uri, $data, $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -249,8 +305,9 @@ public function putJson($uri, array $data = [], array $headers = []) | |||||
public function patch($uri, array $data = [], array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('PATCH', $uri, $data, [], [], $server); | ||||||
return $this->call('PATCH', $uri, $data, $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -277,8 +334,9 @@ public function patchJson($uri, array $data = [], array $headers = []) | |||||
public function delete($uri, array $data = [], array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('DELETE', $uri, $data, [], [], $server); | ||||||
return $this->call('DELETE', $uri, $data, $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -305,8 +363,9 @@ public function deleteJson($uri, array $data = [], array $headers = []) | |||||
public function options($uri, array $data = [], array $headers = []) | ||||||
{ | ||||||
$server = $this->transformHeadersToServerVars($headers); | ||||||
$cookies = $this->prepareCookiesForRequest(); | ||||||
|
||||||
return $this->call('OPTIONS', $uri, $data, [], [], $server); | ||||||
return $this->call('OPTIONS', $uri, $data, $cookies, [], $server); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -460,6 +519,22 @@ protected function extractFilesFromDataArray(&$data) | |||||
return $files; | ||||||
} | ||||||
|
||||||
/** | ||||||
* If enabled, encrypt cookie values for request. | ||||||
* | ||||||
* @return array | ||||||
*/ | ||||||
protected function prepareCookiesForRequest() | ||||||
{ | ||||||
if (! $this->encryptCookies) { | ||||||
return $this->defaultCookies; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what would happen here if I needed my own cookies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method only effects the code if you use these new cookie methods and one of the HTTP method helpers. As noted in the PR, if you have your own encryption/cookies, you can continue testing as you have before using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense, thanks for your reply. I am still a bit lost with the implementation tho. For instance, if I can Is it expected to remove the default? This is my main doubt by looking at the changes. |
||||||
} | ||||||
|
||||||
return collect($this->defaultCookies)->map(function ($value) { | ||||||
return encrypt($value, false); | ||||||
})->all(); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Follow a redirect chain until a non-redirect is received. | ||||||
* | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.