NAME
LWPx::Record::DataSection - Record/restore LWP response using __DATA__ section
SYNOPSIS
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
DESCRIPTION
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]
...
RECORDING RESPONSES
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.
CLASS METHODS
- LWPx::Record::DataSection->load_data
-
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.Example:
use Test::Requires 'LWPx::Record::DataSection'; LWPx::Record::DataSection->load_data;
OPTIONS
You can specify option when C<< use >> this module.
use LWPx::Record::DataSection %option;
- decode_content => 1 | 0
-
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.
- record_response_header => \@headers | ':all'
-
By default, uncommon headers like "X-Framework" are dropped when recording. Specify this option to record extra headers.
- record_post_param => \@params
-
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
- append_data_section => $ENV{LWPX_RECORD_APPEND_DATA};
-
Automatically record responses to __DATA__ section if not recorded. You can specify this by LWPX_RECORD_APPEND_DATA environment variable.
CAVEATS
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.
AUTHOR
motemen <motemen@gmail.com>
SEE ALSO
Data::Section::Simple, LWP::Protocol
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.