From c0aa9b30f99deaeb3cae611fa871541d8d3bb906 Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Fri, 4 Aug 2017 23:29:31 +0200 Subject: [PATCH 1/5] Add the ability to share cookies between requests --- src/Zttp.php | 16 ++++++++++++++++ tests/ZttpTest.php | 12 ++++++++++++ tests/server/public/index.php | 9 +++++++++ 3 files changed, 37 insertions(+) diff --git a/src/Zttp.php b/src/Zttp.php index 4d53254..6486676 100755 --- a/src/Zttp.php +++ b/src/Zttp.php @@ -103,6 +103,22 @@ function withDigestAuth($username, $password) }); } + static $cookieJar; + + function withCookies() + { + return tap($this, function($request) { + return $this->options = array_merge_recursive($this->options, [ + 'cookies' => static::getCookieJar(), + ]); + }); + } + + static function getCookieJar() + { + return static::$cookieJar ? : static::$cookieJar = new \GuzzleHttp\Cookie\CookieJar(); + } + function beforeSending($callback) { return tap($this, function () use ($callback) { diff --git a/tests/ZttpTest.php b/tests/ZttpTest.php index 993c5e9..9347a9b 100644 --- a/tests/ZttpTest.php +++ b/tests/ZttpTest.php @@ -462,6 +462,18 @@ function can_use_basic_auth() $this->assertTrue($response->isOk()); } + + /** @test */ + function cookies_can_be_shared_between_requests() + { + $response = Zttp::withCookies()->get($this->url('/echo-cookie')); + $this->assertEmpty($response->body()); + Zttp::withCookies()->get($this->url('/set-cookie')); + $response = Zttp::withCookies()->get($this->url('/echo-cookie')); + $this->assertEquals('bar', $response->body()); + $response = Zttp::get($this->url('/echo-cookie')); + $this->assertEmpty($response->body()); + } } class ZttpServer diff --git a/tests/server/public/index.php b/tests/server/public/index.php index 427dd59..9f6aec9 100644 --- a/tests/server/public/index.php +++ b/tests/server/public/index.php @@ -67,4 +67,13 @@ function build_response($request) ], 200); }); +$app->get('/set-cookie', function() { + return response(null, 200) + ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('foo','bar')); +}); + +$app->get('/echo-cookie', function() { + return response(app('request')->cookies->get('foo'), 200); +}); + $app->run(); From 3c375998c1b61116c91ec212c1772fffcfdc3e04 Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Fri, 4 Aug 2017 23:38:30 +0200 Subject: [PATCH 2/5] Fix code style --- src/Zttp.php | 2 +- tests/server/public/index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Zttp.php b/src/Zttp.php index 6486676..fd18ef7 100755 --- a/src/Zttp.php +++ b/src/Zttp.php @@ -116,7 +116,7 @@ function withCookies() static function getCookieJar() { - return static::$cookieJar ? : static::$cookieJar = new \GuzzleHttp\Cookie\CookieJar(); + return static::$cookieJar ?: static::$cookieJar = new \GuzzleHttp\Cookie\CookieJar(); } function beforeSending($callback) diff --git a/tests/server/public/index.php b/tests/server/public/index.php index 9f6aec9..00678d7 100644 --- a/tests/server/public/index.php +++ b/tests/server/public/index.php @@ -69,7 +69,7 @@ function build_response($request) $app->get('/set-cookie', function() { return response(null, 200) - ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('foo','bar')); + ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('foo', 'bar')); }); $app->get('/echo-cookie', function() { From 29d94f21bf815d4218ca54837ac1110090dca9f9 Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Fri, 4 Aug 2017 23:43:09 +0200 Subject: [PATCH 3/5] Better code style --- tests/server/public/index.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/server/public/index.php b/tests/server/public/index.php index 00678d7..770d8d9 100644 --- a/tests/server/public/index.php +++ b/tests/server/public/index.php @@ -68,8 +68,9 @@ function build_response($request) }); $app->get('/set-cookie', function() { - return response(null, 200) - ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('foo', 'bar')); + return response(null, 200)->withCookie( + new \Symfony\Component\HttpFoundation\Cookie('foo', 'bar') + ); }); $app->get('/echo-cookie', function() { From d4477c91cbc6ca3b951a4ff12ebf94e35538cdae Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Tue, 3 Oct 2017 09:21:53 +0200 Subject: [PATCH 4/5] Modification on cookie API The cookie jar must be passed from the previous response to be used in another request --- src/Zttp.php | 43 +++++++++++++++++++---------------- tests/ZttpTest.php | 17 ++++++++------ tests/server/public/index.php | 7 ++++-- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/Zttp.php b/src/Zttp.php index fd18ef7..ca17a7f 100755 --- a/src/Zttp.php +++ b/src/Zttp.php @@ -14,7 +14,9 @@ class PendingZttpRequest { function __construct() { - $this->beforeSendingCallbacks = collect(); + $this->beforeSendingCallbacks = collect([ function ($request, $options) { + $this->cookies = $options['cookies']; + }]); $this->bodyFormat = 'json'; $this->options = [ 'http_errors' => false, @@ -103,22 +105,15 @@ function withDigestAuth($username, $password) }); } - static $cookieJar; - - function withCookies() + function withCookies($cookies) { - return tap($this, function($request) { + return tap($this, function($request) use ($cookies) { return $this->options = array_merge_recursive($this->options, [ - 'cookies' => static::getCookieJar(), + 'cookies' => $cookies, ]); }); } - static function getCookieJar() - { - return static::$cookieJar ?: static::$cookieJar = new \GuzzleHttp\Cookie\CookieJar(); - } - function beforeSending($callback) { return tap($this, function () use ($callback) { @@ -163,14 +158,19 @@ function delete($url, $params = []) function send($method, $url, $options) { - return new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions([ - 'query' => $this->parseQueryParams($url), - ], $options))); + return tap(new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions( + ['query' => $this->parseQueryParams($url)], $options))), + function($response) { + $response->cookies = $this->cookies; + }); } function buildClient() { - return new \GuzzleHttp\Client(['handler' => $this->buildHandlerStack()]); + return new \GuzzleHttp\Client([ + 'handler' => $this->buildHandlerStack(), + 'cookies' => true, + ]); } function buildHandlerStack() @@ -183,15 +183,15 @@ function buildBeforeSendingHandler() { return function ($handler) { return function ($request, $options) use ($handler) { - return $handler($this->runBeforeSendingCallbacks($request), $options); + return $handler($this->runBeforeSendingCallbacks($request, $options), $options); }; }; } - function runBeforeSendingCallbacks($request) + function runBeforeSendingCallbacks($request, $options) { - return tap($request, function ($request) { - $this->beforeSendingCallbacks->each->__invoke(new ZttpRequest($request)); + return tap($request, function ($request) use ($options) { + $this->beforeSendingCallbacks->each->__invoke(new ZttpRequest($request), $options); }); } @@ -301,6 +301,11 @@ function isServerError() return $this->status() >= 500; } + function cookies() + { + return $this->cookies; + } + function __call($method, $args) { if (static::hasMacro($method)) { diff --git a/tests/ZttpTest.php b/tests/ZttpTest.php index 9347a9b..0810306 100644 --- a/tests/ZttpTest.php +++ b/tests/ZttpTest.php @@ -466,13 +466,16 @@ function can_use_basic_auth() /** @test */ function cookies_can_be_shared_between_requests() { - $response = Zttp::withCookies()->get($this->url('/echo-cookie')); - $this->assertEmpty($response->body()); - Zttp::withCookies()->get($this->url('/set-cookie')); - $response = Zttp::withCookies()->get($this->url('/echo-cookie')); - $this->assertEquals('bar', $response->body()); - $response = Zttp::get($this->url('/echo-cookie')); - $this->assertEmpty($response->body()); + $response = Zttp::get($this->url('/set-cookie')); + $response = Zttp::withCookies($response->cookies())->get($this->url('/get')); + $this->assertEquals(['foo' => 'bar'], $response->json()['cookies']); + + $response = Zttp::withCookies($response->cookies())->get($this->url('/set-another-cookie')); + $response = Zttp::withCookies($response->cookies())->get($this->url('/get')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $response->json()['cookies']); + + $response = Zttp::get($this->url('/get')); + $this->assertEquals([], $response->json()['cookies']); } } diff --git a/tests/server/public/index.php b/tests/server/public/index.php index 770d8d9..07f4601 100644 --- a/tests/server/public/index.php +++ b/tests/server/public/index.php @@ -13,6 +13,7 @@ function build_response($request) 'query' => $request->query(), 'json' => $request->json()->all(), 'form_params' => $request->request->all(), + 'cookies' => $request->cookies->all(), ], $request->header('Z-Status', 200)); } @@ -73,8 +74,10 @@ function build_response($request) ); }); -$app->get('/echo-cookie', function() { - return response(app('request')->cookies->get('foo'), 200); +$app->get('/set-another-cookie', function() { + return response(null, 200)->withCookie( + new \Symfony\Component\HttpFoundation\Cookie('baz', 'qux') + ); }); $app->run(); From 6c9ac5dee4341179656526be38544cd6f9eab6fa Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Wed, 11 Oct 2017 08:56:54 +0200 Subject: [PATCH 5/5] fix test server as done in 9c5508e083f4470c1f30a5df2aaf66b6d0a9ea8a --- tests/server/public/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/server/public/index.php b/tests/server/public/index.php index 3749827..dbfa9d5 100644 --- a/tests/server/public/index.php +++ b/tests/server/public/index.php @@ -106,13 +106,13 @@ function build_response($request) ], 200); }); -$app->get('/set-cookie', function() { +$app->router->get('/set-cookie', function() { return response(null, 200)->withCookie( new \Symfony\Component\HttpFoundation\Cookie('foo', 'bar') ); }); -$app->get('/set-another-cookie', function() { +$app->router->get('/set-another-cookie', function() { return response(null, 200)->withCookie( new \Symfony\Component\HttpFoundation\Cookie('baz', 'qux') );