| 1 | package Finance::IG::REST::Client; | ||||||||||
| 2 | 5 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use Digest::MD5 qw(md5 md5_hex md5_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | my $datapath; | |||||||||
| 5 | my $content; | ||||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use vars '$AUTOLOAD'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | 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 | 4 | 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 | 46 | ▶ | { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||
| 61 | 46 | $AUTOLOAD =~ s/.*:://; | |||||||||
| 62 | |||||||||||
| 63 | 46 | 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 | 874 | ▶ | if ($AUTOLOAD eq $name) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | { | ||||||||||
| 72 | 23 | return 1; | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 23 | 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 | 0 | ▶ | { | ||||||||
|
Subroutine
whoami: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 91 | 0 | return "This is Finance::IG::REST::Client"; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 5 | my $rcpath; | |||||||||
| 96 | 5 | my $thispath=$INC{'Finance/IG/REST/Client.pm'}; | |||||||||
| 97 | 5 | $datapath=$thispath; | |||||||||
| 98 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 99 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 100 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 101 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 102 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 103 | 5 | $datapath=~s#/[^/]+$##; | |||||||||
| 104 | |||||||||||
| 105 | 5 | $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 | 23 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 129 | 23 | return bless {}; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =head2 POST | ||||||||||
| 133 | |||||||||||
| 134 | Mocks the REST::CLient POST function | ||||||||||
| 135 | |||||||||||
| 136 | =cut | ||||||||||
| 137 | |||||||||||
| 138 | 23 | ▶ | sub POST{ | ||||||||
|
Subroutine
POST: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 139 | 23 | my ($self,$url,$jdata,$headers)=@_; | |||||||||
| 140 | 23 | local $"=', '; | |||||||||
| 141 | |||||||||||
| 142 | 23 | my $jheaders=JSON->new->canonical->encode($headers); | |||||||||
| 143 | 23 | ▶ | if ($jdata) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | { | ||||||||||
| 145 | 5 | $jdata=~s/("identifier":)("[^"]+")/$1"igusername"/g; | |||||||||
| 146 | 5 | $jdata=~s/("password":)("[^"]+")/$1"ig_correct_password"/g; | |||||||||
| 147 | } | ||||||||||
| 148 | 23 | $jheaders=~s/("X-IG-API-KEY":)("[^"]+)"/$1"securitykey"/g; | |||||||||
| 149 | 23 | $jheaders=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g; | |||||||||
| 150 | 23 | $jheaders=~s/("CST":)("[^"]+)"/$1"."/g; | |||||||||
| 151 | |||||||||||
| 152 | 23 | ▶ | $fdebug and print " url=$url\n jdata=$jdata\n jheaders=$jheaders\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | |||||||||||
| 154 | 23 | my $file="$datapath/".md5_hex($url,$jdata,$jheaders).".txt"; | |||||||||
| 155 | 23 | ▶ | $fdebug and print "Reading $file\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 23 | ▶ | open(F,"<",$file) or die "Cannot open file $file for read"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 23 | binmode(F); | |||||||||
| 158 | 23 | read F, $content, 1000000; | |||||||||
| 159 | 23 | close F; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | =head2 responseCode | ||||||||||
| 163 | |||||||||||
| 164 | returns the success response code (200) | ||||||||||
| 165 | |||||||||||
| 166 | =cut | ||||||||||
| 167 | |||||||||||
| 168 | sub responseCode | ||||||||||
| 169 | 21 | ▶ | { | ||||||||
|
Subroutine
responseCode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 170 | 21 | return 200; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | =head2 responseContent | ||||||||||
| 174 | |||||||||||
| 175 | returns the mocked content of the response | ||||||||||
| 176 | |||||||||||
| 177 | =cut | ||||||||||
| 178 | |||||||||||
| 179 | sub responseContent | ||||||||||
| 180 | 18 | ▶ | { | ||||||||
|
Subroutine
responseContent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 181 | 18 | $content=~s/,"headers":\{[^}]+\}\}//; | |||||||||
| 182 | 18 | $content=~s/\{"content"://; | |||||||||
| 183 | 18 | return $content; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | =head2 XSECURITYTOKEN | ||||||||||
| 187 | |||||||||||
| 188 | =cut | ||||||||||
| 189 | |||||||||||
| 190 | sub XSECURITYTOKEN | ||||||||||
| 191 | 0 | ▶ | { | ||||||||
|
Subroutine
XSECURITYTOKEN: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 192 | 0 | die $content; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | =head2 responseHeader | ||||||||||
| 196 | |||||||||||
| 197 | returns the mocked response header. | ||||||||||
| 198 | |||||||||||
| 199 | =cut | ||||||||||
| 200 | |||||||||||
| 201 | sub responseHeader | ||||||||||
| 202 | 10 | ▶ | { | ||||||||
|
Subroutine
responseHeader: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 203 | 10 | my ($self,$header)=@_; | |||||||||
| 204 | 10 | my $c=decode_json($content); | |||||||||
| 205 | 10 | my $headers=$c->{headers}; | |||||||||
| 206 | 10 | return $headers->{$header}; | |||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | =head2 CST | ||||||||||
| 210 | |||||||||||
| 211 | Dummy function. | ||||||||||
| 212 | |||||||||||
| 213 | =cut | ||||||||||
| 214 | |||||||||||
| 215 | sub CST | ||||||||||
| 216 | 0 | ▶ | { | ||||||||
|
Subroutine
CST: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 217 | } | ||||||||||
| 218 | |||||||||||
| 219 | =head2 GET | ||||||||||
| 220 | |||||||||||
| 221 | Dummy function, calls POST | ||||||||||
| 222 | |||||||||||
| 223 | =cut | ||||||||||
| 224 | |||||||||||
| 225 | sub GET | ||||||||||
| 226 | 18 | ▶ | { | ||||||||
|
Subroutine
GET: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 227 | 18 | my ($self,$url,$headers)=@_; | |||||||||
| 228 | 18 | POST($self,$url,undef,$headers); | |||||||||
| 229 | } | ||||||||||
| 230 | 4 | 1; | |||||||||
| 231 | |||||||||||