Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend request match with Test::Deep and HTTP::Common #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions lib/Test/LWP/UserAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ sub network_fallback
$network_fallback = $value;
}

sub _match_request
{
my ($self, $request, $request_desc) = @_;

my $uri = $request->uri;

if ($request_desc->$_isa('HTTP::Request'))
{
local $Storable::canonical = 1;
return freeze($request) eq freeze($request_desc);
}

return !! $request_desc->matching ($request)
if $request_desc->$_isa('HTTP::Config');

return Test::Deep::eq_deeply ($request, $request_desc)
if $request_desc->$_isa('Test::Deep::Cmp');

return $uri =~ $request_desc
if __is_regexp($request_desc);

return $request_desc->($request)
if __isa_coderef($request_desc);

$uri = URI->new($uri) unless $uri->$_isa('URI');
return $uri->host eq $request_desc;
}

sub send_request
{
my ($self, $request, $arg, $size) = @_;
Expand All @@ -206,28 +234,10 @@ sub send_request
next if not defined $entry;
my ($request_desc, $response) = @$entry;

if ($request_desc->$_isa('HTTP::Request'))
{
local $Storable::canonical = 1;
$matched_response = $response, last
if freeze($request) eq freeze($request_desc);
}
elsif (__is_regexp($request_desc))
{
$matched_response = $response, last
if $uri =~ $request_desc;
}
elsif (__isa_coderef($request_desc))
{
$matched_response = $response, last
if $request_desc->($request);
}
else
{
$uri = URI->new($uri) if not $uri->$_isa('URI');
$matched_response = $response, last
if $uri->host eq $request_desc;
}
next unless $self->_match_request ($request, $request_desc);

$matched_response = $response;
last;
}

$last_useragent = $self;
Expand Down Expand Up @@ -526,6 +536,30 @@ returns a boolean indicating if there is a match.
The L<HTTP::Request> object is matched identically (including all query
parameters, headers etc) against the provided object.

=item * L<HTTP::Config> object

use HTTP::Config;
my $config = HTTP::Config->new;
$config->add (m_method => 'GET', m_secure => 0);

$test_ua->map_response ($config => HTTP::Response->new ('400'));

See L<HTTP::Config> for details how to match request.

=item * L<Test::Deep> comparison

Applies L<Test::Deep>'s C<eq_deeply> on request to detect match.
It's up to caller to make it available.

use Test::Deep;
# matches any GET requests with query parameter bar=baz
my $match = methods (
method => 'GET',
uri => methods ([query_param => 'bar'] => 'baz'),
),

$test_ua->map_response ($match => HTTP::Response->new('200'));

=back

The response can be represented in multiple ways:
Expand Down
30 changes: 30 additions & 0 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::Deep 0.110;
use Scalar::Util 'refaddr';

use HTTP::Config;

# simulates real code that we are testing
{
package MyApp;
Expand Down Expand Up @@ -154,6 +156,34 @@ cmp_deeply(
'all mappings are now gone', 'GET', 'http://foo', '3000', 'success', { a => 1 },
str('http://foo:3000/success?a=1'), '', '404',
);

$MyApp::useragent->map_response(
methods(method => 'HEAD', uri => re (qr/foo/)),
HTTP::Response->new ('200'),
);

test_send_request(
'Test::Deep comparison / foo', 'HEAD', 'http://foo', '3000', 'fail', { a => 1 },
str('http://foo:3000/fail'), 'a=1', '200', # globally, returning 500
);

$MyApp::useragent->unmap_all;

my $config = HTTP::Config->new;
$config->add (m_method => 'HEAD', _uri => qr/foo/);

$MyApp::useragent->map_response(
$config,
HTTP::Response->new ('201'),
);

test_send_request(
'HTTP::Config comparison / foo', 'HEAD', 'http://foo', '3000', 'fail', { a => 1 },
str('http://foo:3000/fail'), 'a=1', '201', # globally, returning 500
);

$MyApp::useragent->unmap_all;

}

sub test_send_request
Expand Down