I was trying to get GET, POST and other Apache::TestRequest methods to
accept a pre-existing HTTP::Request object, rather than pass arguments
that will construct one.
The reason, I was trying to use HTML::TreeBuilder and
HTTP::Request::Form to fill-out a form and submit that, rather than raw
POST, e.g.:
my $content = get_content($url);
my $tree = HTML::TreeBuilder->new;
$tree->parse($content);
$tree->eof();
my @forms = $tree->find_by_tag_name('form');
die "What, no forms in $url?" unless @forms;
my $f = HTTP::Request::Form->new($forms[0], $url);
$f->field("foo", $foo);
$f->field("bar", $bar);
my $req = $f->press();
and now
POST $req;
With the patch below the basic thing works. The issues I've encountered
so far:
- requiring full url, since Apache::TestRequest won't expand it for you)
- redirects aren't handled
- doesn't seem to work with shortcuts like POST_BODY
The patch simply gives another functionality to the $url argument to any
of the public methods in Apache::TestRequest, to allow an object to be
passed in addition to the string url.
I'm not sure whether it's worth trying to add this functionality, or
just have the test create its own $UA.
Index: lib/Apache/TestRequest.pm
===================================================================
--- lib/Apache/TestRequest.pm (revision 585204)
+++ lib/Apache/TestRequest.pm (working copy)
@@ -200,6 +200,9 @@
my $url = shift;
Carp::croak("no url passed") unless defined $url;
+ # is a request object?
+ return $url if ref $url;
+
return $url if $url =~ m,^(\w+):/,;
$url = "/$url" unless $url =~ m,^/,;
@@ -473,7 +476,7 @@
sub lwp_call {
my($name, $shortcut) = (shift, shift);
- my $r = (\&{$name})->(@_);
+ my $r = ($_[0] && ref $_[0]) ? $_[0] : (\&{$name})->(@_);
Carp::croak("$name(@_) didn't return a response object") unless $r;
--
_____________________________________________________________
Stas Bekman mailto:[EMAIL PROTECTED] http://stason.org/
http://www.linkedin.com/in/stasbekman http://stasosphere.com/
http://stason.org/photos/gallery/ http://healingcloud.com
http://chestofbooks.com/ http://modperlbook.org/
http://modperl2book.org