You are viewing a plain text version of this content. The canonical link for it is here.
Posted to test-dev@perl.apache.org by Stas Bekman <st...@stason.org> on 2007/10/16 21:08:08 UTC

[patch] Passing an existing request object to shortcuts methods

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: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


Re: [patch] Passing an existing request object to shortcuts methods

Posted by Stas Bekman <st...@stason.org>.
Fred Moyer wrote:
> Stas Bekman wrote:
>> 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?
> 
> There's some great syntactic sugar that make submitting forms relatively 
>  painless.  I've used it in my A::T programs by grabbing the port number 
> from the config hash and used a WWW::Mechanize->new object to do the calls.

Though it bypasses the A-T infrastructure, so if you are trying to rely 
on any of the features A-T provides, you can't. Which is probably OK for 
most people anyway.

-- 
_____________________________________________________________
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


Re: [patch] Passing an existing request object to shortcuts methods

Posted by Fred Moyer <fr...@taperfriendlymusic.org>.
Stas Bekman wrote:
> 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?

There's some great syntactic sugar that make submitting forms relatively 
  painless.  I've used it in my A::T programs by grabbing the port 
number from the config hash and used a WWW::Mechanize->new object to do 
the calls.

Re: [patch] Passing an existing request object to shortcuts methods

Posted by Stas Bekman <st...@stason.org>.
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


Re: [patch] Passing an existing request object to shortcuts methods

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

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/

> 
>     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.

> 
> 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

glad to see you back with us :)

--Geoff