Amazon-S3-Thin

 view release on metacpan or  search on metacpan

t/02_signer_v2.t  view on Meta::CPAN

use strict;
use warnings;
use Amazon::S3::Thin::Credentials;
use Amazon::S3::Thin::Signer::V2;
use Test::More;
use HTTP::Headers;
use HTTP::Request;

# What this test does is only to calculate signature,
# no HTTP communication.
{
    diag "test PUT request";
    my $secret = "secretfoobar";
    my $verb = "PUT";
    my $path = "example/file.txt";

    my $hdr = HTTP::Headers->new;
    $hdr->header("content-length", 15);
    $hdr->header("date", 'Sun, 01 Mar 2015 15:11:25 GMT');
    my $credentials = Amazon::S3::Thin::Credentials->new('', $secret);
    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $sig = $signer->calculate_signature($verb,$path,$hdr);

    is $sig, "/WcvruHFtEoxcEMmdsfLJ6iZClw=";
}

{
    diag "test GET request with single subresource";
    my $secret = "somesecret";
    my $verb = "GET";
    my $path = "example/?delete";
    my $date = 'Sun, 01 Mar 2015 15:11:25 GMT';
    my $string_to_sign = "$verb\n\n\n$date\n/$path";

    my $credentials = Amazon::S3::Thin::Credentials->new('', $secret);
    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $hdr = HTTP::Headers->new;
    $hdr->header("date", $date);

    is(
        $signer->string_to_sign($verb,$path,$hdr),
        $string_to_sign,
        'string to sign'
    );
    my $sig = $signer->calculate_signature($verb, $path, $hdr);
    is $sig, 'IM6VtFJwF3z+lulFGux8tlU4N8Q=', "get with subresource";
}

{
    diag "test GET request with single subresource";
    my $secret = "somesecret";
    my $verb = "GET";
    my $path = 'example/?delete&versionId=4&invalid&acl&location="foo"';
    my $date = 'Sun, 01 Mar 2015 15:11:25 GMT';
    my $string_to_sign = "$verb\n\n\n$date\n/example/?acl&delete&location&versionId";

    my $credentials = Amazon::S3::Thin::Credentials->new('', $secret);
    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $hdr = HTTP::Headers->new;
    $hdr->header("date", $date);

    is(
        $signer->string_to_sign($verb,$path,$hdr),
        $string_to_sign,
        'string to sign'
    );
    my $sig = $signer->calculate_signature($verb, $path, $hdr);

t/02_signer_v2.t  view on Meta::CPAN

    $hdr->header("X-Amz-Meta-FileChecksum", $x_amz_meta_filechecksum);
    $hdr->header("X-Amz-Meta-ChecksumAlgorithm", $x_amz_meta_checksum_algorithm);
    $hdr->header("Content-Disposition", $content_disposition);
    $hdr->header("Content-Encoding", $content_encoding);
    $hdr->header("Content-Length", $content_length);

    is(
        $signer->string_to_sign($verb,$path,$hdr),
        $string_to_sign,
        'string to sign'
    );
    my $sig = $signer->calculate_signature($verb, $path, $hdr);
    is $sig, 'ilyl83RwaSoYIEdixDQcA4OnAnc=', "puppy upload (PUT)";
}

{
    diag "test Amazon example list buckets";

    my $verb           = "GET";
    my $date           = "Wed, 28 Mar 2007 01:29:59 +0000";
    my $path           = "";
    my $string_to_sign = "$verb\n\n\n$date\n/$path";

    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $hdr = HTTP::Headers->new;
    $hdr->header("Date", $date);

    is(
        $signer->string_to_sign($verb,$path,$hdr),
        $string_to_sign,
        'string to sign'
    );
    my $sig = $signer->calculate_signature($verb, $path, $hdr);
    is $sig, 'qGdzdERIC03wnaRNKh6OqZehG9s=', "puppy list buckets (GET)";
}

{
    diag "test Amazon example unicode keys";

    my $verb           = "GET";
    my $date           = "Wed, 28 Mar 2007 01:49:49 +0000";
    my $path           = "dictionary/fran%C3%A7ais/pr%c3%a9f%c3%a8re";
    my $string_to_sign = "$verb\n\n\n$date\n/$path";

    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $hdr = HTTP::Headers->new;
    $hdr->header("Date", $date);

    is(
        $signer->string_to_sign($verb,$path,$hdr),
        $string_to_sign,
        'string to sign'
    );
    my $sig = $signer->calculate_signature($verb, $path, $hdr);
    is $sig, 'DNEZGsoieTZ92F3bUfSPQcbGmlM=', "puppy unicode keys";
}

{
  diag "test sign";

  my $request = HTTP::Request->new(GET => 'https://mybucket.s3.amazonaws.com/myfile.txt');
  $request->header('Date' => 'Wed, 28 Mar 2007 01:49:49 +0000');
  my $credentials = Amazon::S3::Thin::Credentials->new('accesskey', 'secretkey');
  my $signer = Amazon::S3::Thin::Signer::V2->new($credentials, 's3.amazonaws.com');
  $signer->sign($request);
  is_deeply ($request->headers, {
      authorization => 'AWS accesskey:Up4jVMLZzEbhnf+Thj0XJ68JREs=',
      date => 'Wed, 28 Mar 2007 01:49:49 +0000',
    }, 'Request headers');
}

{
    diag "test sign (session token)";

    my $request = HTTP::Request->new(GET => 'https://mybucket.s3.amazonaws.com/myfile.txt');
    $request->header('Date' => 'Wed, 28 Mar 2007 01:49:49 +0000');
    my $credentials = Amazon::S3::Thin::Credentials->new('accesskey', 'secretkey', 'sessiontoken');
    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials, 's3.amazonaws.com');
    $signer->sign($request);
    my $headers = $request->headers;
    delete $headers->{'::std_case'};
    is_deeply ($headers, {
        authorization => 'AWS accesskey:jALzlsXtPsSS7qFbE7l2f7Dpx5Y=',
        date => 'Wed, 28 Mar 2007 01:49:49 +0000',
        'x-amz-security-token' => 'sessiontoken',
    }, 'Request headers');
}

done_testing;



( run in 1.132 second using v1.01-cache-2.11-cpan-364913b4093 )