|
Posted by Ignoramus9014 on February 19, 2008, 4:49 pm
Please log in for more thread options >
>> I would like to know what are the "most recommended" perl modules for
>> testing websites. For example, my typical task would be
>>
>> - connect to IP address A
>> - request webpage B http://B/page.html
>> - check that it contacts a keyword C
>>
>> What is important is that address A (my test or backup website) does
>> not necessarily match virtual host name B.
>>
>> I read on Test::WWW::Mechanize, which I like a lot as I am very
>> familiar with WWW::Mechanize, but it does not seem to offer this
>> ability (connect to a given IP but use a unrelated virtual address).
>
> You should be able to subclass LWP::Protocol::http, override
> ->_new_socket to map B->A and call SUPER::_new_socket, and then use
> LWP::Protocol::implementor to set your new class as the implementation
> to use for http. Then you should be able to just use WWW::Mech normally.
> Something like (completely untested):
>
> use LWP;
> use WWW::Mechanize;
>
> {
> package My::LWP::Protocol::http;
>
> our @ISA = 'LWP::Protocol::http';
>
> my %fake = (
> 'B' => '192.168.1.2',
> );
>
> sub _new_socket {
> my ($self, $host, $port, $timeout) = @_;
> $fake and $host = $fake;
> return $self->SUPER::_new_socket($host, $port, $timeout);
> }
>
> LWP::Protocol::implementor http => __PACKAGE__;
> }
>
> my $M = WWW::Mechanize->new(...);
>
> ...
>
> Ben
>
Ben, thanks, it is a little over my head, but I spent some time
trying.
I got to the point of running queries, but the _new_socket function
was never called.
Here's my actual code:
######################################################################
#!/usr/bin/perl
use LWP;
use WWW::Mechanize;
use Data::Dumper;
{
package My::LWP::Protocol::http;
use Data::Dumper;
use LWP::Protocol::http;
our @ISA = 'LWP::Protocol::http';
my %fake = (
'B' => '127.0.0.1',
);
sub _new_socket {
my ($self, $host, $port, $timeout) = @_;
print STDERR "HOST=$host\n";
exit 0;
$fake and $host = $fake;
return $self->SUPER::_new_socket($host, $port, $timeout);
}
sub new {
return LWP::Protocol::http->new;
}
LWP::Protocol::implementor http => __PACKAGE__;
}
my $M = WWW::Mechanize->new();
my $resp = $M->get( 'http://www.algebra.com/robots.txt' );
print $resp->content;
|