From 6ecda1644ab6374f7ebf5cacc4451b9ed4ec7210 Mon Sep 17 00:00:00 2001 From: Sam Feyaerts Date: Tue, 2 Apr 2019 07:08:17 +0200 Subject: [PATCH] Add ability to get effective response URI --- src/Zttp.php | 16 +++++++++++++--- tests/ZttpTest.php | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Zttp.php b/src/Zttp.php index 3c20a58..7ca6d51 100755 --- a/src/Zttp.php +++ b/src/Zttp.php @@ -162,9 +162,13 @@ function delete($url, $params = []) function send($method, $url, $options) { try { - return new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions([ + $response = $this->buildClient()->request($method, $url, $this->mergeOptions([ 'query' => $this->parseQueryParams($url), - ], $options))); + 'on_stats' => function (\GuzzleHttp\TransferStats $stats) use (&$transferStats) { + $transferStats = $stats; + } + ], $options)); + return new ZttpResponse($response, $transferStats); } catch (\GuzzleHttp\Exception\ConnectException $e) { throw new ConnectionException($e->getMessage(), 0, $e); } @@ -247,9 +251,10 @@ class ZttpResponse __call as macroCall; } - function __construct($response) + function __construct($response, $transferStats) { $this->response = $response; + $this->transferStats = $transferStats; } function body() @@ -279,6 +284,11 @@ function status() return $this->response->getStatusCode(); } + function effectiveURI() + { + return $this->transferStats->getEffectiveUri(); + } + function isSuccess() { return $this->status() >= 200 && $this->status() < 300; diff --git a/tests/ZttpTest.php b/tests/ZttpTest.php index ada0fd1..c0586e4 100644 --- a/tests/ZttpTest.php +++ b/tests/ZttpTest.php @@ -487,6 +487,14 @@ function can_use_digest_auth() $this->assertTrue($response->isOk()); } + /** @test */ + function can_retrieve_effective_uri() + { + $response = Zttp::get($this->url('/redirect')); + + $this->assertEquals($this->url('/redirected'), $response->effectiveURI()); + } + /** * @test * @expectedException \Zttp\ConnectionException