blib/lib/Finance/IG/Record/REST/Client.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 133 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
after_POST
2.0
after_GET
2.0
CRAP 2.0
1 # So this package was used to generate the hashed response files for testing.
2 # It cannot be uploaded to cpan named REST::Client as I do not own that name space.
3 # You could rename it as below to use.
4 # A better approach is likely to use the true name below, and reloacte in the correct place and use Package::Alias
5 # to call as is now done with Finance::IG::REST::Client.
6 # The action in this package is under the hood, it calls the true REST::CLient and records the
7 # rsults in files that can later be used stand alone with no internet.
8 # So this is a bit broken right now, but only needed if you want to expand the tests for Finance::IG.
9 # Could do with some tests of its own!
10 package REST::Client;
11 use strict;
12 no strict 'refs';
13 use warnings;
14 no warnings 'redefine';
15  
16 use JSON;
17 use Digest::MD5 qw(md5 md5_hex md5_base64);
18 my $datapath;
19 our $fdebug=0; # set to 1 prints some diagnostics about whats going on
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Finance::IG::Record::REST::Client, or more generally REST::Client
26  
27 =head1 Description.
28  
29 The aim of this module is to generate files used by the module Finance::IG::REST::Client which is a module that
30 mocks REST::Client by using pre-canned on disk responses to queries rather than the internet.
31  
32 THIS module is designedd to emulate REST::Client in providing data sourced from the internet, but also cans the
33 exchange for latter use by the mock version of REST::Client
34  
35 =head1 DESIGN
36  
37 There are a number of design issues that need to be handled, in particular interjecting this module as well as having the real module loaded.
38  
39 The latter is achieved by locating the real module, reading the file, changing the name to REST::Cli_orig, and then using an eval to load it.
40  
41 Its an important design decision that the unmodified Finance::IG.pm module is fitted up to use the this module or Finance::IG::REST::Client
42 entirely by manipulating %INC.
43  
44 =head1 VERSION
45  
46 Version 0.093
47  
48 =head1 SYNOPSIS
49  
50 See the included file Testing/test-record.pl
51  
52 =cut
53
54 our $VERSION=0.093;
55  
56 BEGIN {
57 my $rcpath;
58 shift(@INC); # remove local path that was needed to find this file
59 my $thispath=$INC{'REST/Client.pm'};
60 $datapath=$thispath;
61 # die "datapath=$datapath"; # /home/mark/igrec/work/Finance-IG/Testing/../lib/Finance/IG/Record/REST/Client.pm
62 $datapath=~s#/[^./]+/\.\.##g; # /home/mark/igrec/work/Finance-IG/lib/Finance/IG/Record/REST/Client.pm
63 $datapath=~s#/[^/]+$##;
64 $datapath=~s#/[^/]+$##;
65 $datapath=~s#/[^/]+$##;
66 $datapath=~s#/[^/]+$##;
67 $datapath=~s#/[^/]+$##;
68 $datapath=~s#/[^/]+$##;
69 $datapath=$datapath."/Testing/Data";
70 # die "datapath=$datapath"; # /home/mark/igrec/work/Finance-IG/Testing/Data
71 mkdir $datapath;
72 delete $INC{'REST/Client.pm'}; #
73 require REST::Client; # determine path to original module.
74 $rcpath=$INC{'REST/Client.pm'}; # typically /usr/share/perl5/REST/Client.pm
75  
76 open(F,$rcpath) or die "Cannot find REST::Client not installed? ";
77 my $code=join("",<F>);
78 close F;
79 $code=~s/REST::Client/REST::Cli_orig/gm;
80 $code=~s/return if \$self->can\('setHost'\)/return if \$self->can('ContentFile')/gm;
81 $code=~s/no strict 'refs';/no strict 'refs';no warnings 'redefine';/gm;
82 eval $code ;
83 }
84  
85 # Doesn't seem needed with global
86 # no warnings 'redefine';
87 # Subroutine REST::Client::PUT redefined at ...
88 #$SIG{__WARN__} = sub
89 #{
90 # my $warning = shift;
91 # my $m;
92 # warn $warning unless $warning =~ /Subroutine .* redefined at \(eval [0-9]+\)/;
93 # # Subroutine _buildUseragent redefined at (eval 179) line 491.
94 #};
95  
96 for my $name (qw/
97 PUT GET POST new _buildAccessors _buildUseragent getUseragent setUseragent
98 request _prepareURL getHost getTimeout
99 getCert getCa getPkcs12 getFollow getContentFile responseCode
100 responseHeader responseContent setHost
101 /
102 )
103 {
104 no strict 'refs';
105 # no warnings 'redefine'; # did work but doesn't now. ?
106  
107 my $orig = \&{"REST::Cli_orig::$name"};
108 my $before= \&{"REST::Client::before_$name"};
109 my $after= \&{"REST::Client::after_$name"};
110 my $responseContent = \&{"REST::Cli_orig::responseContent"};
111 my $responseHeader = \&{"REST::Cli_orig::responseHeader"};
112  
113 *{"REST::Client::$name"} =
114 sub {
115 my $self=$_[0];
116 defined(&$before) and &$before(@_);
117 my @return_values = wantarray ? $orig->(@_) : scalar $orig->(@_);
118
119 if (defined(&$after))
120 {
121 my $headers={};
122 for my $header (&{"REST::Cli_orig::responseHeaders"})
123 {
124 #print "retrieving header named $header\n";
125 my $value='';
126
127 $value=$self->responseHeader($header);
128 #print "$header=$value\n";
129 $headers->{$header}=$value;
130 }
131 &$after($self,
132 \@_,
133 JSON->new->canonical->encode(
134 { content=>decode_json($self->responseContent) ,
135 headers=>$headers
136 }
137 )
138 );
139 }
140 return wantarray ? @return_values : $return_values[0];
141 };
142 }
143  
144  
145 # delete $SIG{__WARN__}; # This causes reinstatement of the warnings.
146  
147 #sub before_POST
148 #{
149 # print "b4 post @_\n";
150 # die;
151 #}
152 sub after_POST
153 {
154 my ($self,$rparams,$content)=@_;
155 local $"=', ';
156 #die "@$rparams";
157 my ($url,$jdata,$headers)=@$rparams;
158 #my $jheaders=encode_json($headers);
159 #print "headers#1=$headers\n";
160 my $jheaders=JSON->new->canonical->encode($headers);
161 # print "After POST url=$url jdata=$jdata jheaders=$jheaders, content=\n". encode_json($content)."\n";
162 #### $content=JSON->new->canonical->encode(decode_json($content)->{$content});
163 $content=~s/("currentAccountId":)("[^"]+")/$1"REDACTED1"/g;
164 $content=~s/("clientId":)("[^"]+")/$1"REDACTED2"/g;
165 $content=~s/("accountId":)("[^"]+")/$1"REDACTED3"/g;
166 #$content=~s/\xfffd/£/gms;
167 $content=~s/\xA3/£/gms;
168 $content=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g;
169 $content=~s/("CST":)("[^"]+)"/$1"."/g;
170  
171 $jdata=~s/("identifier":)("[^"]+")/$1"igusername"/g;
172 $jdata=~s/("password":)("[^"]+")/$1"ig_correct_password"/g;
173 $jheaders=~s/("X-IG-API-KEY":)("[^"]+)"/$1"securitykey"/g;
174 $jheaders=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g;
175 $jheaders=~s/("CST":)("[^"]+)"/$1"."/g;
176 # print "After POST url=$url jdata=$jdata jheaders=$jheaders, content=\n". $content."\n";
177
178 $fdebug and print " url=$url\n jdata=$jdata\n jheaders=$jheaders\n";
179 my $file="$datapath/".md5_hex($url,$jdata,$jheaders).".txt";
180 $fdebug and print "writing file $file\n";
181 open(F,">",$file) or die "Cannot open file $file for write";
182 print F $content."\n"; ;
183 close F;
184 }
185 sub after_GET
186 {
187 my ($self,$rparams,$content)=@_;
188 #print "AFTER GET $$rparams[1]\n";
189 #print "rparams=@$rparams\n";
190 #my ($url,$headers)=@$rparams;
191 my ($url,$jdata,$headers)=@$rparams;
192 # my $jheaders=JSON->new->canonical->encode($headers);
193
194 #print "AFTER GET url=$url jdata=$jdata jheaders=$jheaders\n";
195 after_POST($self,[$url,$jdata,$headers] ,$content);
196 }
197 1;
198