Skip to content

Commit

Permalink
Fixed the S3 link signing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremeamia committed Dec 12, 2013
1 parent 7fe67d4 commit 777acdc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ before_script:
- sh -c 'if [ $(php -r "echo PHP_MINOR_VERSION;") -le 4 ]; then echo "extension = apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
- cp phpunit.xml.dist phpunit.xml
- composer install --dev
script: vendor/bin/phpunit
script: vendor/bin/phpunit --coverage-text
6 changes: 3 additions & 3 deletions tests/Aws/Tests/View/Helper/CloudFrontLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testAssertDoesUseSslByDefault()
}

/**
* @expectedException Aws\View\Exception\InvalidSchemeException
* @expectedException \Aws\View\Exception\InvalidSchemeException
*/
public function testAssertInvalidSchemesThrowExceptions()
{
Expand All @@ -63,7 +63,7 @@ public function testGenerateSimpleLink()

public function testGenerateSimpleNonSslLink()
{
$this->viewHelper->setUseSsl(false);
$this->viewHelper->setScheme('http');

$link = $this->viewHelper->__invoke('my-object', 'my-domain');
$this->assertEquals('http://my-domain.cloudfront.net/my-object', $link);
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testGenerateSignedLink()
}

/**
* @expectedException Aws\View\Exception\InvalidSchemeException
* @expectedException \Aws\View\Exception\InvalidSchemeException
*/
public function testGenerateSignedProtocolRelativeLink()
{
Expand Down
79 changes: 20 additions & 59 deletions tests/Aws/Tests/View/Helper/S3LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function setUp()

public function testAssertUseSslByDefault()
{
$this->assertTrue($this->viewHelper->getUseSsl());
$this->assertEquals('https', $this->viewHelper->getScheme());
}

/**
* @expectedException Aws\View\Exception\InvalidSchemeException
* @expectedException \Aws\View\Exception\InvalidSchemeException
*/
public function testAssertInvalidSchemesThrowExceptions()
{
Expand Down Expand Up @@ -115,79 +115,40 @@ public function testFailsWhenNoBucketSpecified()
$link = $this->viewHelper->__invoke('my-object');
}

public function testGenerateSignedLink()
{
$timeTest = time() + 10;

$link = $this->viewHelper->__invoke('my-object', 'my-bucket', $timeTest);

$request = $this->s3Client->get($this->viewHelper->__invoke('my-object', 'my-bucket'));

$signature = $this->s3Client->getSignature();
$signature = $signature->signString(
$signature->createCanonicalizedString($request, $timeTest),
$this->s3Client->getCredentials()
);

$expectedResult = sprintf(
'https://my-bucket.s3.amazonaws.com/my-object?AWSAccessKeyId=%s&Expires=%s&Signature=%s',
$this->s3Client->getCredentials()->getAccessKeyId(),
$timeTest,
urlencode($signature)
);

$this->assertEquals($expectedResult, $link);
}

public function testGenerateSignedNotSslLink()
/**
* @dataProvider dataForLinkSigningTest
*/
public function testGenerateSignedLink($scheme)
{
$this->viewHelper->setUseSsl(false);

$timeTest = time() + 10;
$this->viewHelper->setScheme($scheme);
$expires = time() + 10;

$link = $this->viewHelper->__invoke('my-object', 'my-bucket', $timeTest);
$actualResult = $this->viewHelper->__invoke('my-object', 'my-bucket', $expires);

// Build expected signature
$request = $this->s3Client->get($this->viewHelper->__invoke('my-object', 'my-bucket'));

$request->getParams()->set('s3.resource', '/my-bucket/my-object');
$signature = $this->s3Client->getSignature();
$signature = $signature->signString(
$signature->createCanonicalizedString($request, $timeTest),
$signature->createCanonicalizedString($request, $expires),
$this->s3Client->getCredentials()
);

$expectedResult = sprintf(
'http://my-bucket.s3.amazonaws.com/my-object?AWSAccessKeyId=%s&Expires=%s&Signature=%s',
ltrim("{$scheme}://my-bucket.s3.amazonaws.com/my-object?AWSAccessKeyId=%s&Expires=%s&Signature=%s", ':'),
$this->s3Client->getCredentials()->getAccessKeyId(),
$timeTest,
$expires,
urlencode($signature)
);

$this->assertEquals($expectedResult, $link);
$this->assertEquals($expectedResult, $actualResult);
}

public function testGenerateSignedProtocolRelativeLink()
public function dataForLinkSigningTest()
{
$this->viewHelper->setScheme(null);

$timeTest = time() + 10;

$link = $this->viewHelper->__invoke('my-object', 'my-bucket', $timeTest);

$request = $this->s3Client->get($this->viewHelper->__invoke('my-object', 'my-bucket'));

$signature = $this->s3Client->getSignature();
$signature = $signature->signString(
$signature->createCanonicalizedString($request, $timeTest),
$this->s3Client->getCredentials()
return array(
array('https'),
array('http'),
array(NULL),
);

$expectedResult = sprintf(
'//my-bucket.s3.amazonaws.com/my-object?AWSAccessKeyId=%s&Expires=%s&Signature=%s',
$this->s3Client->getCredentials()->getAccessKeyId(),
$timeTest,
urlencode($signature)
);

$this->assertEquals($expectedResult, $link);
}
}

0 comments on commit 777acdc

Please sign in to comment.