Skip to content
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

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 81 additions & 6 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

trait MakesHttpRequests
{
/**
* Additional cookies for the request.
*
* @var array
*/
protected $defaultCookies = [];

/**
* Additional headers for the request.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -131,6 +145,33 @@ public function withMiddleware($middleware = null)
return $this;
}

/**
* Define additional cookies to be sent with the request.
*
* @param array $cookies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array $cookies
* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param string $name
* @param string $name

* @param string $value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param string $value
* @param string $value

* @return $this
*/
public function withCookie(string $name, string $value)
{
$this->defaultCookies[$name] = $value;

return $this;
}

/**
* Automatically follow any redirects returned from the response.
*
Expand All @@ -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.
*
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would happen here if I needed my own cookies?

Copy link
Contributor Author

@jasonmccreary jasonmccreary Sep 26, 2019

Choose a reason for hiding this comment

The 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 call directly.

Copy link
Contributor

@gocanto gocanto Sep 26, 2019

Choose a reason for hiding this comment

The 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 withCookies, should I expect to send the default ones too?.

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.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ public function testWithoutAndWithMiddlewareWithParameter()
$this->app->make(MyMiddleware::class)->handle('foo', $next)
);
}

public function testWithCookieSetCookie()
{
$this->withCookie('foo', 'bar');

$this->assertCount(1, $this->defaultCookies);
$this->assertSame('bar', $this->defaultCookies['foo']);
}

public function testWithCookiesSetsCookiesAndOverwritesPreviousValues()
{
$this->withCookie('foo', 'bar');
$this->withCookies([
'foo' => 'baz',
'new-cookie' => 'new-value',
]);

$this->assertCount(2, $this->defaultCookies);
$this->assertSame('baz', $this->defaultCookies['foo']);
$this->assertSame('new-value', $this->defaultCookies['new-cookie']);
}
}

class MyMiddleware
Expand Down