Record and playback LWP interactions.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/Test/VCR
t
.gitignore
.travis.yml
Changes
README.pod
dist.ini

README.pod

NAME

Test::VCR::LWP - Record and playback LWP interactions.

VERSION

version 0.5

SYNOPSIS

withVCR {
        my $res = $useragent->get('http://metacpan.org/');
};

DESCRIPTION

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

OO Interface

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();
        }
});

withVCR

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.

withoutVCR

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');
        };
};

TODO

  • The docs are pretty middling at the moment.

AUTHORS

Chris Reinhardt
crein@cpan.org

Mark Ng
cpan@markng.co.uk   

COPYRIGHT

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.

SEE ALSO

LWP::UserAgent, perl(1)