[Catalyst] Testing Catalyst::Controller::REST based controllers
Damon Snyder
drsnyder at influxmedia.com
Tue Feb 26 20:20:10 GMT 2008
Hi Everyone,
I'm developing a Catalyst::Controller::REST based controller in an app
that I'm working on. I've created the _GET and _POST methods and now
I'm trying to test them. They work fine if I test them using curl from
the command line or LWP::UserAgent, but now I'm trying to add tests
for them using Test::WWW::Mechanize::Catalyst.
I'm having a hard time getting Test::WWW::Mechanize::Catalyst to
interact my REST controller. I can build the request fine with
HTTP::Request and LWP::UserAgent, but I can't seem to get it to work
with Test::WWW::Mechanize::Catalyst. Does anyone have any example code
where they were able to get the GET, POST, and PUT to test
successfully using Test::WWW::Mechanize::Catalyst?
Here is the basic construction of the POST with LWP::UserAgent and
Test::More:
my $ua = new LWP::UserAgent;
$ua->default_header( 'X-Username' => 'xxxx', 'X-Password' => 'xxxxx' );
#my $req = new HTTP::Request POST => "http://localhost/rest/something";
$req->content_type('text/x-json');
$req->content(to_json(( { name => "bla", foo => "haaa" } ));
my $res = $ua->request($req);
ok($res, "Request was successful");
ok($res->headers->{status} == 201);
my $something = from_json($res->content);
ok($something->{foo} eq 'haaa');
I could continue with LWP::UserAgent, but it seems easier and more
maintainable to use the catalyst mechanize route if possible (testing
against other servers, don't need local server running etc).
Thanks,
Damon
More information about the Catalyst
mailing list