Skip to content

Commit

Permalink
Add methods for sending cookies with test requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Sep 25, 2019
1 parent ba6e0fd commit 4542387
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
87 changes: 81 additions & 6 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
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
* @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
* @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;
}

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

0 comments on commit 4542387

Please sign in to comment.