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

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 95 CC 3 SCAR 11.0
CC
3
Cov
0%
AUTOLOAD
6.0
POST
6.0
whoami
2.0
CRAP 3.0
1 package Finance::IG::REST::Client;
2 use JSON;
3 use Digest::MD5 qw(md5 md5_hex md5_base64);
4 my $datapath;
5 my $content;
6  
7 use vars '$AUTOLOAD';
8  
9 my $fdebug=0; # print debug infor relating to files etc.
10  
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Finance::IG::REST::Client - Module to mock REST::Client when testing Finance::IG
17  
18 =head1 DESCRIPTION
19  
20 This module mocks REST::CLient. Rather than sending request via the internet a series of files with names derived from a hash of
21 url and parameters are retrieved and contain the expected respons.
22  
23 The module is intended to be used with use Package::Alias, see below.
24  
25 The files can be generated with the module in Testing::Record::REST::Client, but you will need an IG account to run
26 generate files as you will need to really access the IG site.
27  
28 =head1 VERSION
29  
30 Version 0.093
31  
32 =cut
33  
34 our $VERSION=0.093;
35  
36 =head1 SYNOPSIS
37  
38  
39 use Package::Alias 'REST::Client'=>'Finance::IG::REST::Client' ;
40 use REST::Client;
41  
42 Or
43 use Package::Alias 'REST::Client'=>'Finance::IG::REST::Client' ;
44 use Some::Other::Module::That::UsesREST::Client'
45  
46 calls to Some::Other... # All use the mocked REST::Client;
47  
48 =head1 SUBROUTINES/METHODS
49  
50 This is a list of the implemented methods. We only implement those needed for testing Finance::IG
51  
52 =head2 new
53  
54 No parameters, returns a blessed reference to the item
55  
56 =cut
57  
58  
59 sub AUTOLOAD
60 {
61 $AUTOLOAD =~ s/.*:://;
62  
63 for my $name (qw/
64 GET PUT _buildAccessors _buildUseragent getUseragent setUseragent
65 request _prepareURL getHost getTimeout
66 getCert getCa getPkcs12 getFollow getContentFile responseCode
67 responseHeader responseContent setHost
68 /)
69 {
70 if ($AUTOLOAD eq $name)
71 {
72 return 1;
73 }
74 }
75  
76 shift->${\"NEXT::$AUTOLOAD"}(@_);
77 }
78  
79 =head2 whoami
80  
81 This function is not implemented in the original REST::Client. It returns a string
82  
83 This is Finance::IG::REST::Client
84  
85 so that you may know you have succesfully instantiated the right module.
86  
87 =cut
88  
89 sub whoami
90 {
91 return "This is Finance::IG::REST::Client";
92 }
93  
94 BEGIN {
95 my $rcpath;
96 my $thispath=$INC{'Finance/IG/REST/Client.pm'};
97 $datapath=$thispath;
98 $datapath=~s#/[^/]+$##;
99 $datapath=~s#/[^/]+$##;
100 $datapath=~s#/[^/]+$##;
101 $datapath=~s#/[^/]+$##;
102 $datapath=~s#/[^/]+$##;
103 $datapath=~s#/[^/]+$##;
104  
105 $datapath=$datapath."/Data";
106 # mkdir $datapath;
107 }
108  
109 #=pod
110 #
111 #for my $name (qw/
112 # PUT _buildAccessors _buildUseragent getUseragent setUseragent
113 # request _prepareURL getHost getTimeout
114 # getCert getCa getPkcs12 getFollow getContentFile responseCode
115 # responseHeader responseContent setHost
116 # /
117 # )
118 #{
119 # no strict 'refs';
120 # no warnings 'redefine';
121 #
122 # *{"Finance::IG::REST::Client::$name"} = sub {};
123 #}
124 #
125 #=cut
126  
127 sub new
128 {
129 return bless {};
130 }
131  
132 =head2 POST
133  
134 Mocks the REST::CLient POST function
135  
136 =cut
137  
138 sub POST{
139 my ($self,$url,$jdata,$headers)=@_;
140 local $"=', ';
141  
142 my $jheaders=JSON->new->canonical->encode($headers);
143 if ($jdata)
144 {
145 $jdata=~s/("identifier":)("[^"]+")/$1"igusername"/g;
146 $jdata=~s/("password":)("[^"]+")/$1"ig_correct_password"/g;
147 }
148 $jheaders=~s/("X-IG-API-KEY":)("[^"]+)"/$1"securitykey"/g;
149 $jheaders=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g;
150 $jheaders=~s/("CST":)("[^"]+)"/$1"."/g;
151  
152 $fdebug and print " url=$url\n jdata=$jdata\n jheaders=$jheaders\n";
153
154 my $file="$datapath/".md5_hex($url,$jdata,$jheaders).".txt";
155 $fdebug and print "Reading $file\n";
156 open(F,"<",$file) or die "Cannot open file $file for read";
157 binmode(F);
158 read F, $content, 1000000;
159 close F;
160 }
161
162 =head2 responseCode
163  
164 returns the success response code (200)
165  
166 =cut
167  
168 sub responseCode
169 {
170 return 200;
171 }
172  
173 =head2 responseContent
174  
175 returns the mocked content of the response
176  
177 =cut
178  
179 sub responseContent
180 {
181 $content=~s/,"headers":\{[^}]+\}\}//;
182 $content=~s/\{"content"://;
183 return $content;
184 }
185  
186 =head2 XSECURITYTOKEN
187  
188 =cut
189  
190 sub XSECURITYTOKEN
191 {
192 die $content;
193 }
194  
195 =head2 responseHeader
196  
197 returns the mocked response header.
198  
199 =cut
200  
201 sub responseHeader
202 {
203 my ($self,$header)=@_;
204 my $c=decode_json($content);
205 my $headers=$c->{headers};
206 return $headers->{$header};
207 }
208  
209 =head2 CST
210  
211 Dummy function.
212  
213 =cut
214  
215 sub CST
216 {
217 }
218  
219 =head2 GET
220  
221 Dummy function, calls POST
222  
223 =cut
224  
225 sub GET
226 {
227 my ($self,$url,$headers)=@_;
228 POST($self,$url,undef,$headers);
229 }
230 1;
231