summaryrefslogtreecommitdiffstats
path: root/t/08-tls.t
blob: 950fef3cda8e75232eaa1a48b72f2836f5d0b162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use warnings;

use FindBin ();
use lib "$FindBin::Bin/lib";

use HTTP::Tiny;
use Test::More;
use TestServer qw(se_address json new_client);
use Time::HiRes ();

use Cepis;

my ($expected) = HTTP::Tiny->can('can_ssl') ? HTTP::Tiny->can_ssl : (1);
my ( $ok, $why ) = Cepis::Client::tls_available();

is( !!$ok, !!$expected, 'tls_available agrees with HTTP::Tiny->can_ssl' );

SKIP: {
    skip 'this Perl has HTTPS support', 6 if $ok;

    ok( defined $why && length $why, 'the reason is reported' );

    my $client = Cepis::Client->new(
        base_url     => 'https://cep.is',
        max_attempts => 3,
        backoff_base => 30,
        backoff_max  => 30,
    );

    my $started = Time::HiRes::time();
    my $done    = eval { $client->lookup('01001000'); 1 };
    my $err     = $@;

    ok( !$done, 'an https call without TLS support throws' );
    isa_ok( $err, 'Cepis::Error::Network' );
    like( "$err", qr/IO::Socket::SSL/, 'the message names IO::Socket::SSL' );
    like( "$err", qr/Net::SSLeay/,     'and Net::SSLeay' );
    cmp_ok( Time::HiRes::time() - $started, '<', 5,
        'and it fails at once rather than retrying a missing module' );
}

SKIP: {
    skip 'this Perl has no HTTPS support', 1 unless $ok;
    is( $why, undef, 'no reason is reported when HTTPS works' );
}

{
    my $server = TestServer->new(
        routes => { '/01001000' => [ 200, json( se_address() ), {} ] } );
    my $client = new_client($server);

    is( $client->lookup('01001000')->uf, 'SP',
        'an http base_url is never blocked by the TLS guard' );
    $server->stop;
}

done_testing();