The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Mock::LWP::Distilled::JSON - JSON support for Test::Mock::LWP::Distilled

VERSION

This is version 1.000.

SYNOPSIS

 package My::JSON::Test::LWP::UserAgent;
 
 use Moo;
 extends 'LWP::UserAgent::JSON';
 with 'Test::Mock::LWP::Distilled', 'Test::Mock::LWP::Distilled::JSON';
 
 sub filename_suffix { 'my-json-test' }
 
 sub distilled_request_from_request {
     my ($self, $request) = @_;
 
     ...
 }

 # distilled_response_from_response and response_from_distilled_response
 # implemented automatically.

DESCRIPTION

This is a very simple extension to Test::Mock::LWP::Distilled that handles the very common case of APIs that return JSON. If you are happy that only the HTTP code and the JSON content of any website response are important (you don't need to remember any other headers), then you can just pull in this code and not worry about having to deal with JSON.

If you occasionally need other information, you may still be able to use this role but wrap it with method modifiers. Or just steal the code; it's not complicated.

Note that your base class must be an extension of LWP::UserAgent::JSON (provided by LWP::JSON::Tiny) for the automatic JSON conversion to work.

Provided methods

distilled_response_from_response

Returns a hashref with keys code (the HTTP code) and either json_content (the result of decoding JSON in the response), or decoded_content (the raw decoded content) and content_type (the content type of the response).

response_from_distilled_response

Converts the recorded mock back.