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?
What's the easiest way to test a Catalyst REST API
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);
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
Or in more modern parlance:
my $data = '{"username":"xyz","password":"xyz"}';
my $res = request
(
POST '/bar/thing',
Content_Type => 'application/json',
Content => $data,
);
;)
© 2022 - 2024 — McMap. All rights reserved.