perl-test-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stas Bekman <s...@stason.org>
Subject Re: [patch] Passing an existing request object to shortcuts methods
Date Tue, 16 Oct 2007 20:42:50 GMT
Geoffrey Young wrote:
> 
> Stas Bekman wrote:
>> 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.:
> 
> have you tried mech for this?  I haven't but have always wanted to
> 
>   http://search.cpan.org/dist/WWW-Mechanize/

What difference does it make?

>>     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;
> 
> can you deconstruct $req into it's parts and use them?  ugly, but it may
> save you the full url issues.

Probably. If it's an HTTP::Request object, it should be doable. I just 
wasn't sure whether the idea would be supported at all, before spending 
any more time on it.

Here it is:

Index: lib/Apache/TestRequest.pm
===================================================================
--- lib/Apache/TestRequest.pm   (revision 585204)
+++ lib/Apache/TestRequest.pm   (working copy)
@@ -200,6 +200,13 @@
      my $url = shift;
      Carp::croak("no url passed") unless defined $url;

+    # is a request object?
+    if (ref $url eq 'HTTP::Request') {
+        # dig out the uri portion and resolve it
+        $url->uri(resolve_url($url->uri));
+        return $url;
+    }
+
      return $url if $url =~ m,^(\w+):/,;
      $url = "/$url" unless $url =~ m,^/,;

@@ -473,7 +480,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;




>> 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.
> 
> yeah, that's the thing.  the patch is simple enough, and provided it
> does't break antything it should be fine.  but, really, what is probably
> more useful is for us to figure out how to get the magic of TestRequest
> working well with other interesting LWP classes like mech,
> HTTP::Request::Form, etc

Yup. I'm now trying to make it work with: HTTP::Request::Form

> glad to see you back with us :)

;)


-- 
_____________________________________________________________
Stas Bekman    mailto:stas@stason.org 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


Mime
View raw message