Skip to content

Commit 3667cee

Browse files
nadermanNyholm
authored andcommitted
PathPrepend plugin: Only overwrite the request uri if it actually changed (#663)
* PathPrepend plugin: Only overwrite the request uri if it actually changed * Add test for pathprepend http plugin * Formatting * Updated to phpunit6
1 parent 633e004 commit 3667cee

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/Github/HttpClient/Plugin/PathPrepend.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
3030
$currentPath = $request->getUri()->getPath();
3131
if (strpos($currentPath, $this->path) !== 0) {
3232
$uri = $request->getUri()->withPath($this->path.$currentPath);
33+
$request = $request->withUri($uri);
3334
}
3435

35-
$request = $request->withUri($uri);
36-
3736
return $next($request);
3837
}
3938
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Github\Tests\HttpClient\Plugin;
4+
5+
use Github\HttpClient\Plugin\PathPrepend;
6+
use GuzzleHttp\Psr7\Request;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @author Nils Adermann <naderman@naderman.de>
11+
*/
12+
class PathPrependTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider uris
16+
*/
17+
public function testPathIsPrepended($uri, $expectedPath)
18+
{
19+
$request = new Request('GET', $uri);
20+
$plugin = new PathPrepend('/api/v3');
21+
22+
$newRequest = null;
23+
$plugin->handleRequest($request, function ($request) use (&$newRequest) {
24+
$newRequest = $request;
25+
}, function () {
26+
throw new \RuntimeException("Did not expect plugin to call first");
27+
});
28+
29+
$this->assertEquals($expectedPath, $newRequest->getUri()->getPath());
30+
}
31+
32+
public static function uris()
33+
{
34+
return [
35+
['http://example.com/foo/bar/api', '/api/v3/foo/bar/api'],
36+
['http://example.com/api/v3/foo/bar/api', '/api/v3/foo/bar/api'],
37+
];
38+
}
39+
}

0 commit comments

Comments
 (0)