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

Add the ability to share cookies between requests #41

Merged
merged 7 commits into from
May 27, 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
39 changes: 30 additions & 9 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,6 +105,15 @@ function withDigestAuth($username, $password)
});
}

function withCookies($cookies)
{
return tap($this, function($request) use ($cookies) {
return $this->options = array_merge_recursive($this->options, [
'cookies' => $cookies,
]);
});
}

function timeout($seconds)
{
return tap($this, function () use ($seconds) {
Expand Down Expand Up @@ -155,17 +166,22 @@ function delete($url, $params = [])
function send($method, $url, $options)
{
try {
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;
});
} catch (\GuzzleHttp\Exception\ConnectException $e) {
throw new ConnectionException($e->getMessage(), 0, $e);
}
}

function buildClient()
{
return new \GuzzleHttp\Client(['handler' => $this->buildHandlerStack()]);
return new \GuzzleHttp\Client([
'handler' => $this->buildHandlerStack(),
'cookies' => true,
]);
}

function buildHandlerStack()
Expand All @@ -178,15 +194,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 @@ -296,6 +312,11 @@ function isServerError()
return $this->status() >= 500;
}

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

function __toString()
{
return $this->body();
Expand Down
15 changes: 15 additions & 0 deletions tests/ZttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,21 @@ function client_will_force_timeout()
{
Zttp::timeout(1)->get($this->url('/timeout'));
}

/** @test */
function cookies_can_be_shared_between_requests()
{
$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']);
}
}

class ZttpServer
Expand Down
13 changes: 13 additions & 0 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 @@ -105,4 +106,16 @@ function build_response($request)
], 200);
});

$app->router->get('/set-cookie', function() {
return response(null, 200)->withCookie(
new \Symfony\Component\HttpFoundation\Cookie('foo', 'bar')
);
});

$app->router->get('/set-another-cookie', function() {
return response(null, 200)->withCookie(
new \Symfony\Component\HttpFoundation\Cookie('baz', 'qux')
);
});

$app->run();