r48@latte: adam | 2006-12-03 11:32:40 -0800
[catagits/Catalyst-Action-REST.git] / t / lib / Test / Rest.pm
CommitLineData
7ad87df9 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
8package Test::Rest;
9
10use strict;
11use warnings;
12
13use LWP::UserAgent;
14use Params::Validate qw(:all);
15
16sub new {
17 my $self = shift;
d34c067a 18 my %p = validate( @_, { content_type => { type => SCALAR }, }, );
19 my $ref = {
20 'ua' => LWP::UserAgent->new,
7ad87df9 21 'content_type' => $p{'content_type'},
22 };
23 bless $ref, $self;
24}
25
d34c067a 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 }
7ad87df9 39
d34c067a 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 }
7ad87df9 62}
63
7ad87df9 641;
65