From aaf053d2ce401ae02de56844ec9ca39052606deb Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Mon, 11 Dec 2017 17:18:03 +0100 Subject: [PATCH 1/4] PathPrepend plugin: Only overwrite the request uri if it actually changed --- lib/Github/HttpClient/Plugin/PathPrepend.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Github/HttpClient/Plugin/PathPrepend.php b/lib/Github/HttpClient/Plugin/PathPrepend.php index 567bb156ed7..2c91bf74f3b 100644 --- a/lib/Github/HttpClient/Plugin/PathPrepend.php +++ b/lib/Github/HttpClient/Plugin/PathPrepend.php @@ -30,10 +30,9 @@ public function handleRequest(RequestInterface $request, callable $next, callabl $currentPath = $request->getUri()->getPath(); if (strpos($currentPath, $this->path) !== 0) { $uri = $request->getUri()->withPath($this->path.$currentPath); + $request = $request->withUri($uri); } - $request = $request->withUri($uri); - return $next($request); } } From 65751286b1cb7269c698262128d84ae94cd94169 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Mon, 11 Dec 2017 18:03:58 +0100 Subject: [PATCH 2/4] Add test for pathprepend http plugin --- .../Tests/HttpClient/PathPrependTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/Github/Tests/HttpClient/PathPrependTest.php diff --git a/test/Github/Tests/HttpClient/PathPrependTest.php b/test/Github/Tests/HttpClient/PathPrependTest.php new file mode 100644 index 00000000000..82868b6ade4 --- /dev/null +++ b/test/Github/Tests/HttpClient/PathPrependTest.php @@ -0,0 +1,38 @@ + + */ +class PathPrependTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider uris + */ + public function testPathIsPrepended($uri, $expectedPath) + { + $request = new Request('GET', $uri); + $plugin = new PathPrepend('/api/v3'); + + $newRequest = null; + $plugin->handleRequest($request, function ($request) use (&$newRequest) { + $newRequest = $request; + }, function () { + throw new \RuntimeException("Did not expect plugin to call first"); + }); + + $this->assertEquals($expectedPath, $newRequest->getUri()->getPath()); + } + + public static function uris() + { + return [ + ['http://example.com/foo/bar/api', '/api/v3/foo/bar/api'], + ['http://example.com/api/v3/foo/bar/api', '/api/v3/foo/bar/api'], + ]; + } +} From c37bf230d288ed59a1fa1999519f421337b1cb25 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Mon, 11 Dec 2017 18:05:21 +0100 Subject: [PATCH 3/4] Formatting --- test/Github/Tests/HttpClient/PathPrependTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Github/Tests/HttpClient/PathPrependTest.php b/test/Github/Tests/HttpClient/PathPrependTest.php index 82868b6ade4..cdf9c53782c 100644 --- a/test/Github/Tests/HttpClient/PathPrependTest.php +++ b/test/Github/Tests/HttpClient/PathPrependTest.php @@ -20,8 +20,8 @@ public function testPathIsPrepended($uri, $expectedPath) $newRequest = null; $plugin->handleRequest($request, function ($request) use (&$newRequest) { - $newRequest = $request; - }, function () { + $newRequest = $request; + }, function () { throw new \RuntimeException("Did not expect plugin to call first"); }); From 6e146018c40e47f05e765ad13373047d1f7d7f75 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 12 Dec 2017 20:06:52 +0100 Subject: [PATCH 4/4] Updated to phpunit6 --- test/Github/Tests/HttpClient/PathPrependTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Github/Tests/HttpClient/PathPrependTest.php b/test/Github/Tests/HttpClient/PathPrependTest.php index cdf9c53782c..dbd7905c739 100644 --- a/test/Github/Tests/HttpClient/PathPrependTest.php +++ b/test/Github/Tests/HttpClient/PathPrependTest.php @@ -4,11 +4,12 @@ use Github\HttpClient\Plugin\PathPrepend; use GuzzleHttp\Psr7\Request; +use PHPUnit\Framework\TestCase; /** * @author Nils Adermann */ -class PathPrependTest extends \PHPUnit_Framework_TestCase +class PathPrependTest extends TestCase { /** * @dataProvider uris