Net-Amazon-S3

 view release on metacpan or  search on metacpan

lib/Shared/Examples/Net/Amazon/S3.pm  view on Meta::CPAN

package Shared::Examples::Net::Amazon::S3;
# ABSTRACT: used for testing and as example
$Shared::Examples::Net::Amazon::S3::VERSION = '0.992';
use strict;
use warnings;

use parent qw[ Exporter::Tiny ];

use Hash::Util;
use Ref::Util (
	qw[ is_regexpref ],
);

use Test::Deep;
use Test::More;
use Test::LWP::UserAgent;

use Net::Amazon::S3;

use Shared::Examples::Net::Amazon::S3::API;
use Shared::Examples::Net::Amazon::S3::Client;
use Shared::Examples::Net::Amazon::S3::Request;

our @EXPORT_OK = (
	qw[ s3_api_with_signature_4 ],
	qw[ s3_api_with_signature_2 ],
	qw[ expect_net_amazon_s3_feature ],
	qw[ expect_net_amazon_s3_operation ],
	qw[ expect_operation_list_all_my_buckets ],
	qw[ expect_operation_bucket_create ],
	qw[ expect_operation_bucket_delete ],
	qw[ with_fixture ],
	qw[ fixture ],
	qw[ with_response_fixture ],
);

my %fixtures;
sub fixture {
	my ($name) = @_;

	$fixtures{$name} = eval "require Shared::Examples::Net::Amazon::S3::Fixture::$name"
		unless defined $fixtures{$name};

	die "Fixture $name not found: $@"
		unless defined $fixtures{$name};

	return +{ %{ $fixtures{$name} } };
}

sub with_fixture {
	my ($name) = @_;

	my $fixture = fixture ($name);
	return wantarray
		? %$fixture
		: $fixture
		;
}

sub with_response_fixture {
	my ($name) = @_;

	my $fixture = fixture ($name);
	my $response_fixture = {};

	for my $key (keys %$fixture) {
		my $new_key;
		$new_key ||= "with_response_data" if $key eq 'content';
		$new_key ||= "with_$key" if $key =~ m/^response/;
		$new_key ||= "with_response_header_$key";

lib/Shared/Examples/Net/Amazon/S3.pm  view on Meta::CPAN

	);
}

sub s3_api_with_signature_4 {
	s3_api (
		@_,
		aws_access_key_id     => 'AKIDEXAMPLE',
		aws_secret_access_key => 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY',
		authorization_method  => 'Net::Amazon::S3::Signature::V4',
		secure                => 1,
		use_virtual_host      => 1,
	);
}

sub s3_api_with_signature_2 {
	s3_api (
		@_,
		aws_access_key_id     => 'AKIDEXAMPLE',
		aws_secret_access_key => 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY',
		authorization_method  => 'Net::Amazon::S3::Signature::V2',
		secure                => 1,
		use_virtual_host      => 1,
	);
}

sub expect_net_amazon_s3_feature {
	my ($title, %params) = @_;

	my $s3 = delete $params{with_s3};
	my $feature = delete $params{feature};
	my $expectation = "expect_$feature";

	local $Test::Builder::Level = $Test::Builder::Level + 1;

	subtest $title => sub {
		plan tests => 2;

		if (my $code = Shared::Examples::Net::Amazon::S3::API->can ($expectation)) {
			$code->( "using S3 API" => (
				with_s3 => $s3,
				%params
			));
		} else {
			fail "Net::Amazon::S3 feature expectation $expectation not found";
		}

		if (my $code = Shared::Examples::Net::Amazon::S3::Client->can ($expectation)) {
			$code->( "using S3 Client" => (
				with_client => Net::Amazon::S3::Client->new (s3 => $s3),
				%params
			));
		} else {
			fail "Net::Amazon::S3::Client feature expectation $expectation not found";
		}
	};
}

sub _operation_parameters {
	my ($params, @names) = @_;
	my $map = {};
	$map = shift @names if Ref::Util::is_plain_hashref ($names[0]);

	return
		map +( ($map->{$_} || $_) => $params->{"with_$_"} ),
		grep exists $params->{"with_$_"},
		@names
		;
}

sub _with_keys {
	map "with_$_", @_;
}

sub _keys_operation () {
	return (
		qw[ -shared_examples ],
		qw[ -method ],
		qw[ with_s3 ],
		qw[ with_client ],
		qw[ shared_examples ],
		qw[ with_response_code ],
		qw[ with_response_data ],
		qw[ with_response_headers ],
		qw[ with_response_header_content_type ],
		qw[ with_response_header_content_length ],
		qw[ expect_s3_err ],
		qw[ expect_s3_errstr ],
		qw[ expect_data ],
		qw[ expect_request ],
		qw[ expect_request_content ],
		qw[ expect_request_headers ],
		qw[ throws ],
	);
}

sub _expect_request {
	my ($request, $expect, $title) = @_;

	local $Test::Builder::Level = $Test::Builder::Level + 1;

	my ($method, $uri) = %$expect;
	cmp_deeply
		$request,
		all (
			methods (method => $method),
			methods (uri => methods (as_string => $uri)),
		),
		$title || 'expect request'
		;
}

sub _expect_request_content {
	my ($request, $expected, $title) = @_;

	local $Test::Builder::Level = $Test::Builder::Level + 1;

	my $got = Shared::Examples::Net::Amazon::S3::Request::_canonical_xml ($request->content);
	$expected = Shared::Examples::Net::Amazon::S3::Request::_canonical_xml ($expected);

	cmp_deeply $got, $expected, $title || "expect request content";
}



( run in 1.053 second using v1.01-cache-2.11-cpan-39bf76dae61 )