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?
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);
Or in more modern parlance:
my $data = '{"username":"xyz","password":"xyz"}';
my $res = request
(
POST '/bar/thing',
Content_Type => 'application/json',
Content => $data,
);
;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With