On Tue May 14 05:37:10 2013, justincase wrote:
Show quoted text> Most browsers will convert a POST to a GET when they encounter a
> redirect. LWP does not do this. If it encounters a redirect from a
> POST, and the POST method is in the list of requests_redirectable,
> it propagates the POST to the redirected url. This is unexpected
> behavior when trying to automate browser tasks. Please consider
> adding an option to mimic the standard behavior of browsers, or
> include an example in the docs on how to accomplish this using the
> handlers.
Please close this ticket. It works as expected.
$| = 1; # autoflush
require IO::Socket; # make sure this work before we try to make a HTTP::Daemon
# First we make ourself a daemon in another process
my $D = shift || '';
if ($D eq 'daemon') {
require HTTP::Daemon;
my $d = HTTP::Daemon->new(Timeout => 10);
print "Please to meet you at: <URL:", $d->url, ">\n";
open(STDOUT, $^O eq 'VMS'? ">nl: " : ">/dev/null");
while ($c = $d->accept) {
$r = $c->get_request;
if ($r) {
my $p = ($r->uri->path_segments)[1];
my $func = lc("httpd_" . $r->method . "_$p");
if (defined &$func) {
&$func($c, $r);
}
else {
$c->send_error(404);
}
}
$c = undef; # close connection
}
print STDERR "HTTP Server terminated\n";
exit;
}
else {
use Config;
use FindBin;
my $perl = $Config{'perlpath'};
$perl = $^X if $^O eq 'VMS' or -x $^X and $^X =~ m,^([a-z]:)?/,i;
open(DAEMON, "$perl $FindBin::Bin/$FindBin::Script daemon |") or die "Can't exec daemon: $!";
}
use Test::More;
my $greeting = <DAEMON>;
$greeting =~ /(<[^>]+>)/;
require URI;
my $base = URI->new($1);
sub url {
my $u = URI->new(@_);
$u = $u->abs($_[1]) if @_ > 1;
$u->as_string;
}
print "Will access HTTP server at $base\n";
require LWP::UserAgent;
require HTTP::Request;
$ua = new LWP::UserAgent;
$ua->agent("Mozilla/0.01 " . $ua->agent);
push @{ $ua->requests_redirectable }, 'POST';
sub httpd_get_echo
{
my($c, $req) = @_;
$c->send_basic_header(200);
print $c "Content-Type: message/http\015\012";
$c->send_crlf;
print $c $req->as_string;
}
*httpd_post_echo = \&httpd_get_echo;
sub httpd_get_redirect
{
my($c) = @_;
$c->send_redirect("/echo/foo?a=1&b=2", 302);
}
sub httpd_post_redirect
{
my($c) = @_;
$c->send_redirect("/echo/foo?a=1&b=2", 302);
}
$req = new HTTP::Request GET => url("/redirect/foo", $base);
$res = $ua->request($req);
is($res->request->uri->path, '/echo/foo', 'redirected GET path');
is($res->request->uri->query, 'a=1&b=2', 'redirect GET query');
$req = new HTTP::Request POST => url("/redirect/foo", $base);
$req->content_type("application/x-www-form-urlencoded");
$req->content("foo=bar&bar=test");
$res = $ua->request($req);
is($res->request->uri->path, '/echo/foo', 'redirected POST path');
is($res->request->uri->query, 'a=1&b=2', 'redirected POST query');
is($res->request->method, 'GET', 'redirected POST method');
done_testing;