LWPx::Record::DataSection - Record/restore LWP response using __DATA__ section
use Test::More; use LWPx::Record::DataSection; use LWP::Simple qw($ua); my $res = $ua->get('http://www.example.com/'); # does not access to the internet actually is $res->code, 200; __DATA__ @@ GET http://www.example.com/ HTTP/1.0 200 OK Content-Type: text/html ... # HTTP response
LWPx::Record::DataSection overrides LWP::Protocol and creates response object from __DATA__ section. The response should be recorded as below:
__DATA__ @@ [method] [url] [raw response] @@ [method] [url] [raw response] ...
When LWP try to send request without corresponding data section, LWPx::Record::DataSection allows actual connection and records the response to the test file's __DATA__ section.
Example:
# test.t use strict; use Test::More; use LWPx::Record::DataSection; use LWP::Simple qw($ua); my $res = $ua->get('http://www.example.com/'); is $res->code, 200; # No __END__ please, LWPx::Record::DataSection confuses __DATA__
Running this test with environment variable LWPX_RECORD_APPEND_DATA=1 appends the actual response to the test file itself, thus produces such:
# test.t use strict; use Test::More; use LWPx::Record::DataSection; use LWP::Simple qw($ua); my $res = $ua->get('http://www.example.com/'); is $res->code, 200; # No __END__ please, LWPx::Record::DataSection confuses __DATA__ @@ GET http://www.example.com/ HTTP/1.0 302 Found Connection: Keep-Alive Location: http://www.iana.org/domains/example/ ... @@ GET http://www.iana.org/domains/example/ HTTP/1.1 200 OK ...
After that running the test does not require internet connection.
Load __DATA__ section into $LWPx::Record::DataSection::Data. LWPx::Record::DataSection->import implies this, so if you do not use this module, explicitly call this.
use
use Test::Requires 'LWPx::Record::DataSection'; LWPx::Record::DataSection->load_data;
You can specify option when C<< use >> this module. use LWPx::Record::DataSection %option;
By default, responses are recorded as decoded so that you will not see unreadable bytes in your file. If this behavior is not desired, turn this option off.
By default, uncommon headers like "X-Framework" are dropped when recording. Specify this option to record extra headers.
Use POSTed parameters as extra key. Post keys are recorded as:
@@ POST http://localhost/ Post:foo=1,foo=2
By default, only request method and request uri are used to identify request. Specify this option to use certain cookie as key. Cookie keys are recorded as:
@@ GET http://localhost/ Cookie:foo=1,bar=2
Automatically record responses to __DATA__ section if not recorded. You can specify this by LWPX_RECORD_APPEND_DATA environment variable.
If the file contains __END__ section, storing response will not work.
LWPx::Record::DataSection appends __DATA__ section only files that directly use this module. This is to avoid accidents.
motemen <motemen@gmail.com>
Data::Section::Simple, LWP::Protocol
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install LWPx::Record::DataSection, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LWPx::Record::DataSection
CPAN shell
perl -MCPAN -e shell install LWPx::Record::DataSection
For more information on module installation, please visit the detailed CPAN module installation guide.