AWS-S3
view release on metacpan or search on metacpan
t/aws/s3/request/headers.t view on Meta::CPAN
#!perl
use strict;
use warnings;
# Requests are signed with SigV4 only. There used to be a SigV2 signer whose
# result was computed for every request and thrown away, it supplied the Date
# and Content-MD5 headers, check they are still sent and correct, and that the
# old module is gone.
package Capturing::UA;
use parent 'LWP::UserAgent';
our @SEEN;
sub request {
my ( $self, $req ) = @_;
push @SEEN, $req->clone;
return HTTP::Response->new( 200, 'OK', [ ETag => '"abc"' ] );
}
package main;
use Test::More;
use Digest::MD5 qw( md5_base64 );
use Digest::SHA qw( sha256_hex );
use_ok( 'AWS::S3' );
use_ok( 'AWS::S3::Bucket' );
use_ok( 'AWS::S3::File' );
use_ok( 'AWS::S3::HTTPRequest' );
my $s3 = AWS::S3->new(
access_key_id => 'foo',
secret_access_key => 'bar',
session_token => 'TOKEN',
endpoint => 's3.baz.com',
ua => Capturing::UA->new,
);
my $bucket = AWS::S3::Bucket->new( s3 => $s3, name => 'maibucket' );
sub _md5 {
# md5_base64 has no padding, Content-MD5 does
my $md5 = md5_base64( $_[0] );
return $md5 . '=' x ( ( 4 - length( $md5 ) % 4 ) % 4 );
}
note( "the old signer is gone" );
{
ok( !$INC{'AWS/S3/Signer.pm'}, 'AWS::S3::Signer was not loaded' );
}
note( "every request is v4 signed and dated" );
{
my %requests = (
ListAllMyBuckets => { },
CreateBucket => { bucket => 'maibucket' },
DeleteBucket => { bucket => 'maibucket' },
DeleteFile => { bucket => 'maibucket', key => 'k' },
GetFileContents => { bucket => 'maibucket', key => 'k' },
GetFileInfo => { bucket => 'maibucket', key => 'k' },
GetBucketPolicy => { bucket => 'maibucket' },
GetBucketAccessControl => { bucket => 'maibucket' },
GetBucketLocationConstraint => { bucket => 'maibucket' },
ListBucket => { bucket => 'maibucket', max_keys => 1 },
SetBucketPolicy => { bucket => 'maibucket', policy => '{}' },
SetBucketAccessControl => { bucket => 'maibucket', acl_short => 'private' },
DeleteMulti => { bucket => 'maibucket', keys => ['k'] },
);
foreach my $type ( sort keys %requests ) {
@Capturing::UA::SEEN = ();
$s3->request( $type, %{ $requests{$type} } )->request;
is( scalar @Capturing::UA::SEEN, 1, "$type sent one request" );
my $req = $Capturing::UA::SEEN[0];
like( $req->header( 'Authorization' ), qr/^AWS4-HMAC-SHA256 Credential=foo\//, '... v4 signature' );
unlike( $req->header( 'Authorization' ), qr/^AWS foo:/, '... and no v2 signature' );
like( $req->header( 'Date' ), qr/^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/, '... has a Date' );
is( $req->header( 'X-Amz-Security-Token' ), 'TOKEN', '... and the session token' );
}
}
note( "Content-MD5 of a multi object delete" );
{
@Capturing::UA::SEEN = ();
$s3->request( 'DeleteMulti', bucket => 'maibucket', keys => [ 'a', 'b' ] )->request;
my $req = $Capturing::UA::SEEN[0];
is( $req->method, 'POST', 'POST' );
is( $req->uri->query, 'delete', '... of the delete subresource' );
is( $req->header( 'Content-MD5' ), _md5( $req->content ), '... md5 of the body that was sent' );
}
note( "uploading a file" );
( run in 0.887 second using v1.01-cache-2.11-cpan-eb134014566 )