AWS-S3

 view release on metacpan or  search on metacpan

t/aws/s3.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

package Mocked::HTTP::Response;

use Moose;
extends 'HTTP::Response';

sub content { return shift->{_msg}; }

1;

package main;

use Test::More 'no_plan';
use Test::Deep;
use Test::Exception;
use Data::Section::Simple 'get_data_section';

use Carp 'confess';
$SIG{__DIE__} = \&confess;

use_ok('AWS::S3');

my $s3 = AWS::S3->new(
  access_key_id     => $ENV{AWS_ACCESS_KEY_ID}     // 'foo',
  secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY} // 'bar',
  endpoint          => 'bad.hostname.',
);

my $bucket_name = "aws-s3-test-" . int(rand() * 1_000_000) . '-' . time() . "-foo";

eval {
    my $bucket = $s3->add_bucket( name => $bucket_name, location => 'us-west-1' );
};

like(
    $@,
    qr/Can't connect to aws-s3-test-.*?bad\.hostname/,
    'endpoint was used'
);

isa_ok(
	$s3->request( 'CreateBucket',bucket => 'foo' ),
	'AWS::S3::Request::CreateBucket'
);

subtest 'create bucket strange temporary redirect' => sub {
    plan tests => 8;    # make sure all tests in here get run

    my $i = 1;
    local *LWP::UserAgent::Determined::request = sub {
        my ( undef, $req ) = @_;

        if ( $i == 1 ) {

            # first PUT request, send a forward
            is( $req->method, 'PUT', 'bucket creation with PUT request' );
            is( $req->uri->as_string, 'http://bar.bad.hostname./', '... and with correct URI' );

            $i++;
            return HTTP::Response->new(
                307,
                'TEMPORARY REDIRECT',
                [ Location => 'http://example.org' ],
                '<fake>TemporaryRedirect</fake>'
            );
        }
        elsif ( $i == 2 ) {

            # the PUT is sent again, but to the forwarded location

            is( $req->method, 'PUT', 'redirected and second PUT request' );
            is( $req->uri->as_string, 'http://example.org', '... and to the correct URI' );

            $i++;
            return Mocked::HTTP::Response->new( 200, q{} );



( run in 0.549 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )