6

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?

xenoterracide
  • 16,274
  • 24
  • 118
  • 243

2 Answers2

7

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); 
singingfish
  • 3,136
  • 22
  • 25
  • that'll work, but I was hoping there was something that would just do all the serialization/deserialization for me without writing all the JSON/HTTP code, e.g convenience methods. – xenoterracide Nov 29 '11 at 20:37
1

Or in more modern parlance:

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

;)

Dave Hodgkinson
  • 375
  • 4
  • 13