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

2

7

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?

Cuesta answered 29/11, 2011 at 3:16 Comment(0)
D
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); 
Divert answered 29/11, 2011 at 3:23 Comment(1)
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.Cuesta
A
1

Or in more modern parlance:

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

;)

Ardith answered 19/3, 2018 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.