1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
use strict;
use warnings;
use Test::More 0.88;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::Deep;
use Path::Tiny;
use Test::LWP::UserAgent;
# the root problem here was that the real send_request calls LWP::Protocol::*
# with all the arguments, some of which are then processed via the collect()
# method -- including the option to save the content to a file.
# I thought about creating a new LWP::Protocol subclass, which did the heavy
# lifting that is in my send_request, but that's overboard, even for me... and
# after looking at LWP::Protocol::http::request, all it does after handling
# the networking itself is call $self->collect with all the args.
{
# internally-mapped responses
my $useragent = Test::LWP::UserAgent->new;
$useragent->map_response(
qr/foo.com/,
HTTP::Response->new(
200, 'OK',
['Content-Type' => 'text/plain'], 'all good!',
),
);
my $tmpfile = Path::Tiny->tempfile;
my $response = $useragent->get(
'http://foo.com',
':content_file' => $tmpfile->stringify,
);
my $contents = $tmpfile->slurp_utf8;
is($contents, 'all good!', 'response body is saved to file (internal responses)');
is($response->content, '', 'response body is removed');
cmp_deeply(
$response,
methods(
[ header => 'X-Died' ] => undef,
[ header => 'Content-Type' ], => 'text/plain',
[ header => 'Client-Date' ] => ignore,
),
'response headers look ok',
);
}
{
# and another, using mirror() directly
my $useragent = Test::LWP::UserAgent->new;
$useragent->map_response(
qr/foo.com/,
HTTP::Response->new(
200, 'OK',
['Content-Type' => 'text/plain'], 'all good!',
),
);
my $tmpfile = Path::Tiny->tempfile;
my $response = $useragent->mirror('http://foo.com', $tmpfile->stringify);
my $contents = $tmpfile->slurp_utf8;
is($contents, 'all good!', 'response body is saved to file (internal responses)');
is($response->content, '', 'response body is removed');
cmp_deeply(
$response,
methods(
[ header => 'X-Died' ] => undef,
[ header => 'Content-Type' ], => 'text/plain',
[ header => 'Client-Date' ] => ignore,
),
'response headers look ok',
);
}
done_testing;
|