AWS-Signature-V4

 view release on metacpan or  search on metacpan

t/canonical.t  view on Meta::CPAN

use v5.24;
use utf8;
use experimental 'signatures';
use Test2::V0;
use FindBin '$Bin';
use lib "$Bin/../lib";
use AWS::Signature::V4;
use Digest::SHA qw< sha256_hex >;

my %cred = (access_key_id => 'AKID', secret_access_key => 'SECRET');
my $std = AWS::Signature::V4->new(service => 'service', region => 'us-east-1', credentials => {%cred});
my $s3  = AWS::Signature::V4->new(service => 's3',      region => 'us-east-1', credentials => {%cred});

sub canon ($signer, $url, %rest) {
   my $r = $signer->sign(method => 'GET', url => $url, time => 1440938160, %rest);
   my @lines = split /\n/, $r->{canonical_request}, -1;
   return ($lines[1], $lines[2], $r);    # path, query, whole result
}
sub path_of  ($signer, $url) { (canon($signer, $url))[0] }
sub query_of ($signer, $url) { (canon($signer, $url))[1] }
sub req (@args) { (canon(@args))[2] }

# errors caused by the caller: Ouch exceptions with code 400
sub bad ($re = qr/./) {
   object { prop blessed => 'Ouch'; call code => 400; call message => match $re };
}

subtest 'path, non-S3 (normalized, double-encoded)' => sub {
   my %case = (    # from the AWS SigV4 test suite
      'https://h'                      => '/',
      'https://h/'                     => '/',
      'https://h/example/..'           => '/',
      'https://h/./'                   => '/',
      'https://h/./example'            => '/example',
      'https://h//example//'           => '/example/',
      'https://h/example/./a/../b'     => '/example/b',
      'https://h/../a'                 => '/a',
      'https://h/example/'             => '/example/',
      'https://h/example space/'       => '/example%2520space/',
      'https://h/example%20space/'     => '/example%2520space/',
      'https://h/%E1%88%B4'            => '/%25E1%2588%25B4',
      'https://h/a-._~b'               => '/a-._~b',
      'https://h/a$b'                  => '/a%2524b',
   );
   is path_of($std, $_), $case{$_}, $_ for sort keys %case;
};

subtest 'path, S3 (verbatim, single-encoded)' => sub {
   my %case = (
      'https://h/'                 => '/',
      'https://h/example space/'   => '/example%20space/',
      'https://h/example%20space/' => '/example%20space/',
      'https://h/a/../b'           => '/a/../b',
      'https://h//a//b'            => '//a//b',
      'https://h/%E1%88%B4'        => '/%E1%88%B4',
      'https://h/a$b'              => '/a%24b',
   );
   is path_of($s3, $_), $case{$_}, $_ for sort keys %case;
};

subtest 'query' => sub {
   my %case = (
      'https://h/'                                 => '',
      'https://h/?'                                => '',



( run in 0.765 second using v1.01-cache-2.11-cpan-234fa220139 )