From 36d783ce8dbd8736e694ff60ae66e542c62411c3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 4 Mar 2020 16:50:57 -0600 Subject: [PATCH] fix with cookies --- src/Illuminate/Http/Client/PendingRequest.php | 8 +++++--- tests/Http/HttpClientTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 3b590d6251fa..62859c1d4756 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -3,6 +3,7 @@ namespace Illuminate\Http\Client; use GuzzleHttp\Client; +use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\HandlerStack; use Illuminate\Support\Traits\Macroable; @@ -285,13 +286,14 @@ public function withToken($token, $type = 'Bearer') * Specify the cookies that should be included with the request. * * @param array $cookies + * @param string $domain * @return $this */ - public function withCookies(array $cookies) + public function withCookies(array $cookies, string $domain) { - return tap($this, function ($request) use ($cookies) { + return tap($this, function ($request) use ($cookies, $domain) { return $this->options = array_merge_recursive($this->options, [ - 'cookies' => $cookies, + 'cookies' => CookieJar::fromArray($cookies, $domain), ]); }); } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 051132ca5bd5..eabc427eab02 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -193,4 +193,22 @@ public function testFakeSequence() $this->assertSame(201, $this->factory->get('https://example.com')->status()); $this->assertSame(301, $this->factory->get('https://example.com')->status()); } + + public function testWithCookies() + { + $this->factory->fakeSequence()->pushStatus(200); + + $response = $this->factory->withCookies( + ['foo' => 'bar'], 'https://laravel.com' + )->get('https://laravel.com'); + + $this->assertCount(1, $response->cookies()->toArray()); + + /** @var CookieJarInterface $responseCookies */ + $responseCookie = $response->cookies()->toArray()[0]; + + $this->assertSame('foo', $responseCookie['Name']); + $this->assertSame('bar', $responseCookie['Value']); + $this->assertSame('https://laravel.com', $responseCookie['Domain']); + } }