Skip to content

Commit

Permalink
Modification on cookie API
Browse files Browse the repository at this point in the history
The cookie jar must be passed from the previous response to be used in another request
  • Loading branch information
bastien-phi committed Oct 3, 2017
1 parent 29d94f2 commit d4477c9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
43 changes: 24 additions & 19 deletions src/Zttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -301,6 +301,11 @@ function isServerError()
return $this->status() >= 500;
}

function cookies()
{
return $this->cookies;
}

function __call($method, $args)
{
if (static::hasMacro($method)) {
Expand Down
17 changes: 10 additions & 7 deletions tests/ZttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/server/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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();

0 comments on commit d4477c9

Please sign in to comment.