views:

77

answers:

3

Does the test of the file-size make sense here? I tried to cut the connection while downloading, but it looks like the size-test is never reached.

#!/usr/bin/env perl
use warnings; 
use strict;
use 5.012;
use LWP::Simple;

my $url = 'http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.34-rc5-next-20100428.bz2';
my $file = 'next_kernel.bz';
my $file_size = '3462166';

my $response = getstore( $url, $file );
if ( is_success $response ) {
    if ( $file_size == -s $file ) {
        say "OK";
    }
    else {
        say "Not OK";
    }
}
+2  A: 

If you just want to exercise the test code, put in the wrong file size and assert that your download is not "OK". If you want to test what happens with an aborted connection, you need a much larger file -- a 2342-byte download should only take a fraction of a fraction of a second.

mobrule
A: 

I suppose it doesn't hurt, but I'm fairly certain is_success will not return true if the connection is aborted.

Dan
I tried again and if I wait long enough I get a "Not OK".
sid_com
+1  A: 

This is weird, I got the same results as you. I don't have a solution, but I report here a bit of hacking around your problem.

What is happening is that the file saved is bigger than expected and there was no connection break. Trying to bunzip2 it will give error, even after truncating the resulting file at desired size.

Exact same result was achieved by using LWP::UserAgent with :content_file parameter to get() method. Same again adding a response_data handler to $ua object.

But the following command got me good results:

GET http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.34-rc5-next-20100428.bz2 > next_kernel.bz

And since GET is the lwp-request script, based on LWP, you might find answers by checking its code if you're willing to dig that deep.

Hope this helps you.

lfagundes