Skip to content

Instantly share code, notes, and snippets.

@bolav
Created March 31, 2009 14:38
Show Gist options
  • Save bolav/88215 to your computer and use it in GitHub Desktop.
Save bolav/88215 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use Test::More tests => 7;
use WWW::Curl::Easy;
use HTTP::Request;
my $url = 'http://en.wikipedia.org/wiki/Main_Page';
my $req = HTTP::Request->new( GET => $url );
isa_ok( $req, 'HTTP::Request' );
my $c;
{
my $curl = new WWW::Curl::Easy;
$curl->setopt( CURLOPT_HEADER, 1 );
$curl->setopt( CURLOPT_NOPROGRESS, 1 );
# my $url = $req->uri->as_string;
$curl->setopt( CURLOPT_URL, $url );
is( $curl->getinfo(CURLINFO_EFFECTIVE_URL), $url );
if ( $req->method eq 'POST' ) {
$curl->setopt( CURLOPT_POST, 1 );
$curl->setopt( CURLOPT_POSTFIELDS, $req->content );
}
my @headers;
foreach my $h ( +$req->headers->header_field_names ) {
warn "h: $h";
push( @headers, "$h: " . $req->header($h) );
}
if ( scalar(@headers) ) {
$curl->setopt( CURLOPT_HTTPHEADER, \@headers );
}
$curl->setopt(CURLOPT_WRITEFUNCTION, sub {
my $chunk = shift;
$c = $chunk;
return length($chunk);
});
my $retcode = $curl->perform;
is( $retcode, 0 );
is( $curl->strerror($retcode), 'no error' );
}
{
my $curl = new WWW::Curl::Easy;
$curl->setopt( CURLOPT_HEADER, 1 );
$curl->setopt( CURLOPT_NOPROGRESS, 1 );
# my $url = $req->uri->as_string;
$curl->setopt( CURLOPT_URL, $req->uri->as_string );
is( $curl->getinfo(CURLINFO_EFFECTIVE_URL), $url );
if ( $req->method eq 'POST' ) {
$curl->setopt( CURLOPT_POST, 1 );
$curl->setopt( CURLOPT_POSTFIELDS, $req->content );
}
my @headers;
foreach my $h ( +$req->headers->header_field_names ) {
warn "h: $h";
push( @headers, "$h: " . $req->header($h) );
}
if ( scalar(@headers) ) {
$curl->setopt( CURLOPT_HTTPHEADER, \@headers );
}
$curl->setopt(CURLOPT_WRITEFUNCTION, sub {
my $chunk = shift;
$c = $chunk;
return length($chunk);
});
my $retcode = $curl->perform;
is( $retcode, 0 );
is( $curl->strerror($retcode), 'no error' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment