Skip to content

Commit 274adf2

Browse files
authored
Add decorator tests for guzzle and symfony (#256)
Signed-off-by: Kim Pepper <[email protected]>
1 parent 4ff7088 commit 274adf2

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace OpenSearch\Tests\Aws;
4+
5+
use Aws\Credentials\CredentialsInterface;
6+
use Aws\Signature\SignatureV4;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\HttpFactory;
10+
use GuzzleHttp\Psr7\Response;
11+
use OpenSearch\Aws\SigningClientDecorator;
12+
use OpenSearch\Client;
13+
use OpenSearch\EndpointFactory;
14+
use OpenSearch\RequestFactory;
15+
use OpenSearch\Serializers\SmartSerializer;
16+
use OpenSearch\TransportFactory;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Tests the signing client decorator.
21+
*/
22+
class SigningClientDecoratorGuzzleTest extends TestCase
23+
{
24+
public function testGuzzleRequestIsSigned(): void
25+
{
26+
// Mock out the Guzzle handler so we can test the client without making a real HTTP request.
27+
$mockHandler = new MockHandler([
28+
new Response(200, [], '{"foo":"bar"}'),
29+
]);
30+
31+
$handlerStack = HandlerStack::create($mockHandler);
32+
33+
$guzzleClient = new \GuzzleHttp\Client([
34+
'base_uri' => 'http://localhost:9200',
35+
'headers' => [
36+
'Accept' => 'application/json',
37+
'Content-Type' => 'application/json',
38+
],
39+
'handler' => $handlerStack,
40+
]);
41+
42+
$guzzleHttpFactory = new HttpFactory();
43+
44+
$serializer = new SmartSerializer();
45+
46+
$requestFactory = new RequestFactory(
47+
$guzzleHttpFactory,
48+
$guzzleHttpFactory,
49+
$guzzleHttpFactory,
50+
$serializer,
51+
);
52+
$credentials = $this->createMock(CredentialsInterface::class);
53+
$signer = new SignatureV4('es', 'us-east-1');
54+
55+
$decorator = new SigningClientDecorator($guzzleClient, $credentials, $signer);
56+
57+
$transport = (new TransportFactory())
58+
->setHttpClient($decorator)
59+
->setRequestFactory($requestFactory)
60+
->create();
61+
62+
$endpointFactory = new EndpointFactory();
63+
$client = new Client($transport, $endpointFactory, []);
64+
65+
$client->request('GET', '/_cat/indices');
66+
67+
// Check the last request to ensure it was signed and has a host header.
68+
$lastRequest = $mockHandler->getLastRequest();
69+
$this->assertEquals('localhost:9200', $lastRequest->getHeader('Host')[0]);
70+
$this->assertNotEmpty($lastRequest->getHeader('x-amz-content-sha256'));
71+
$this->assertNotEmpty($lastRequest->getHeader('x-amz-date'));
72+
$this->assertNotEmpty($lastRequest->getHeader('Authorization'));
73+
}
74+
75+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenSearch\Tests\Aws;
6+
7+
use Aws\Credentials\CredentialsInterface;
8+
use Aws\Signature\SignatureV4;
9+
use OpenSearch\Aws\SigningClientDecorator;
10+
use OpenSearch\Client;
11+
use OpenSearch\EndpointFactory;
12+
use OpenSearch\RequestFactory;
13+
use OpenSearch\Serializers\SmartSerializer;
14+
use OpenSearch\TransportFactory;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Psr18Client;
18+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
19+
20+
/**
21+
* Tests the signing client decorator with the Symfony HTTP client.
22+
*/
23+
class SigningClientDecoratorSymfonyTest extends TestCase
24+
{
25+
public function testSymfonyRequestIsSigned(): void
26+
{
27+
$response = new JsonMockResponse([
28+
'foo' => 'bar',
29+
]);
30+
31+
$mockHttpClient = (new MockHttpClient([$response]))->withOptions([
32+
'base_uri' => 'http://localhost:9200',
33+
'headers' => [
34+
'Accept' => 'application/json',
35+
'Content-Type' => 'application/json',
36+
'Host' => 'localhost:9200',
37+
],
38+
]);
39+
40+
$symfonyPsr18Client = (new Psr18Client($mockHttpClient));
41+
42+
$serializer = new SmartSerializer();
43+
44+
$requestFactory = new RequestFactory(
45+
$symfonyPsr18Client,
46+
$symfonyPsr18Client,
47+
$symfonyPsr18Client,
48+
$serializer,
49+
);
50+
51+
$credentials = $this->createMock(CredentialsInterface::class);
52+
$signer = new SignatureV4('es', 'us-east-1');
53+
54+
$decorator = new SigningClientDecorator($symfonyPsr18Client, $credentials, $signer);
55+
56+
$transport = (new TransportFactory())
57+
->setHttpClient($decorator)
58+
->setRequestFactory($requestFactory)
59+
->create();
60+
61+
$endpointFactory = new EndpointFactory();
62+
63+
$client = new Client($transport, $endpointFactory, []);
64+
65+
// Send a request to the 'info' endpoint.
66+
$client->request('GET', '/');
67+
68+
$this->assertNotEmpty($response->getRequestUrl());
69+
$requestHeaders = $response->getRequestOptions()['normalized_headers'];
70+
$this->assertArrayHasKey('authorization', $requestHeaders);
71+
$this->assertArrayHasKey('x-amz-content-sha256', $requestHeaders);
72+
$this->assertArrayHasKey('x-amz-date', $requestHeaders);
73+
$this->assertArrayHasKey('host', $requestHeaders);
74+
75+
}
76+
77+
78+
}

tests/Aws/SigningClientDecoratorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function testSendRequest()
4444
)
4545
->willReturn($this->createMock(ResponseInterface::class));
4646

47-
4847
$decorator = new SigningClientDecorator($client, $credentials, $signer);
4948
$request = new Request('GET', 'http://localhost:9200/_search');
5049
$decorator->sendRequest($request);

0 commit comments

Comments
 (0)