Skip to content

Commit

Permalink
Updated the CloudSearchDomain client to support request signing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremeamia committed Aug 14, 2014
1 parent aeeb3d3 commit f1e4362
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/Aws/CloudSearchDomain/CloudSearchDomainClientBuilder.php
Expand Up @@ -29,8 +29,7 @@
class CloudSearchDomainClientBuilder extends ClientBuilder
{
protected static $commonConfigDefaults = array(
'scheme' => 'https',
'signature.ignore' => true
Options::SCHEME => 'https',
);

public function build()
Expand All @@ -52,14 +51,19 @@ public function build()
)));
}

// Determine the region from the endpoint
$endpoint = Url::factory($config->get(Options::BASE_URL));
list(,$region) = explode('.', $endpoint->getHost());
$config[Options::REGION] = $config[Options::SIGNATURE_REGION] = $region;

// Create dependencies
$exceptionParser = new JsonQueryExceptionParser();
$signature = new SignatureV4();
$credentials = new Credentials('','');
$description = ServiceDescription::factory(sprintf(
$config->get(Options::SERVICE_DESCRIPTION),
$config->get(Options::VERSION)
));
$signature = $this->getSignature($description, $config);
$credentials = $this->getCredentials($config);

// Resolve backoff strategy
$backoff = $config->get(Options::BACKOFF);
Expand Down
Expand Up @@ -5,6 +5,8 @@
'endpointPrefix' => 'cloudsearchdomain',
'serviceFullName' => 'Amazon CloudSearchDomain',
'serviceType' => 'rest-json',
'signatureVersion' => 'v4',
'signingName' => 'cloudsearch',
'namespace' => 'CloudSearchDomain',
'operations' => array(
'Search' => array(
Expand Down
48 changes: 41 additions & 7 deletions tests/Aws/Tests/CloudSearchDomain/CloudSearchDomainClientTest.php
Expand Up @@ -3,8 +3,11 @@
namespace Aws\Tests\CloudSearchDomain;

use Aws\CloudSearchDomain\CloudSearchDomainClient;
use Aws\Common\Enum\ClientOptions as OPT;
use Aws\Credentials\Credentials;
use Aws\Common\Enum\ClientOptions as Opt;
use Aws\Common\Credentials\Credentials;
use Guzzle\Common\Event;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Mock\MockPlugin;

/**
* @covers Aws\CloudSearchDomain\CloudSearchDomainClient
Expand All @@ -15,8 +18,8 @@ class CloudSearchDomainClientTest extends \Guzzle\Tests\GuzzleTestCase
public function testFactoryInitializesClient()
{
$client = CloudSearchDomainClient::factory(array(
OPT::BASE_URL => 'foo.us-east-1.cloudsearch.amazonaws.com',
OPT::VALIDATION => false,
Opt::BASE_URL => 'foo.us-east-1.cloudsearch.amazonaws.com',
Opt::VALIDATION => false,
));

$this->assertEquals('https://foo.us-east-1.cloudsearch.amazonaws.com', $client->getBaseUrl());
Expand All @@ -30,14 +33,45 @@ public function testFailsWithoutBaseUrl()
CloudSearchDomainClient::factory();
}

public function testThrowsExceptionWhenAttemptingToMutateCredentialsOrRegion()
public function testThrowsExceptionWhenAttemptingToMutateRegion()
{
$client = CloudSearchDomainClient::factory(array('base_url' => 'example.com'));

$this->setExpectedException('BadMethodCallException');
$client->setRegion('us-west-2');
}

public function testThrowsExceptionWhenAttemptingToMutateCredentials()
{
$client = CloudSearchDomainClient::factory(array('base_url' => 'example.com'));
$this->setExpectedException('BadMethodCallException');
$client->setCredentaisl(new Credentials('foo', 'bar'));
$client->setCredentials(new Credentials('foo', 'bar'));
}

public function testSignsRequests()
{
$mock = new MockPlugin(array(new Response(200), new Response(200)));
$config = array(
Opt::BASE_URL => 'foo.us-east-1.cloudsearch.amazonaws.com',
Opt::VALIDATION => false,
Opt::CREDENTIALS => new Credentials('foo', 'bar'),
);
$getAuthorizationHeader = function(Event $event) use (&$auth) {
$auth = (string) $event['request']->getHeader('Authorization');
};

$client1 = CloudSearchDomainClient::factory($config);
$client1->addSubscriber($mock);
$client1->getEventDispatcher()->addListener('request.before_send', $getAuthorizationHeader, -999);
$client1->search();

$this->assertNotEmpty($auth);

$config[Opt::CREDENTIALS] = false;
$client2 = CloudSearchDomainClient::factory($config);
$client2->addSubscriber($mock);
$client2->getEventDispatcher()->addListener('request.before_send', $getAuthorizationHeader, -999);
$client2->search();

$this->assertEmpty($auth);
}
}

0 comments on commit f1e4362

Please sign in to comment.