Skip to content

Commit cf9515f

Browse files
authored
Merge pull request #28 from packagist/t/http-plug-1-and-2
HttpPlug: support version 1 and 2
2 parents 171b99a + 40f4bdb commit cf9515f

File tree

7 files changed

+62
-47
lines changed

7 files changed

+62
-47
lines changed

Diff for: composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
"require": {
1414
"php": "^5.6 || ^7.0",
1515
"psr/http-message": "^1.0",
16-
"php-http/httplug": "^1.0",
16+
"php-http/httplug": "^1.1 || ^2.0",
1717
"php-http/discovery": "^1.0",
1818
"php-http/client-implementation": "^1.0",
19-
"php-http/client-common": "^1.3"
19+
"php-http/client-common": "^1.9 || ^2.0"
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^5.5 || ^6.0",
23-
"php-http/guzzle6-adapter": "^1.0",
23+
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
2424
"php-http/mock-client": "^1.0",
2525
"guzzlehttp/psr7": "^1.2",
2626
"cache/array-adapter": "^0.4",

Diff for: src/Exception/HttpTransportException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ public function getRequestUri()
2828
{
2929
return $this->requestUri;
3030
}
31-
}
31+
}

Diff for: src/HttpClient/Plugin/ExceptionThrower.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
use PrivatePackagist\ApiClient\Exception\ErrorException;
1414
use PrivatePackagist\ApiClient\Exception\HttpTransportException;
1515
use PrivatePackagist\ApiClient\Exception\ResourceNotFoundException;
16-
use PrivatePackagist\ApiClient\Exception\RuntimeException;
1716
use PrivatePackagist\ApiClient\HttpClient\Message\ResponseMediator;
1817
use Psr\Http\Message\RequestInterface;
1918
use Psr\Http\Message\ResponseInterface;
2019

2120
class ExceptionThrower implements Plugin
2221
{
22+
use Plugin\VersionBridgePlugin;
23+
2324
/** @var ResponseMediator */
2425
private $responseMediator;
2526

@@ -28,7 +29,7 @@ public function __construct(ResponseMediator $responseMediator = null)
2829
$this->responseMediator = $responseMediator ? $responseMediator : new ResponseMediator();
2930
}
3031

31-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
32+
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
3233
{
3334
return $next($request)->then(function (ResponseInterface $response) use ($request) {
3435
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {

Diff for: src/HttpClient/Plugin/PathPrepend.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class PathPrepend implements Plugin
1616
{
17+
use Plugin\VersionBridgePlugin;
18+
1719
/** @var string */
1820
private $path;
1921

@@ -28,7 +30,7 @@ public function __construct($path)
2830
/**
2931
* {@inheritdoc}
3032
*/
31-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
33+
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
3234
{
3335
$uri = $request->getUri();
3436

Diff for: src/HttpClient/Plugin/RequestSignature.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class RequestSignature implements Plugin
1616
{
17+
use Plugin\VersionBridgePlugin;
18+
1719
/** @var string */
1820
private $token;
1921
/** @var string */
@@ -32,7 +34,7 @@ public function __construct($token, $secret)
3234
/**
3335
* {@inheritdoc}
3436
*/
35-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
37+
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
3638
{
3739
$params = [
3840
'key' => $this->token,

Diff for: tests/HttpClient/Plugin/PathPrependTest.php

+34-30
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,47 @@
1010
namespace PrivatePackagist\ApiClient\HttpClient\Plugin;
1111

1212
use GuzzleHttp\Psr7\Request;
13+
use Http\Promise\FulfilledPromise;
1314
use PHPUnit\Framework\TestCase;
1415

1516
class PathPrependTest extends TestCase
1617
{
17-
public function testPrefixRequestPath()
18+
/** @var PathPrepend */
19+
private $plugin;
20+
private $next;
21+
private $first;
22+
23+
protected function setUp()
1824
{
19-
$request = new Request('GET', '/packages/');
20-
$expected = new Request('GET', '/api/packages/');
21-
$plugin = new PathPrepend('/api');
22-
$callback = $this->getMockBuilder(\stdClass::class)
23-
->setMethods(['next'])
24-
->getMock()
25-
;
26-
$callback->expects($this->once())
27-
->method('next')
28-
->with($expected)
29-
;
30-
31-
$plugin->handleRequest($request, [$callback, 'next'], function () {
32-
});
25+
parent::setUp();
26+
27+
$this->plugin = new PathPrepend('/api');
28+
$this->next = function (Request $request) {
29+
return new FulfilledPromise($request);
30+
};
31+
$this->first = function () {
32+
throw new \RuntimeException('Did not expect plugin to call first');
33+
};
34+
}
35+
36+
/**
37+
* @dataProvider pathProvider
38+
*/
39+
public function testPrefixApiRequestPath($path, $expectedPath)
40+
{
41+
$request = new Request('GET', $path);
42+
$expected = new Request('GET', $expectedPath);
43+
44+
$promise = $this->plugin->handleRequest($request, $this->next, $this->first);
45+
46+
$this->assertEquals($expected, $promise->wait(true));
3347
}
3448

35-
public function testDontPrefixApiRequestPath()
49+
public function pathProvider()
3650
{
37-
$request = new Request('GET', '/api/packages/');
38-
$expected = new Request('GET', '/api/packages/');
39-
$plugin = new PathPrepend('/api');
40-
$callback = $this->getMockBuilder(\stdClass::class)
41-
->setMethods(['next'])
42-
->getMock()
43-
;
44-
$callback->expects($this->once())
45-
->method('next')
46-
->with($expected)
47-
;
48-
49-
$plugin->handleRequest($request, [$callback, 'next'], function () {
50-
});
51+
return [
52+
['/api/packages/', '/api/packages/'],
53+
['/packages/', '/api/packages/'],
54+
];
5155
}
5256
}

Diff for: tests/HttpClient/Plugin/RequestSignatureTest.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
namespace PrivatePackagist\ApiClient\HttpClient\Plugin;
1111

1212
use GuzzleHttp\Psr7\Request;
13+
use Http\Promise\FulfilledPromise;
1314
use PHPUnit\Framework\TestCase;
1415

1516
class RequestSignatureTest extends TestCase
1617
{
1718
/** @var RequestSignature */
1819
private $plugin;
20+
private $next;
21+
private $first;
1922
private $token;
2023
private $secret;
2124
private $timestamp;
@@ -29,6 +32,12 @@ protected function setUp()
2932
$this->nonce = '78b9869e96cf58b5902154e0228f8576f042e5ac';
3033
$this->plugin = new RequestSignatureMock($this->token, $this->secret);
3134
$this->plugin->init($this->timestamp, $this->nonce);
35+
$this->next = function (Request $request) {
36+
return new FulfilledPromise($request);
37+
};
38+
$this->first = function () {
39+
throw new \RuntimeException('Did not expect plugin to call first');
40+
};
3241
}
3342

3443
public function testPrefixRequestPath()
@@ -43,20 +52,17 @@ public function testPrefixRequestPath()
4352
json_encode(['foo' => 'bar'])
4453
);
4554

46-
$this->plugin->handleRequest($request, function (Request $actual) use ($expected) {
47-
$this->assertEquals($expected->getHeaders(), $actual->getHeaders());
48-
}, function () {
49-
});
55+
$promise = $this->plugin->handleRequest($request, $this->next, $this->first);
56+
57+
$this->assertEquals($expected->getHeaders(), $promise->wait(true)->getHeaders());
5058
}
5159

5260
public function testPrefixRequestPathSmoke()
5361
{
5462
$request = new Request('POST', '/packages/?foo=bar', [], json_encode(['foo' => 'bar']));
5563

56-
$plugin = new RequestSignature($this->token, $this->secret);
57-
$plugin->handleRequest($request, function (Request $actual) {
58-
$this->assertNotNull($actual->getHeader('Authorization')[0]);
59-
}, function () {
60-
});
64+
$promise = $this->plugin->handleRequest($request, $this->next, $this->first);
65+
66+
$this->assertNotNull($promise->wait(true)->getHeader('Authorization')[0]);
6167
}
6268
}

0 commit comments

Comments
 (0)