Amazon-S3-Thin

 view release on metacpan or  search on metacpan

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

#    my $path           = "static.johnsmith.net:8080/db-backup.dat.gz";
    my $path           = "static.johnsmith.net/db-backup.dat.gz";
    my $user_agent     = "curl/7.15.5";
    my $x_amz_acl      = "public-read";
    my $content_type   = "application/x-download";
    my $content_md5    = "4gJE4saaMU4BqNR0kLY+lw==";
    my @x_amz_meta_reviewed_by = ('joe@johnsmith.net', 'jane@johnsmith.net');
    my $x_amz_meta_filechecksum = '0x02661779';
    my $x_amz_meta_checksum_algorithm = 'crc32';
    my $content_disposition = "attachment; filename=database.dat";
    my $content_encoding = "gzip";
    my $content_length   = 5913339;

    my $string_to_sign = "PUT\n4gJE4saaMU4BqNR0kLY+lw==\napplication/x-download\nTue, 27 Mar 2007 21:06:08 +0000\nx-amz-acl:public-read\nx-amz-meta-checksumalgorithm:crc32\nx-amz-meta-filechecksum:0x02661779\nx-amz-meta-reviewedby:joe\@johnsmith.net,...

    my $signer = Amazon::S3::Thin::Signer::V2->new($credentials);
    my $hdr = HTTP::Headers->new;
    $hdr->header("Date", $date);
    $hdr->header("User-Agent", $user_agent);
    $hdr->header("x-amz-acl", $x_amz_acl);
    $hdr->header("content-type", $content_type);
    $hdr->header("Content-MD5", $content_md5);
    $hdr->header("X-Amz-Meta-ReviewedBy", join(',' => @x_amz_meta_reviewed_by));
    $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.744 second using v1.01-cache-2.11-cpan-0b5f733616e )