You are viewing a plain text version of this content. The canonical link for it is here.
Posted to test-dev@httpd.apache.org by Boris Zentner <bz...@2bz.de> on 2004/07/27 23:15:27 UTC

cookie_jar ignored with requests_redirectable => 1

Hi,
recently I try to use Apache::Test with HTTP::Cookies. But it did not work as 
expected. I suppose the way to add a cookie_jar to A::T was with 
'Apache::TestRequest::user_agent'. I read the docs from 
Apache::TestRequest::user_agent. Here is the relevant part of it.

       And finally, the semantics of the "requests_redirectable"
       parameter is different than for "LWP::UserAgent": It
       either follows redirects for a request, or it doesn't.
       Thus "requests_redirectable" is a boolean value instead of
       the array reference that "LWP::UserAgent" expects. To

This implies to me that I have the choice to enable or disable redirects for 
this useragent with a bool value.

Apache::TestRequest::user_agent(
  reset => 1, 
  cookie_jar => $cookie_jar, 
  requests_redirectable => 1 
);

But this way the cookies are ignored. I expected that 
HTTP::Cookies->extract_cookies is called after every request. Therefor I 
create the cookie_jar from

package My::Cookies;
use base 'HTTP::Cookies';
sub extract_cookies {
  warn "extract_cookies!!!";
  shift->SUPER::extract_cookies(@_);
}

To get it work, I need to parse the cookie headers myself or 

Apache::TestRequest::user_agent(
  reset => 1, 
  cookie_jar => $cookie_jar, 
  requests_redirectable => 0 
);

But here I need to redirect myself or do it with the undocumented 

Apache::TestRequest::user_agent(
  reset => 1, 
  cookie_jar => $cookie_jar, 
  requests_redirectable => [qw~x y~]
);

that does anything I want but is undocumented!

Here is a part from Apache::TestRequest::user_agent that looks wrong to me.

        my $redir = $args->{requests_redirectable};
        if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
            $RedirectOK = 1;
        } else {
            $RedirectOK = 0;
        }

##############################
And a test script.

use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestUtil;
use Apache::TestRequest qw'GET POST';
plan tests => 3, have 'LWP';
require HTTP::Cookies;
require HTML::Form;
use Data::Dumper;

package My::Cookies;
use base 'HTTP::Cookies';
sub extract_cookies {
  warn "extract_cookies!!!";
  shift->SUPER::extract_cookies(@_);
}
package main;
my $cookie_jar = My::Cookies->new;
Apache::TestRequest::user_agent(
  reset      => 1, 
  cookie_jar => $cookie_jar, 
  requests_redirectable => [qw/c d/]
); 

# check if we can request a page
my $r = GET '/x';
ok t_cmp( $r->code, 200, '$r->code == HTTP_OK?');
ok t_cmp( qr:\Qnew account:, $r->content, "new account page" );
$r = POST '/y', [ 
  email => 'bzm@2bz.de', 
  login => 'boris16', 
];
ok t_cmp( $r->code, 200, '$r->code == HTTP_OK?');

-- 
Boris

Re: cookie_jar ignored with requests_redirectable => 1

Posted by David Wheeler <da...@kineticode.com>.
On Jul 30, 2004, at 6:30 AM, Boris Zentner wrote:

> So I think if LWP is used, instead of passing requests_redirectable to 
> LWP,
> the 1 should be changed to [ qw/GET POST HEAD/ ] or propably more. 
> This patch
> passwd all my tests.

Yes, of course you're right. It has just been so long since I looked at 
this stuff. I've committed your patch (modulo some style edits) and 
added some documentation on passing an array reference to 
requests_redirectable.

Regards,

David

Re: cookie_jar ignored with requests_redirectable => 1

Posted by Boris Zentner <bz...@2bz.de>.
Hi David,

Am Donnerstag 29 Juli 2004 23:07 schrieb David Wheeler:
> On Jul 28, 2004, at 1:29 AM, Stas Bekman wrote:
> > Boris Zentner wrote:
> >> Hi,
> >> recently I try to use Apache::Test with HTTP::Cookies. But it did not
[...]
> >> HTTP::Cookies->extract_cookies is called after every request.
> >> Therefor I create the cookie_jar from
>
> Oops.
>
> >> package My::Cookies;
> >> use base 'HTTP::Cookies';
> >> sub extract_cookies {
> >>   warn "extract_cookies!!!";
> >>   shift->SUPER::extract_cookies(@_);
> >> }
> >> To get it work, I need to parse the cookie headers myself or
> >> Apache::TestRequest::user_agent(
> >>   reset => 1,   cookie_jar => $cookie_jar,   requests_redirectable =>
> >> 0 );
> >> But here I need to redirect myself or do it with the undocumented
> >> Apache::TestRequest::user_agent(
> >>   reset => 1,   cookie_jar => $cookie_jar,   requests_redirectable =>
> >> [qw~x y~]
> >> );
> >> that does anything I want but is undocumented!
> >> Here is a part from Apache::TestRequest::user_agent that looks wrong
> >> to me.
> >>         my $redir = $args->{requests_redirectable};
> >>         if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
> >>             $RedirectOK = 1;
> >>         } else {
> >>             $RedirectOK = 0;
> >>         }
>
> Does this address the issue?
>
> --- TestRequest.pm.~1.96.~	Thu May  6 12:11:33 2004
> +++ TestRequest.pm	Thu Jul 29 14:03:58 2004
> @@ -115,7 +115,7 @@
>
>       if (exists $args->{requests_redirectable}) {
>           my $redir = $args->{requests_redirectable};
> -        if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
> +        if ((ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) or
> $redir) {
>               $RedirectOK = 1;
>           } else {
>               $RedirectOK = 0;
>


No, it is not enough.

The problem is ( at least in my case ) where I use LWP. My test start with

  plan tests => 6, have 'LWP';

later I Add a cookie_jar with

  Apache::TestRequest::user_agent( reset      => 1,
                                   cookie_jar => $cookie_jar,
  );

this works fine.

But the docs from Aapche::TestRequest

      And finally, the semantics of the "requests_redirectable"
       parameter is different than for "LWP::UserAgent": It
       either follows redirects for a request, or it doesn't.
       Thus "requests_redirectable" is a boolean value instead of
       the array reference that "LWP::UserAgent" expects. To
       force "Apache::TestRequest" not to follow redirects in any
       of its convenience functions, pass a false value to
       "requests_redirectable":

This implies to me, that I have the choice to enable or disable
"requests_redirectable" with a boolean value. I choice to enable it with

  Apache::TestRequest::user_agent( reset      => 1,
                                   cookie_jar => $cookie_jar,
				   requests_redirectable => 1
  );

and it does not work anymore. I think this is since I use LWP and the 
requests_redirectable is passwd directly to LWP::UserAgent. But 
requests_redirectable in LWP need a 0 to disable requests_redirectable or a 
arrayref, where the redirects explicite allowed. For example:

  requests_redirectable => [ qw/GET POST HEAD/ ];

the default form LWP is [ qw/GET HEAD/ ];

So I think if LWP is used, instead of passing requests_redirectable to LWP, 
the 1 should be changed to [ qw/GET POST HEAD/ ] or propably more. This patch 
passwd all my tests.

--- a/Apache-Test-1.12/lib/Apache/TestRequest.pm        2004-05-06 
21:11:33.000000000 +0200
+++ b/Apache-Test-1.12/lib/Apache/TestRequest.pm        2004-07-30 
15:27:28.686759440 +0200
@@ -117,7 +117,12 @@
         my $redir = $args->{requests_redirectable};
         if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
             $RedirectOK = 1;
-        } else {
+        }
+       elsif ( $redir ) {
+           $args->{requests_redirectable} = [ qw/GET HEAD POST/ ] if 
$have_lwp;
+           $RedirectOK = 1;
+       }
+       else {
             $RedirectOK = 0;
         }
     }






> I think that I might have changed this code to work this way, and
> overlooked that it could be passed as a simple boolean, even though
> that's the _only_ way it worked before I got my hands on it.
>
> So does passing the array reference actually affect the way the LWP
> user agent object operates? If so, that's cool, but it's undocumented.
> I'm not sure whether it should be documented, though, since if LWP
> isn't installed it certainly won't work with the simple request
> interface that Apache::TestRequest uses in its place.
>
> Regards,
>
> David

-- 
Boris

Re: cookie_jar ignored with requests_redirectable => 1

Posted by Stas Bekman <st...@stason.org>.
David Wheeler wrote:
[...]
> So does passing the array reference actually affect the way the LWP user 
> agent object operates? If so, that's cool, but it's undocumented. I'm 
> not sure whether it should be documented, though, since if LWP isn't 
> installed it certainly won't work with the simple request interface that 
> Apache::TestRequest uses in its place.

if you want to rely on LWP-specific features, you always need to have:

   plan tests => $tests, have_lwp;

in your client tests. So the documentation should mention that requirement.

Thanks.


-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Re: cookie_jar ignored with requests_redirectable => 1

Posted by David Wheeler <da...@kineticode.com>.
On Jul 28, 2004, at 1:29 AM, Stas Bekman wrote:

> Boris Zentner wrote:
>> Hi,
>> recently I try to use Apache::Test with HTTP::Cookies. But it did not 
>> work as expected. I suppose the way to add a cookie_jar to A::T was 
>> with 'Apache::TestRequest::user_agent'. I read the docs from 
>> Apache::TestRequest::user_agent. Here is the relevant part of it.
>>        And finally, the semantics of the "requests_redirectable"
>>        parameter is different than for "LWP::UserAgent": It
>>        either follows redirects for a request, or it doesn't.
>>        Thus "requests_redirectable" is a boolean value instead of
>>        the array reference that "LWP::UserAgent" expects. To
>> This implies to me that I have the choice to enable or disable 
>> redirects for this useragent with a bool value.
>> Apache::TestRequest::user_agent(
>>   reset => 1,   cookie_jar => $cookie_jar,   requests_redirectable => 
>> 1 );
>> But this way the cookies are ignored. I expected that 
>> HTTP::Cookies->extract_cookies is called after every request. 
>> Therefor I create the cookie_jar from

Oops.

>> package My::Cookies;
>> use base 'HTTP::Cookies';
>> sub extract_cookies {
>>   warn "extract_cookies!!!";
>>   shift->SUPER::extract_cookies(@_);
>> }
>> To get it work, I need to parse the cookie headers myself or 
>> Apache::TestRequest::user_agent(
>>   reset => 1,   cookie_jar => $cookie_jar,   requests_redirectable => 
>> 0 );
>> But here I need to redirect myself or do it with the undocumented 
>> Apache::TestRequest::user_agent(
>>   reset => 1,   cookie_jar => $cookie_jar,   requests_redirectable => 
>> [qw~x y~]
>> );
>> that does anything I want but is undocumented!
>> Here is a part from Apache::TestRequest::user_agent that looks wrong 
>> to me.
>>         my $redir = $args->{requests_redirectable};
>>         if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
>>             $RedirectOK = 1;
>>         } else {
>>             $RedirectOK = 0;
>>         }

Does this address the issue?

--- TestRequest.pm.~1.96.~	Thu May  6 12:11:33 2004
+++ TestRequest.pm	Thu Jul 29 14:03:58 2004
@@ -115,7 +115,7 @@

      if (exists $args->{requests_redirectable}) {
          my $redir = $args->{requests_redirectable};
-        if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
+        if ((ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) or 
$redir) {
              $RedirectOK = 1;
          } else {
              $RedirectOK = 0;

I think that I might have changed this code to work this way, and 
overlooked that it could be passed as a simple boolean, even though 
that's the _only_ way it worked before I got my hands on it.

So does passing the array reference actually affect the way the LWP 
user agent object operates? If so, that's cool, but it's undocumented. 
I'm not sure whether it should be documented, though, since if LWP 
isn't installed it certainly won't work with the simple request 
interface that Apache::TestRequest uses in its place.

Regards,

David

Re: cookie_jar ignored with requests_redirectable => 1

Posted by Stas Bekman <st...@stason.org>.
Stas Bekman wrote:
> David?
> 
> Boris Zentner wrote:

We are all at the OSCon at the moment, so we hardly get a chance to 
sleep. I suppose David will follow up soonish :)

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Re: cookie_jar ignored with requests_redirectable => 1

Posted by Stas Bekman <st...@stason.org>.
David?

Boris Zentner wrote:
> Hi,
> recently I try to use Apache::Test with HTTP::Cookies. But it did not work as 
> expected. I suppose the way to add a cookie_jar to A::T was with 
> 'Apache::TestRequest::user_agent'. I read the docs from 
> Apache::TestRequest::user_agent. Here is the relevant part of it.
> 
>        And finally, the semantics of the "requests_redirectable"
>        parameter is different than for "LWP::UserAgent": It
>        either follows redirects for a request, or it doesn't.
>        Thus "requests_redirectable" is a boolean value instead of
>        the array reference that "LWP::UserAgent" expects. To
> 
> This implies to me that I have the choice to enable or disable redirects for 
> this useragent with a bool value.
> 
> Apache::TestRequest::user_agent(
>   reset => 1, 
>   cookie_jar => $cookie_jar, 
>   requests_redirectable => 1 
> );
> 
> But this way the cookies are ignored. I expected that 
> HTTP::Cookies->extract_cookies is called after every request. Therefor I 
> create the cookie_jar from
> 
> package My::Cookies;
> use base 'HTTP::Cookies';
> sub extract_cookies {
>   warn "extract_cookies!!!";
>   shift->SUPER::extract_cookies(@_);
> }
> 
> To get it work, I need to parse the cookie headers myself or 
> 
> Apache::TestRequest::user_agent(
>   reset => 1, 
>   cookie_jar => $cookie_jar, 
>   requests_redirectable => 0 
> );
> 
> But here I need to redirect myself or do it with the undocumented 
> 
> Apache::TestRequest::user_agent(
>   reset => 1, 
>   cookie_jar => $cookie_jar, 
>   requests_redirectable => [qw~x y~]
> );
> 
> that does anything I want but is undocumented!
> 
> Here is a part from Apache::TestRequest::user_agent that looks wrong to me.
> 
>         my $redir = $args->{requests_redirectable};
>         if (ref $redir and (@$redir > 1 or $redir->[0] ne 'POST')) {
>             $RedirectOK = 1;
>         } else {
>             $RedirectOK = 0;
>         }
> 
> ##############################
> And a test script.
> 
> use strict;
> use warnings FATAL => 'all';
> use Apache::Test;
> use Apache::TestUtil;
> use Apache::TestRequest qw'GET POST';
> plan tests => 3, have 'LWP';
> require HTTP::Cookies;
> require HTML::Form;
> use Data::Dumper;
> 
> package My::Cookies;
> use base 'HTTP::Cookies';
> sub extract_cookies {
>   warn "extract_cookies!!!";
>   shift->SUPER::extract_cookies(@_);
> }
> package main;
> my $cookie_jar = My::Cookies->new;
> Apache::TestRequest::user_agent(
>   reset      => 1, 
>   cookie_jar => $cookie_jar, 
>   requests_redirectable => [qw/c d/]
> ); 
> 
> # check if we can request a page
> my $r = GET '/x';
> ok t_cmp( $r->code, 200, '$r->code == HTTP_OK?');
> ok t_cmp( qr:\Qnew account:, $r->content, "new account page" );
> $r = POST '/y', [ 
>   email => 'bzm@2bz.de', 
>   login => 'boris16', 
> ];
> ok t_cmp( $r->code, 200, '$r->code == HTTP_OK?');
> 


-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com