A Test::WWW::Mechanize trap: getting contents in a file

ferreira on 2007-10-03T22:27:29

Sometimes I wonder at how good I am to use code in the wrong way. This time I did it with Test::WWW::Mechanize. I started a test script with:

    use Test::WWW::Mechanize;

    get_ok( $url );

and then I decided to take a look at the HTML I was getting. Using the LWP options seemed natural:

    get_ok( $url, { ':content_file' => 'p.html' } );

But that gave me some binary content rather than the HTML I was waiting for. The problem was that WWW::Mechanize (the father of most of the browser functionality of the testing module) determines that 'gzip' can be used as the content encoding (if Compress::Zlib is available) and transparently decodes the response.

Unfortunately, the LWP options in get_ok are the same as in LWP::UserAgent::get and knows nothing about this helpful trick. So I got the gzipped content and not the HTML page. The content is uncompressed later with other WWW::Mechanize methods.

In turn,

    use File::Slurp qw( write_file );
    write_file( 'p.html', $mech->content );
worked as I needed it.