| 1 | package HealthCheck::Diagnostic::RemoteHealth; | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent 'HealthCheck::Diagnostic::WebRequest'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Get results from an HTTP HealthCheck | ||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = 'v0.1.2'; # VERSION | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 13 | 1 | my ($class, @params) = @_; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH' | ||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | ? %{ $params[0] } : @params; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
ref $params[0] || ''
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1
ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # Allows 200 and 503 codes by default. | ||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | $params{status_code} //= '200, 503'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{"status_code"} //= "200, 503"
MC/DC0%0/1
$params{"status_code"} //= "200, 503"
$params{"status_code"}
Truth table50%1/2
A
A= $params{"status_code"}
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | return $class->SUPER::new( | |||||||||||||||||||||||||||||||
| 22 | id => 'remotehealth', | ||||||||||||||||||||||||||||||||
| 23 | label => 'RemoteHealth', | ||||||||||||||||||||||||||||||||
| 24 | %params, | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 29 | 6 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||
| 30 | 6 | 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 | ▶ | if (($result->{results}->[0]->{status} || '') eq 'OK' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$result->{'results'}[0]{'status'} || ''
MC/DC0%0/1
$result->{'results'}[0]{'status'} || ''
$result->{'results'}[0]{'status'}
Truth table50%1/2
A
A= $result->{'results'}[0]{'status'}
| |||||||||||||||||||||||||||||||||
| 36 | 4 | shift @{ $result->{results} }; | |||||||||||||||||||||||||||||||
| 37 | # info key is removed since it is redundant with the result-level info keys | ||||||||||||||||||||||||||||||||
| 38 | 4 | return { results => $result->{results} }; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | 2 | 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 | 4 | ▶ | sub check_content { | ||||||||||||||||||||||||||||||
|
Subroutine
check_content: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 4 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 4 | local $@; | |||||||||||||||||||||||||||||||
| 49 | 4 | my $remote_result = eval { decode_json($response->content) }; | |||||||||||||||||||||||||||||||
| 50 | return { | ||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | status => 'CRITICAL', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or ref $remote_result ne "HASH"
MC/DC50%1/2
$@ or ref $remote_result ne "HASH"
$@
ref $remote_result ne "HASH"
Truth table66%2/3
A or B
A= $@B= ref $remote_result ne "HASH"
| |||||||||||||||||||||||||||||||||
| 52 | info => 'Could not decode JSON.', | ||||||||||||||||||||||||||||||||
| 53 | data => $response->content, | ||||||||||||||||||||||||||||||||
| 54 | } if $@ or ref($remote_result) ne 'HASH'; | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 3 | return $remote_result; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 2 | 1; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | __END__ | ||||||||||||||||||||||||||||||||