Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Zttp.php
  • Loading branch information
fyrts committed Jun 8, 2019
2 parents 6ecda16 + 46bcae6 commit 2da7f80
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
46 changes: 33 additions & 13 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 @@ -110,6 +112,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 @@ -162,21 +173,26 @@ function delete($url, $params = [])
function send($method, $url, $options)
{
try {
$response = $this->buildClient()->request($method, $url, $this->mergeOptions([
return tap(new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions([
'query' => $this->parseQueryParams($url),
'on_stats' => function (\GuzzleHttp\TransferStats $stats) use (&$transferStats) {
$transferStats = $stats;
'on_stats' => function (\GuzzleHttp\TransferStats $stats) {
$this->transferStats = $stats;
}
], $options));
return new ZttpResponse($response, $transferStats);
], $options))), function($response) {
$response->cookies = $this->cookies;
$response->transferStats = $this->transferStats;
});
} 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 @@ -190,15 +206,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 @@ -251,10 +267,9 @@ class ZttpResponse
__call as macroCall;
}

function __construct($response, $transferStats)
function __construct($response)
{
$this->response = $response;
$this->transferStats = $transferStats;
}

function body()
Expand Down Expand Up @@ -314,6 +329,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 @@ -503,6 +503,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();

0 comments on commit 2da7f80

Please sign in to comment.