The easiest way to test Catalyst REST APIs

I am creating a RESTful web service using Catalyst :: Controller :: REST . I usually use Test :: WWW :: Mechanize for web testing, but this seems more suitable for testing GET / POST HTML RPC. Are there any test modules that will test HTTP with basic auth using GET / POST / PUT / DELETE etc. And JSON? maybe something that integrates well with Catalyst / PSGI, so I don't need to start a web server?

+6
source share
2 answers

Catalyst :: Test is a subclass of LWP :: UserAgent. The following is the correct 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); 
+7
source

Or in a more modern language:

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

;)

+1
source

Source: https://habr.com/ru/post/902609/


All Articles