-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: s3 expires customization (#2955)
- Loading branch information
1 parent
3f9cc3f
commit 971e6f3
Showing
8 changed files
with
286 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"type": "feature", | ||
"category": "S3", | ||
"description": "Adds customization to output structures for `Expires` parsing which adds an additional shape `ExpiresString`" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,5 +72,4 @@ public function __invoke(CommandInterface $cmd) { | |
} | ||
return $nextHandler($cmd); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace Aws\S3; | ||
|
||
use Aws\CommandInterface; | ||
use Aws\ResultInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Logs a warning when the `expires` header | ||
* fails to be parsed. | ||
* | ||
* @internal | ||
*/ | ||
class ExpiresParsingMiddleware | ||
{ | ||
/** @var callable */ | ||
private $nextHandler; | ||
|
||
/** | ||
* Create a middleware wrapper function. | ||
* | ||
* @return callable | ||
*/ | ||
public static function wrap() | ||
{ | ||
return function (callable $handler) { | ||
return new self($handler); | ||
}; | ||
} | ||
|
||
/** | ||
* @param callable $nextHandler Next handler to invoke. | ||
*/ | ||
public function __construct(callable $nextHandler) | ||
{ | ||
$this->nextHandler = $nextHandler; | ||
} | ||
|
||
public function __invoke(CommandInterface $command, RequestInterface $request = null) | ||
{ | ||
$next = $this->nextHandler; | ||
return $next($command, $request)->then( | ||
function (ResultInterface $result) { | ||
if (empty($result['Expires']) && !empty($result['ExpiresString'])) { | ||
trigger_error( | ||
"Failed to parse the `expires` header as a timestamp due to " | ||
. " an invalid timestamp format.\nPlease refer to `ExpiresString` " | ||
. "for the unparsed string format of this header.\n" | ||
, E_USER_WARNING | ||
); | ||
} | ||
return $result; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace Aws\Test\S3; | ||
|
||
use Aws\CommandInterface; | ||
use Aws\Result; | ||
use Aws\S3\ExpiresParsingMiddleware; | ||
use Aws\Test\UsesServiceTrait; | ||
use GuzzleHttp\Promise; | ||
use Yoast\PHPUnitPolyfills\TestCases\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* @covers Aws\S3\ExpiresParsingMiddleware | ||
*/ | ||
class ExpiresParsingMiddlewareTest extends TestCase | ||
{ | ||
use UsesServiceTrait; | ||
|
||
public function testEmitsWarningWhenMissingExpires() | ||
{ | ||
$this->expectWarning(); | ||
$this->expectWarningMessage( | ||
"Failed to parse the `expires` header as a timestamp due to " | ||
. " an invalid timestamp format.\nPlease refer to `ExpiresString` " | ||
. "for the unparsed string format of this header.\n" | ||
); | ||
|
||
$command = $this->getMockBuilder(CommandInterface::class)->getMock(); | ||
$request = $this->getMockBuilder(RequestInterface::class)->getMock(); | ||
$nextHandler = function ($cmd, $request) { | ||
return Promise\Create::promiseFor(new Result([ | ||
'ExpiresString' => 'not-a-timestamp' | ||
])); | ||
}; | ||
|
||
$mw = new ExpiresParsingMiddleware($nextHandler); | ||
$mw($command, $request)->wait(); | ||
} | ||
|
||
public function testDoesNotEmitWarningWhenExpiresPresent() | ||
{ | ||
$command = $this->getMockBuilder(CommandInterface::class)->getMock(); | ||
$request = $this->getMockBuilder(RequestInterface::class)->getMock(); | ||
$nextHandler = function ($cmd, $request) { | ||
return Promise\Create::promiseFor(new Result([ | ||
'ExpiresString' => 'test', | ||
'Expires' => 'test' | ||
])); | ||
}; | ||
|
||
$mw = new ExpiresParsingMiddleware($nextHandler); | ||
$result = $mw($command, $request)->wait(); | ||
$this->assertEquals('test', $result['Expires']); | ||
$this->assertEquals('test', $result['ExpiresString']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters