X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FTest%2FRest.pm;h=bb748e97daa8cdf8eb92f59ef60432676dd48a1b;hb=d34c067af1ed1960db3a10e29df8fcf223dac59e;hp=581409345aa830ef7a991992003b86f3a2d0937c;hpb=33e5de96f7c37e7b64611c77d305b1bf8d26b2d6;p=catagits%2FCatalyst-Action-REST.git diff --git a/t/lib/Test/Rest.pm b/t/lib/Test/Rest.pm index 5814093..bb748e9 100644 --- a/t/lib/Test/Rest.pm +++ b/t/lib/Test/Rest.pm @@ -15,72 +15,51 @@ use Params::Validate qw(:all); sub new { my $self = shift; - my %p = validate(@_, - { - content_type => { type => SCALAR }, - }, - ); - my $ref = { - 'ua' => LWP::UserAgent->new, + my %p = validate( @_, { content_type => { type => SCALAR }, }, ); + my $ref = { + 'ua' => LWP::UserAgent->new, 'content_type' => $p{'content_type'}, }; bless $ref, $self; } -sub get { - my $self = shift; - my %p = validate(@_, - { - url => { type => SCALAR }, - }, - ); - my $req = HTTP::Request->new('GET' => $p{'url'}); - $req->content_type($self->{'content_type'}); - return $req; -} - -sub delete { - my $self = shift; - my %p = validate(@_, - { - url => { type => SCALAR }, - }, - ); - my $req = HTTP::Request->new('DELETE' => $p{'url'}); - $req->content_type($self->{'content_type'}); - return $req; -} +{ + my @non_data_methods = qw(GET DELETE OPTIONS); + foreach my $method (@non_data_methods) { + no strict 'refs'; + my $sub = lc($method); + *$sub = sub { + my $self = shift; + my %p = validate( @_, { url => { type => SCALAR }, }, ); + my $req = HTTP::Request->new( "$method" => $p{'url'} ); + $req->content_type( $self->{'content_type'} ); + return $req; + }; + } -sub put { - my $self = shift; - my %p = validate(@_, - { - url => { type => SCALAR }, - data => 1, - }, - ); - my $req = HTTP::Request->new('PUT' => $p{'url'}); - $req->content_type($self->{'content_type'}); - $req->content_length(do { use bytes; length($p{'data'}) }); - $req->content($p{'data'}); - return $req; + my @data_methods = qw(PUT POST); + foreach my $method (@data_methods) { + no strict 'refs'; + my $sub = lc($method); + *{$sub} = sub { + my $self = shift; + my %p = validate( + @_, + { + url => { type => SCALAR }, + data => 1, + }, + ); + my $req = HTTP::Request->new( "$method" => $p{'url'} ); + $req->content_type( $self->{'content_type'} ); + $req->content_length( + do { use bytes; length( $p{'data'} ) } + ); + $req->content( $p{'data'} ); + return $req; + }; + } } -sub post { - my $self = shift; - my %p = validate(@_, - { - url => { type => SCALAR }, - data => { required => 1 }, - }, - ); - my $req = HTTP::Request->new('POST' => $p{'url'}); - $req->content_type($self->{'content_type'}); - $req->content_length(do { use bytes; length($p{'data'}) }); - $req->content($p{'data'}); - return $req; -} - - 1;