Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to test a Catalyst REST API

I'm building a RESTful web service, using Catalyst::Controller::REST. Usually for web testing I use Test::WWW::Mechanize, but that seems more appropriate for "GET/POST HTML RPC" testing. Are there any Test modules that would make testing of HTTP with basic auth, using GET/POST/PUT/DELETE etc and JSON easy? perhaps something that integrates well with Catalyst/PSGI so I don't have to start a webserver?

like image 646
xenoterracide Avatar asked Nov 29 '11 03:11

xenoterracide


2 Answers

Catalyst::Test is a subclass of LWP::UserAgent. The below should give you the right idea:

#!/usr/bin/env perl
use warnings;
use strict;

use Test::More;
use Catalyst::Test 'MyApp';
use HTTP::Request::Common;
use JSON::Any; # or whatever json module you usually use
my $data = 'some_json_data_here';
my $res = request(
    POST '/some_path',
    Content_Type => 'text/xml',
    Content => $data,
);

my $content = json_decode($res->content); # or whatever, can't remember the interface.
my $expected = "some_data";
is_deeply ( $content, $expected); 
like image 71
singingfish Avatar answered Oct 20 '22 03:10

singingfish


Or in more modern parlance:

  my $data = '{"username":"xyz","password":"xyz"}';
  my $res = request
    (
     POST '/bar/thing',
     Content_Type => 'application/json',
     Content => $data,
    );

;)

like image 32
Dave Hodgkinson Avatar answered Oct 20 '22 03:10

Dave Hodgkinson