Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Nipe/Component/Utils/Status.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ package Nipe::Component::Utils::Status {

Readonly my $SUCCESS_CODE => 200;

our $VERSION = '0.0.4';
our $VERSION = '0.0.5';

sub new {
my $api_check = 'https://check.torproject.org/api/ip';
my $request = HTTP::Tiny -> new -> get($api_check);
my $request = HTTP::Tiny -> new(verify_SSL => 1) -> get($api_check);

if ($request -> {status} == $SUCCESS_CODE) {
my $data = decode_json($request -> {content});
Expand All @@ -26,4 +26,4 @@ package Nipe::Component::Utils::Status {
}
}

1;
1;
50 changes: 50 additions & 0 deletions tests/status.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::MockModule;

use lib './lib';

BEGIN {
use_ok('Nipe::Component::Utils::Status');
}

my $http_mock = Test::MockModule -> new('HTTP::Tiny');

subtest 'reports tor when IsTor is true' => sub {
plan tests => 2;

$http_mock -> mock('get', sub {
return { status => 200, content => '{"IsTor":true,"IP":"185.220.101.1"}' };
});

my $output = Nipe::Component::Utils::Status -> new();
ok(index($output, 'Status: true') >= 0, 'status is true');
ok(index($output, '185.220.101.1') >= 0, 'ip is shown');
};

subtest 'reports not tor when IsTor is false' => sub {
plan tests => 1;

$http_mock -> mock('get', sub {
return { status => 200, content => '{"IsTor":false,"IP":"8.8.8.8"}' };
});

my $output = Nipe::Component::Utils::Status -> new();
ok(index($output, 'Status: false') >= 0, 'status is false');
};

subtest 'reports an error when the request fails' => sub {
plan tests => 1;

$http_mock -> mock('get', sub {
return { status => 599, content => q{} };
});

my $output = Nipe::Component::Utils::Status -> new();
ok(index($output, 'not possible to establish a connection') >= 0, 'error branch');
};

done_testing();