File Coverage

blib/lib/Test/Mock/LWP/Distilled/JSON.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 36 37 97.3


line stmt bran cond sub pod time code
1             package Test::Mock::LWP::Distilled::JSON;
2              
3 2     2   418134 use Moo::Role;
  2         14929  
  2         11  
4 2     2   1069 use LWP::JSON::Tiny;
  2         57448  
  2         449  
5              
6             # Have you updated the version number in the POD below?
7             our $VERSION = '1.000';
8             $VERSION = eval $VERSION;
9              
10             =head1 NAME
11              
12             Test::Mock::LWP::Distilled::JSON - JSON support for Test::Mock::LWP::Distilled
13              
14             =head1 VERSION
15              
16             This is version 1.000.
17              
18             =head1 SYNOPSIS
19              
20             package My::JSON::Test::LWP::UserAgent;
21            
22             use Moo;
23             extends 'LWP::UserAgent::JSON';
24             with 'Test::Mock::LWP::Distilled', 'Test::Mock::LWP::Distilled::JSON';
25            
26             sub filename_suffix { 'my-json-test' }
27            
28             sub distilled_request_from_request {
29             my ($self, $request) = @_;
30            
31             ...
32             }
33              
34             # distilled_response_from_response and response_from_distilled_response
35             # implemented automatically.
36              
37             =head1 DESCRIPTION
38              
39             This is a very simple extension to L that
40             handles the very common case of APIs that return JSON. I you are happy
41             that only the HTTP code and the JSON content of any website response are
42             important (you don't need to remember any other headers), then you can just
43             pull in this code and not worry about having to deal with JSON.
44              
45             If you occasionally need other information, you may still be able to use this
46             role but wrap it with method modifiers. Or just steal the code; it's not
47             complicated.
48              
49             Note that your base class must be an extension of L
50             (provided by L) for the automatic JSON conversion to work.
51              
52             =head2 Provided methods
53              
54             =head3 distilled_response_from_response
55              
56             Returns a hashref with keys C (the HTTP code) and either
57             C (the result of decoding JSON in the response), or
58             C (the raw decoded content) and C
59             (the content type of the response).
60              
61             =cut
62              
63             sub distilled_response_from_response {
64 2     2 1 21991 my ($self, $response) = @_;
65              
66 2         12 my %distilled_response = (code => $response->code);
67             # LWP::JSON::Tiny returns undef if there's nothing that looks like JSON
68             # in the response, so be prepared for this to fail...
69 2 50       28 if ($response->can('json_content')) {
70 2         20 $distilled_response{json_content} = $response->json_content;
71             }
72             # ...and if so remember the raw contents elsewhere.
73 2 100       2439 if (!$response->json_content) {
74 1         24 $distilled_response{content_type} = $response->content_type;
75 1         22 $distilled_response{decoded_content} = $response->decoded_content;
76             }
77 2         3484 return \%distilled_response;
78             }
79              
80             =head3 response_from_distilled_response
81              
82             Converts the recorded mock back.
83              
84             =cut
85              
86             sub response_from_distilled_response {
87 2     2 1 6852 my ($self, $distilled_response) = @_;
88              
89 2         14 my $response = HTTP::Response::JSON->new;
90 2         75 $response->code($distilled_response->{code});
91 2 100       17 if ($distilled_response->{json_content}) {
92             # HTTP::Request::JSON knows how to construct JSON from a Perl
93             # data structure, so reuse its business logic.
94 1         8 my $request = HTTP::Request::JSON->new;
95 1         125 $request->json_content($distilled_response->{json_content});
96 1         535 $response->content_type($request->content_type);
97 1         35 $response->content($request->content);
98             } else {
99 1         7 $response->content_type($distilled_response->{content_type});
100 1         23 $response->content($distilled_response->{decoded_content});
101             }
102 2         54 return $response;
103             }
104              
105             1;