581409345aa830ef7a991992003b86f3a2d0937c
[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(@_,
19         {
20             content_type => { type => SCALAR },
21         },
22     );
23     my $ref = { 
24         'ua' => LWP::UserAgent->new,
25         'content_type' => $p{'content_type'},
26     };
27     bless $ref, $self;
28 }
29
30 sub get {
31     my $self = shift;
32     my %p = validate(@_,
33         {
34             url => { type => SCALAR },
35         },
36     );
37     my $req = HTTP::Request->new('GET' => $p{'url'});
38     $req->content_type($self->{'content_type'});
39     return $req;
40 }
41
42 sub delete {
43     my $self = shift;
44     my %p = validate(@_,
45         {
46             url => { type => SCALAR },
47         },
48     );
49     my $req = HTTP::Request->new('DELETE' => $p{'url'});
50     $req->content_type($self->{'content_type'});
51     return $req;
52 }
53
54 sub put {
55     my $self = shift;
56     my %p = validate(@_,
57         {
58             url => { type => SCALAR },
59             data => 1,
60         },
61     );
62     my $req = HTTP::Request->new('PUT' => $p{'url'});
63     $req->content_type($self->{'content_type'});
64     $req->content_length(do { use bytes; length($p{'data'}) });
65     $req->content($p{'data'});
66     return $req;
67 }
68
69 sub post {
70     my $self = shift;
71     my %p = validate(@_,
72         {
73             url => { type => SCALAR },
74             data => { required => 1 },
75         },
76     );
77     my $req = HTTP::Request->new('POST' => $p{'url'});
78     $req->content_type($self->{'content_type'});
79     $req->content_length(do { use bytes; length($p{'data'}) });
80     $req->content($p{'data'});
81     return $req;
82 }
83
84
85 1;
86