bb748e97daa8cdf8eb92f59ef60432676dd48a1b
[catagits/Catalyst-Action-REST.git] / t / lib / Test / Rest.pm
1 #
2 # Rest.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 10/16/2006 11:11:25 AM PDT
5 #
6 # $Id: $
7
8 package Test::Rest;
9
10 use strict;
11 use warnings;
12
13 use LWP::UserAgent;
14 use Params::Validate qw(:all);
15
16 sub new {
17     my $self = shift;
18     my %p    = validate( @_, { content_type => { type => SCALAR }, }, );
19     my $ref  = {
20         'ua'           => LWP::UserAgent->new,
21         'content_type' => $p{'content_type'},
22     };
23     bless $ref, $self;
24 }
25
26 {
27     my @non_data_methods = qw(GET DELETE OPTIONS);
28     foreach my $method (@non_data_methods) {
29         no strict 'refs';
30         my $sub = lc($method);
31         *$sub = sub {
32             my $self = shift;
33             my %p    = validate( @_, { url => { type => SCALAR }, }, );
34             my $req  = HTTP::Request->new( "$method" => $p{'url'} );
35             $req->content_type( $self->{'content_type'} );
36             return $req;
37         };
38     }
39
40     my @data_methods = qw(PUT POST);
41     foreach my $method (@data_methods) {
42         no strict 'refs';
43         my $sub = lc($method);
44         *{$sub} = sub {
45             my $self = shift;
46             my %p    = validate(
47                 @_,
48                 {
49                     url  => { type => SCALAR },
50                     data => 1,
51                 },
52             );
53             my $req = HTTP::Request->new( "$method" => $p{'url'} );
54             $req->content_type( $self->{'content_type'} );
55             $req->content_length(
56                 do { use bytes; length( $p{'data'} ) }
57             );
58             $req->content( $p{'data'} );
59             return $req;
60         };
61     }
62 }
63
64 1;
65