File Coverage

blib/lib/HealthCheck/Diagnostic/RemoteHealth.pm
Criterion Covered Total %
statement 32 33 96.9
branch 5 6 83.3
condition 5 10 50.0
subroutine 8 8 100.0
pod 3 3 100.0
total 53 60 88.3


line stmt bran cond sub pod time code
1             package HealthCheck::Diagnostic::RemoteHealth;
2 2     2   526879 use parent 'HealthCheck::Diagnostic::WebRequest';
  2         230  
  2         15  
3 2     2   166850 use strict;
  2         12  
  2         65  
4 2     2   10 use warnings;
  2         5  
  2         163  
5              
6 2     2   1871 use JSON;
  2         33933  
  2         16  
7              
8             # ABSTRACT: Get results from an HTTP HealthCheck
9 2     2   406 use version;
  2         3  
  2         17  
10             our $VERSION = 'v0.1.2'; # VERSION
11              
12             sub new {
13 1     1 1 392123 my ($class, @params) = @_;
14              
15             my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
16 1 50 33     12 ? %{ $params[0] } : @params;
  0         0  
17              
18             # Allows 200 and 503 codes by default.
19 1   50     27 $params{status_code} //= '200, 503';
20              
21 1         24 return $class->SUPER::new(
22             id => 'remotehealth',
23             label => 'RemoteHealth',
24             %params,
25             );
26             }
27              
28             sub run {
29 6     6 1 47506 my ($self, %params) = @_;
30 6         60 my $result = $self->next::method(%params);
31              
32             # Throws away the HTTP status check if OK,
33             # since it's implied to be successful
34             # if it retrieves the encoded JSON object.
35 6 100 50     3005 if (($result->{results}->[0]->{status} || '') eq 'OK' ) {
36 4         10 shift @{ $result->{results} };
  4         14  
37             # info key is removed since it is redundant with the result-level info keys
38 4         40 return { results => $result->{results} };
39             }
40 2         10 return $result;
41             }
42              
43             # Checking for content regex from JSON seems unnecessary, so this has been
44             # repurposed to return the decoded JSON object.
45             sub check_content {
46 4     4 1 7107 my ($self, $response) = @_;
47              
48 4         14 local $@;
49 4         13 my $remote_result = eval { decode_json($response->content) };
  4         26  
50             return {
51 4 100 66     148 status => 'CRITICAL',
52             info => 'Could not decode JSON.',
53             data => $response->content,
54             } if $@ or ref($remote_result) ne 'HASH';
55              
56 3         15 return $remote_result;
57             }
58              
59             1;
60              
61             __END__