File Coverage

blib/lib/Finance/BankUtils/ID/Mechanize.pm
Criterion Covered Total %
statement 23 38 60.5
branch 0 6 0.0
condition 2 7 28.5
subroutine 7 8 87.5
pod 1 1 100.0
total 33 60 55.0


line stmt bran cond sub pod time code
1             package Finance::BankUtils::ID::Mechanize;
2              
3             our $DATE = '2019-05-10'; # DATE
4             our $VERSION = '0.500'; # VERSION
5              
6 1     1   16 use 5.010;
  1         3  
7 1     1   6 use strict;
  1         1  
  1         17  
8 1     1   4 use warnings;
  1         1  
  1         26  
9 1     1   5 use Log::ger;
  1         1  
  1         5  
10              
11 1     1   183 use parent qw(WWW::Mechanize);
  1         2  
  1         6  
12              
13 1     1   110633 use String::Indent ();
  1         320  
  1         241  
14              
15             sub new {
16 1     1 1 5 my ($class, %args) = @_;
17 1         6 my $mech = WWW::Mechanize->new;
18 1   50     11880 $mech->{verify_https} = $args{verify_https} // 0;
19 1   50     5 $mech->{https_ca_dir} = $args{https_ca_dir} // "/etc/ssl/certs";
20 1         4 $mech->{https_host} = $args{https_host};
21 1         16 bless $mech, $class;
22             }
23              
24             # will be set by some other code, and will be immediately consumed and emptied
25             # by _make_request().
26             our $saved_resp;
27              
28             sub _make_request {
29 0     0     my $self = shift;
30 0           my $req = shift;
31             local $ENV{HTTPS_CA_DIR} = $self->{verify_https} ?
32 0 0         $self->{https_ca_dir} : '';
33 0           log_trace("HTTPS_CA_DIR = %s", $ENV{HTTPS_CA_DIR});
34 0 0 0       if ($self->{verify_https} && $self->{https_host}) {
35 0           $req->header('If-SSL-Cert-Subject',
36             qr!\Q/CN=$self->{https_host}\E(/|$)!);
37             }
38 0           log_trace("Mech request:\n" . String::Indent::indent(' ', $req->headers_as_string));
39 0           my $resp;
40 0 0         if ($saved_resp) {
41 0           $resp = $saved_resp;
42 0           $saved_resp = undef;
43 0           log_trace("Mech response (from saved):" .
44             String::Indent::indent(' ', $resp->headers_as_string));
45             } else {
46 0           $resp = $self->SUPER::_make_request($req, @_);
47 0           log_trace("Mech response:\n" . String::Indent::indent(' ', $resp->headers_as_string));
48             }
49 0           $resp;
50             }
51              
52             1;
53             # ABSTRACT: A subclass of WWW::Mechanize
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             Finance::BankUtils::ID::Mechanize - A subclass of WWW::Mechanize
64              
65             =head1 VERSION
66              
67             This document describes version 0.500 of Finance::BankUtils::ID::Mechanize (from Perl distribution Finance-Bank-ID-BCA), released on 2019-05-10.
68              
69             =head1 SYNOPSIS
70              
71             my $mech = Finance::BankUtils::ID::Mechanize->new(
72             verify_https => 1,
73             #https_ca_dir => '/etc/ssl/certs',
74             https_host => 'example.com',
75             );
76             # use as you would WWW::Mechanize object ...
77              
78             =head1 DESCRIPTION
79              
80             This is a subclass of WWW::Mechanize that can do some extra stuffs:
81              
82             =over
83              
84             =item * HTTPS certificate verification
85              
86             =item * use saved response from a file
87              
88             =item * log using Log::ger
89              
90             =back
91              
92             =head1 METHODS
93              
94             =head2 new()
95              
96             =head2 request()
97              
98             =head1 HOMEPAGE
99              
100             Please visit the project's homepage at L<https://metacpan.org/release/Finance-Bank-ID-BCA>.
101              
102             =head1 SOURCE
103              
104             Source repository is at L<https://github.com/perlancar/perl-Finance-Bank-ID-BCA>.
105              
106             =head1 BUGS
107              
108             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Bank-ID-BCA>
109              
110             When submitting a bug or request, please include a test-file or a
111             patch to an existing test-file that illustrates the bug or desired
112             feature.
113              
114             =head1 AUTHOR
115              
116             perlancar <perlancar@cpan.org>
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2019, 2017, 2015, 2014, 2013, 2012, 2011, 2010 by perlancar@cpan.org.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut