Test::VCR::LWP - Record and playback LWP interactions.
version 0.5
withVCR { my $res = $useragent->get('http://metacpan.org/'); };
Records HTTP transactions done thru LWP to a file and then replays them. Allows your tests suite to be fast, dterministic and accurate.
Inspired by (stolen from) http://relishapp.com/vcr/vcr/docs
You can use the object oriented interface, but the basic decorator style function interface is recommended.
Using the OO interface:
my $vcr = Test::VCR::LWP->new(tape => 'mytape.tape'); $vcr->run(sub { my $ua = LWP::UserAgent->new; my $res = $ua->get('http://www.perl.org/'); if ($_->is_recording) { do_something(); } });
Mocks out any calls to LWP::UserAgent with Test::VCR::LWP. Takes a number of flags which are passed to the VCR constructor, and finally a code ref. For example:
withVCR { my $req = $ua->post('http://oo.com/object'); isa_ok($req, 'HTTP::Response'); if ($_->is_recording) { sleep(5); } my $second = $ua->get('http://oo.com/object/' . $res->id); } tape => 'my_test.tape';
Or to have the name of the calling sub define the tape name:
withVCR { my $req = $ua->post('http://oo.com/object'); isa_ok($req, 'HTTP::Response'); };
The tape file we be placed in the same directory as the script if no tape argument is given. If this function is called outside of a subroutine, the filename of the current perl script will be used to derive a tape filename.
Within the code block, $_ is the current vcr object. The is_recording method might be useful.
$_
is_recording
Allows a section of a withVCR code block to skip recording.
withVCR { my $req = $ua->post('http://oo.com/object'); isa_ok($req, 'HTTP::Response'); withoutVCR { # this will not end up in the tape $ua->post('http://always.com/dothis'); }; };
The docs are pretty middling at the moment.
Chris Reinhardt crein@cpan.org Mark Ng cpan@markng.co.uk
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
LWP::UserAgent, perl(1)
To install Test::VCR::LWP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::VCR::LWP
CPAN shell
perl -MCPAN -e shell install Test::VCR::LWP
For more information on module installation, please visit the detailed CPAN module installation guide.