Skip to content

Commit

Permalink
Tests!!
Browse files Browse the repository at this point in the history
  • Loading branch information
kablamo committed Apr 24, 2012
1 parent 0283c7d commit 1035fca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/MooseX/CachingProxy.pm
Expand Up @@ -52,12 +52,11 @@ requires 'url';
has _caching_proxy_dir => (
is => 'rw',
isa => 'Path::Class::Dir',
lazy => 1,
builder => '_build__caching_proxy_dir',
lazy_build => 1,
coerce => 1,
);

sub _build_caching_proxy_dir {
sub _build__caching_proxy_dir {
my $self = shift;
eval { $self->caching_proxy_dir };
$@ ? '/tmp/caching-proxy' : $self->caching_proxy_dir;
Expand All @@ -76,7 +75,7 @@ sub _build__caching_proxy_app {
enable "Cache", #
match_url => '^/.*',
cache_dir => $self->_caching_proxy_dir;
Plack::App::Proxy->new( remote => $self->_caching_proxy_url )->to_app;
Plack::App::Proxy->new( remote => $self->url )->to_app;
};
}

Expand Down
39 changes: 39 additions & 0 deletions t/basic.t
@@ -0,0 +1,39 @@
use LWP::UserAgent;
use MooseX::CachingProxy;
use MooseX::Test::Role;
use Test::Most;
use lib 't/lib';
use t::Server;

requires_ok('MooseX::CachingProxy', qw/url/);

my $server = t::Server->new(); # start a web server
my $uri = $server->uri . "boop"; # uri to use in tests

# - create a mock object called $mock
# - apply role to a $mock
# - create url() and make_request() methods on $mock
my $mock = consumer_of(
'MooseX::CachingProxy',
url => sub { $server->uri },
make_request => sub {
my ($self, $uri) = @_;
my $request = HTTP::Request->new(GET => $uri);
return LWP::UserAgent->new()->request($request);
},
);

$mock->start_caching_proxy();

ok $mock->make_request($uri)->is_success, "request";

undef $server; # shut down web server

ok $mock->make_request($uri)->is_success, "request from cache";

$mock->stop_caching_proxy();

ok !$mock->make_request($uri)->is_success, "request; no cache, no server";

done_testing;

8 changes: 8 additions & 0 deletions t/lib/t/Server.pm
@@ -0,0 +1,8 @@
package t::Server;
use base 'Test::HTTP::Server';

sub Test::HTTP::Server::Request::boop {
return "hey world";
}

1;

0 comments on commit 1035fca

Please sign in to comment.