blib/lib/Test/LWP/MockSocket/http.pm

filter: Statementstmt 95.1% 97 / 102 Branchbran 81.2% 13 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 2 totaltotal 89.3% 134 / 150 CC 10 SCAR 7.1
CC
10
Cov
92%
mocksock_mode
6.0
_ensure_response_mode
4.0
_get_response_data
3.0
CRAP 2.0
↑ Summary
1 package Test::LWP::MockSocket::http;
2 #Hack into LWP's socket methods
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::Protocol::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 HT_MOCKSOCK_PERSIST => 1,
12 HT_MOCKSOCK_QUICKIE => 2
13 1 };
14  
15 1 our @EXPORT = qw(
16 $LWP_Response $LWP_SocketArgs
17 mocksock_mode mocksock_response
18 );
19  
20 1 our $VERSION = 0.05;
21 1 our ($LWP_Response, $LWP_SocketArgs);
22  
23 1 my $MODE = HT_MOCKSOCK_PERSIST;
24  
25 26 ▶*LWP::Protocol::http::socket_class = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 26 '_LWP::FakeSocket';
27 1 };
28  
29 0 ▶sub mocksock_mode {
Subroutine mocksock_mode: not called CC=2 SCAR=17.9
Pod undocumented
30 0 my $mode = shift;
31 0 ▶ return $MODE unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 0 0
32 0 $MODE = $MODE;
33 }
34  
35 2 ▶sub mocksock_response {
Subroutine mocksock_response: called CC=2 SCAR=7.1
Pod undocumented
36 2 my $response = shift;
37 2 ▶ return $LWP_Response unless defined $response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 2 2
38 2 $LWP_Response = $response;
39 }
40  
41  
42 ################################################################################
43 ### Private ###
44 ################################################################################
45 1 my $RESPONSE_BUF;
46 my $REQDATA; #I don't always use the same conventions for mutables, especially in
47 #such horrible hacks like this
48  
49 1 my $SEND_REQUEST_DONE = 0;
50 1 my $RESPBYTES_SENT = 0;
51  
52 26 ▶sub _add_reqdata {
Subroutine _add_reqdata: called CC=1 SCAR=0.0
53 26 my (undef, $buf) = @_;
54 26 $REQDATA .= $buf;
55 }
56  
57 26 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=0.0
58 26 $REQDATA = "";
59
60 #The following needs to be true in order for can_read to not fail
61 #before the initial sysread.
62 26 $RESPONSE_BUF = "DUMMY";
63
64 26 $SEND_REQUEST_DONE = 0;
65 26 $RESPBYTES_SENT = 0;
66 }
67  
68 52 ▶sub _ensure_response_mode {
Subroutine _ensure_response_mode: called CC=4 SCAR=13.9
69 52 ▶ return unless !$SEND_REQUEST_DONE;
Branch: 1 branch
BranchTrueFalseTotal
if $SEND_REQUEST_DONE 26 26 52
70 26 my $reftype = ref $LWP_Response;
71 26 ▶ if($reftype eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'CODE') { } 2 24 26
elsif ($reftype eq 'ARRAY') { } 2 22 24
72 2 my $req = HTTP::Request->parse($REQDATA);
73 2 $RESPONSE_BUF = $LWP_Response->($REQDATA, $req, $LWP_SocketArgs);
74 } elsif ($reftype eq 'ARRAY') {
75 2 $RESPONSE_BUF = shift @{$LWP_Response};
76 } else {
77 22 $RESPONSE_BUF = $LWP_Response;
78 }
79 26 $SEND_REQUEST_DONE = 1;
80 }
81  
82 52 ▶sub _get_response_data {
Subroutine _get_response_data: called CC=3 SCAR=11.0
83 52 my (undef, $buf,$length) = @_;
84 52 _ensure_response_mode();
85 52 my $remaining_length = length($RESPONSE_BUF);
86 52 ▶ $length = $remaining_length if $length > $remaining_length;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $remaining_length 30 22 52
87 52 my $blob = substr($RESPONSE_BUF, $RESPBYTES_SENT, $length);
88 52 ▶ if(!$blob) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blob) 6 46 52
89 #No data left. Maybe ConnCache is checking to see if we're still alive.
90 #If we set this to -1, can_read will return false, and it will force the
91 #creation of a new socket.
92 6 $RESPBYTES_SENT = -1;
93 6 return 0;
94 }
95 46 $_[1] = $blob;
96 46 $RESPBYTES_SENT += $length;
97 46 return length($blob);
98 }
99  
100 package _LWP::FakeSocket;
101 1 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use base qw(IO::String);
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 Test::LWP::MockSocket::http->import();
107  
108 1 my $mock = 'Test::LWP::MockSocket::http';
109  
110 1 my $n_passed = 0;
111 1 our $AUTOLOAD;
112 307 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
113 307 my $self = shift;
114 307 my ($fn_name) = (split(/::/, $AUTOLOAD))[-1];
115 307 my $meth = Net::HTTP::Methods->can($fn_name);
116 307 ▶ if(!$meth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth) 53 254 307
117 53 return;
118 }
119 254 return $meth->($self, @_);
120 }
121  
122 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
123 26 $mock->_initialize();
124 26 my ($cls,%opts) = @_;
125 26 $LWP_SocketArgs = \%opts;
126 26 my $self = IO::String->new();
127 26 bless $self, __PACKAGE__;
128 26 return $self;
129 }
130  
131 59 ▶sub can_read {
Subroutine can_read: called CC=1 SCAR=0.0
132 59 $RESPONSE_BUF;
133 }
134  
135 0 ▶sub configure {
Subroutine configure: not called CC=1 SCAR=6.9
136 0 my $self = $_[0];
137 #log_err("Configure Called!");
138 0 return $self;
139 }
140  
141 26 ▶sub syswrite {
Subroutine syswrite: called CC=2 SCAR=7.4
142 #We do some hackery here..
143 26 my ($self,$buf,$length) = @_;
144 26 ▶ $length ||= length($buf);
Condition33%1/3
$length ||= length $buf
l!l&&r!l&&!r
2600
MC/DC0%0/2
$length ||= length $buf
$length length $buf
Truth table33%1/3
A ||= B
A= $lengthB= length $buf
ABresult
1X1
011
000
145 26 $mock->_add_reqdata($buf);
146 26 return $length;
147 }
148  
149 52 ▶sub sysread {
Subroutine sysread: called CC=1 SCAR=0.0
150 52 return $mock->_get_response_data($_[1], $_[2]);
151 }
152  
153 1 0xb00b135;
154  
155 =head1 NAME
156  
157 Test::LWP::MockSocket::http - Inject arbitrary data as socket data for LWP::UserAgent
158  
159 =head1 SYNOPSIS
160  
161 use Test::LWP::MockSocket::http;
162 use LWP::UserAgent;
163 # $LWP_Response is exported by this module
164 $LWP_Response = "HTTP/1.0 200 OK\r\n\r\nSome Response Text";
165 my $ua = LWP::UserAgent->new();
166 $ua->proxy("http", "http://1.2.3.4:56");
167 my $http_response = $ua->get("http://www.foo.com/bar.html");
168
169 $http_response->code; #200
170 $http_response->content; # "Some response text"
171 $LWP_SocketArgs->{PeerAddr} # '1.2.3.4'
172  
173 =head1 DESCRIPTION
174  
175 This module, when loaded, mangles some functions in L<LWP::Protocol::http>
176 which will emulate a real socket. LWP is used as normally as much as possible.
177  
178 Effort has been made to maintain the exact behavior of L<Net::HTTP> and L<LWP::Protocol::http>.
179  
180 Two variables are exported, C<$LWP_Response> which should contain raw HTTP 'data',
181 and $LWP_SocketArgs which contains a hashref passed to the socket's C<new> constructor.
182 This is helpful for debugging complex LWP::UserAgent subclasses (or wrappers) which
183 modify possible connection settings.
184  
185 =head2 EXPORTED SYMBOLS
186  
187 Following the inspiration of L<Test::Mock::LWP>, two package variables will nicely
188 invade your namespace; they are C<$LWP_Response> which contains a 'response thingy'
189 (see below) and C<$LWP_SocketArgs> which contains a hashref of options that LWP
190 thought it would pass to L<IO::Socket::INET> or L<IO::Socket::SSL>.
191  
192 In addition, you can use C<mocksock_response> as an accessor to the C<$LWP_Response>,
193 if you absolutely must.
194  
195 =head2 RESPONSE VARIABLE
196  
197 It was mentioned that C<$LWP_Response> is a 'thingy', and this is because it can
198 be three things:
199  
200 =over
201  
202 =item Scalar
203  
204 This is the simplest way to use this module, and it will simply copy the contents
205 of the scalar verbatim into LWP's read buffers.
206  
207 =item Array Reference
208  
209 This functions like the Scalar model, except that it will cycle through each of the
210 elements in the array for each request, exhausting them - I don't know what happens
211 if you overrun the array - and your test code really shouldn't be doing anything that
212 causes it anyway.
213  
214 =item Code Reference
215  
216 This is the most entertaining of the three. The handler is called with three
217 arguments. The first is the raw request data as received from LWP's serialization
218 methods. The second is an L<HTTP::Request> object which is pretty much just there
219 for your convenience (this is a test module, the more information, the better, and
220 performance is not a big issue), and the last is the socket options found in
221 C<$LWP_SocketArgs>, again, for convenience.
222  
223 =back
224  
225 =head1 CAVEATS/BUGS
226  
227 Probably many. This relies on mainly undocumented behavior and features of LWP
228 and is likely to break. In particular, the module test tries to ensure
229 that the mock socket works together with L<LWP::ConnCache>.
230  
231 Depending on how LWP handles POST requests and other, perhaps more exotic requests,
232 this module might break. Then again, if you find a need to use this module in the
233 first place, you probably Know What You Are Doing(TM).
234  
235 =head2 RATIONALE
236  
237 I wrote this for testing code which used LWP and its
238 subclasses heavily, but still desired the full functionality of LWP::UserAgent
239 (if you look closely enough, you will see that the same L<HTTP::Request> object which
240 is passed to LWP is not the actual one sent on the wire, and the L<HTTP::Response>
241 object returned by LWP methods is not the same one received on the wire).
242  
243 =head1 ACKNOWLEDGEMENTS
244  
245 Thanks to mst for helping me with the difficult task of selecting the module name
246  
247 =head1 AUTHOR AND COPYRIGHT
248  
249 Copyright 2011 M. Nunberg
250  
251 You may use and distribute this software under the terms of the GNU General Public
252 License Version 2 or higher.
↑ Summary