Skip Menu |

This queue is for tickets about the Test-Apache2 CPAN distribution.

Report information
The Basics
Id: 50931
Status: resolved
Priority: 0/
Queue: Test-Apache2

People
Owner: KZYS [...] cpan.org
Requestors: ilmari+cpan [...] ilmari.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Assumes method handler
Date: Wed, 28 Oct 2009 20:26:37 +0000
To: bug-Test-Apache2 [...] rt.cpan.org
From: ilmari [...] ilmari.org (Dagfinn Ilmari Mannsåker)
According to the mod_perl documentation[1], handler() should be invoked as a method only when it has the "method" attribute. Attached is a patch that implements this, with tests (and the existing tests adjusted accordingly).
commit ea913b9808f0b4c6110ce6faf3d98d402cadf2ec Author: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> Date: Wed Oct 28 20:19:31 2009 +0000 Handle non-method handler() diff --git a/lib/Test/Apache2/Server.pm b/lib/Test/Apache2/Server.pm index 7999cee..140d2fa 100644 --- a/lib/Test/Apache2/Server.pm +++ b/lib/Test/Apache2/Server.pm @@ -6,6 +6,7 @@ __PACKAGE__->mk_accessors(qw(host)); use Test::Apache2::RequestRec; use HTTP::Response; +use attributes (); sub new { my ($class, @args) = @_; @@ -67,7 +68,13 @@ sub _request { { local *STDOUT; open STDOUT, '>', \$buffer; - $class->handler($req); + my $handler = $class->can('handler'); + if (grep { $_ eq 'method' } attributes::get($handler)) { + $class->$handler($req); + } + else { + $handler->($req); + } } if ($buffer) { diff --git a/t/FooHandler.pm b/t/FooHandler.pm index dcc28ba..980a21a 100644 --- a/t/FooHandler.pm +++ b/t/FooHandler.pm @@ -3,7 +3,7 @@ use strict; use warnings; use Apache2::Const; -sub handler { +sub handler :method { my ($class_or_obj, $req) = @_; $req->content_type('text/plain'); diff --git a/t/NonMethodHandler.pm b/t/NonMethodHandler.pm new file mode 100644 index 0000000..6d4e3ee --- /dev/null +++ b/t/NonMethodHandler.pm @@ -0,0 +1,13 @@ +package t::NonMethodHandler; +use strict; +use warnings; +use Apache2::Const; + +sub handler { + my ($req) = @_; + $req->content_type('text/plain'); + $req->print('not a method'); + return Apache2::Const::OK; +} + +1; diff --git a/t/Util.pm b/t/Util.pm index d1d038a..57c0d22 100644 --- a/t/Util.pm +++ b/t/Util.pm @@ -24,7 +24,7 @@ use strict; use warnings; our $HANDLER; -sub handler { +sub handler :method { $HANDLER->(@_); }
-- ilmari "A disappointingly low fraction of the human race is, at any given time, on fire." - Stig Sandbeck Mathisen
I forgot to include the new test script in the previous patch, here's an updated one.
commit 394db09a94c73090413f82182282866f9a673652 Author: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> Date: 2009-10-29 10:19:47 +0000 Handle non-method handlers diff --git a/lib/Test/Apache2/Server.pm b/lib/Test/Apache2/Server.pm index 7999cee..140d2fa 100644 --- a/lib/Test/Apache2/Server.pm +++ b/lib/Test/Apache2/Server.pm @@ -6,6 +6,7 @@ __PACKAGE__->mk_accessors(qw(host)); use Test::Apache2::RequestRec; use HTTP::Response; +use attributes (); sub new { my ($class, @args) = @_; @@ -67,7 +68,13 @@ sub _request { { local *STDOUT; open STDOUT, '>', \$buffer; - $class->handler($req); + my $handler = $class->can('handler'); + if (grep { $_ eq 'method' } attributes::get($handler)) { + $class->$handler($req); + } + else { + $handler->($req); + } } if ($buffer) { diff --git a/t/FooHandler.pm b/t/FooHandler.pm index dcc28ba..980a21a 100644 --- a/t/FooHandler.pm +++ b/t/FooHandler.pm @@ -3,7 +3,7 @@ use strict; use warnings; use Apache2::Const; -sub handler { +sub handler :method { my ($class_or_obj, $req) = @_; $req->content_type('text/plain'); diff --git a/t/NonMethodHandler.pm b/t/NonMethodHandler.pm new file mode 100644 index 0000000..6d4e3ee --- /dev/null +++ b/t/NonMethodHandler.pm @@ -0,0 +1,13 @@ +package t::NonMethodHandler; +use strict; +use warnings; +use Apache2::Const; + +sub handler { + my ($req) = @_; + $req->content_type('text/plain'); + $req->print('not a method'); + return Apache2::Const::OK; +} + +1; diff --git a/t/Util.pm b/t/Util.pm index d1d038a..57c0d22 100644 --- a/t/Util.pm +++ b/t/Util.pm @@ -24,7 +24,7 @@ use strict; use warnings; our $HANDLER; -sub handler { +sub handler :method { $HANDLER->(@_); } diff --git a/t/nonmethod.t b/t/nonmethod.t new file mode 100644 index 0000000..d990572 --- /dev/null +++ b/t/nonmethod.t @@ -0,0 +1,22 @@ +use strict; +use warnings; +use Test::More tests => 4; +use t::NonMethodHandler; + +use_ok 'Test::Apache2::Server'; + +my $server = Test::Apache2::Server->new; +ok($server, 'new'); + +$server->location('/foo', { + PerlResponseHandler => 't::NonMethodHandler', + PerlSetVar => [ Key => 'Value' ], +}); + +my $resp = $server->get('/foo'); +isa_ok($resp, 'HTTP::Response'); +is( + $resp->content, + 'not a method', + 'response body' +);
I've applied the patch and released 0.05. Thank you!