From 9912305ac09411c49413fbf1364cfe209ac04adc Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sat, 9 Mar 2019 12:30:04 -0700 Subject: [PATCH] fix transform in void context, Middleware::Runtime --- lib/HTTP/AnyUA/Middleware.pm | 2 +- lib/HTTP/AnyUA/Middleware/Runtime.pm | 2 +- t/40-middleware-runtime.t | 20 ++++++++++++++++++-- t/lib/MockBackend.pm | 5 +++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/HTTP/AnyUA/Middleware.pm b/lib/HTTP/AnyUA/Middleware.pm index 01dda30..5f5359b 100644 --- a/lib/HTTP/AnyUA/Middleware.pm +++ b/lib/HTTP/AnyUA/Middleware.pm @@ -24,7 +24,7 @@ package HTTP::AnyUA::Middleware; }; if ($self->response_is_future) { - $response->transform( + $response = $response->transform( done => $handle_response, fail => $handle_response, ); diff --git a/lib/HTTP/AnyUA/Middleware/Runtime.pm b/lib/HTTP/AnyUA/Middleware/Runtime.pm index ed1643c..9021e4f 100644 --- a/lib/HTTP/AnyUA/Middleware/Runtime.pm +++ b/lib/HTTP/AnyUA/Middleware/Runtime.pm @@ -44,7 +44,7 @@ sub request { }; if ($self->response_is_future) { - $resp->transform( + $resp = $resp->transform( done => $handle_response, fail => $handle_response, ); diff --git a/t/40-middleware-runtime.t b/t/40-middleware-runtime.t index 5597fda..594555e 100644 --- a/t/40-middleware-runtime.t +++ b/t/40-middleware-runtime.t @@ -5,8 +5,9 @@ use strict; use lib 't/lib'; +use Future; use HTTP::AnyUA; -use Test::More tests => 1; +use Test::More tests => 2; HTTP::AnyUA->register_backend(Mock => '+MockBackend'); @@ -16,8 +17,23 @@ my $backend = $any_ua->backend; $any_ua->apply_middleware('Runtime'); my $url = 'http://acme.tld/'; - +my $mock_response = { + success => 1, + status => 200, + reason => 'OK', + content => 'whatever', +}; + +$backend->response({%$mock_response}); my $resp = $any_ua->get($url); note explain $resp; isnt $resp->{runtime}, undef, 'runtime is defined'; +$backend->response(Future->done({%$mock_response})); +$resp = $any_ua->get($url); +$resp->on_done(sub { + my $resp = shift; + note explain $resp; + isnt $resp->{runtime}, undef, 'runtime is defined when response is future'; +}); + diff --git a/t/lib/MockBackend.pm b/t/lib/MockBackend.pm index c8b6535..36f13e0 100644 --- a/t/lib/MockBackend.pm +++ b/t/lib/MockBackend.pm @@ -4,6 +4,9 @@ package MockBackend; use warnings; use strict; +use Scalar::Util qw(blessed); +use namespace::clean; + use parent 'HTTP::AnyUA::Backend'; @@ -28,6 +31,8 @@ Get the requests the backend has handled so far. sub requests { @{$_[0]->{requests} || []} } +sub response_is_future { blessed($_[0]->{response}) && $_[0]->{response}->isa('Future') } + sub request { my $self = shift; -- 2.45.2