Test::Varnish - Put your Varnish server to the test!
Version 0.03
Varnish is a high performance reverse proxy.
This module allows you to perform tests against a varnish server, asserting that a given resource (URL) is cached by varnish or not.
See it as a sort of Test::More extension to test Varnish. This can be useful when you want to test that your varnish setup and configuration works as expected.
Test::More
Another use for this module would be to poll random webservers to discover who is using Varnish.
use Test::Varnish; plan tests => 2; my $test_client = Test::Varnish->new({ verbose => 1 }); $test_client->isnt_cached( { url => 'http://my.opera.com/community/', }, 'My Opera frontpage is not (yet) cached by Varnish' ); $test_client->is_cached( { url => 'http://www.cnn.com/', }, 'Is CNN.com using Varnish?' );
Class constructor.
Allows you to create a Test::Varnish object. The allowed options are:
Test::Varnish
verbose
Controls the verbose mode, where additional diagnostic messages (not many, actually) are output together with the test assertions.
Set it to a true value to enable, false to disable.
use Test::Varnish; my $tv = Test::Varnish->new();
or
use Test::Varnish; my $tv = Test::Varnish->new({ verbose => 1 });
Takes an HTTP::Response object as argument. Examines the response headers to look for the default Varnish header (X-Varnish), to tell you if the response was coming directly from the Varnish cache, or not.
X-Varnish
In other words, this tells you if the request was a Varnish cache hit or miss.
is_cached() is a test assertion.
is_cached()
Asserts that a given request to a URL with certain headers, and such, is cached by the given Varnish instance.
Needs 2 arguments:
\%request
Request data, as hashref. You can specify:
url
The URL where to send the request to
headers
Additional HTTP headers, as hashref. See the example. Most probably you will need the Host header for Varnish to direct the request to the appropriate backend. YMMV.
Host
$message (optional)
$message
A message for the test assertion (ex.: Request to the frontpage with cookies should not be cached).
Request to the frontpage with cookies should not be cached
A default message will be provided if none is passed.
use Test::Varnish; my $tv = Test::Varnish->new(); $tv->is_cached( { url => 'http://your-server.your-domain.local', headers => { Host => 'www.your-domain.local', # ... } } );
or:
use Test::Varnish; my $tv = Test::Varnish->new(); $tv->is_cached( { url => 'http://192.168.1.100/super/', headers => { 'Host' => 'www.your-domain.local', 'Accept-Language' => 'it', } }, 'The super pages should always be cached, also in italian', );
isnt_cached() is a test assertion, exactly the opposite of "is_cached".
isnt_cached()
Asserts that a given request to a URL is not cached by the queried Varnish instance.
Returns a suitable user agent object (currently an LWP::UserAgent instance), that can be used to interact with the varnish instance.
Defines the default user agent string to be used for the requests issued by the default user agent object returned by "user_agent".
You can subclass Test::Varnish to define your own user agent string. I'm not sure this is 100% reasonable. Maybe.
Used internally, tells us if we're running in verbose mode. When verbose mode is active, the test assertions methods will output a bunch of diagnostic messages through Test::More::diag().
Test::More::diag()
You can activate the verbose mode by saying:
my $tv = Test::Varnish->new(); $tv->verbose(1);
Or, by instantiating the Test::Varnish object with the verbose option, giving it a true value:
my $tv = Test::Varnish->new({ verbose => 1 });
Cosimo Streppone, <cosimo at cpan.org>
<cosimo at cpan.org>
Please report any bugs or feature requests to bug-test-varnish at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Varnish. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-test-varnish at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Test::Varnish
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Varnish
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Test-Varnish
CPAN Ratings
http://cpanratings.perl.org/d/Test-Varnish
Search CPAN
http://search.cpan.org/dist/Test-Varnish
Copyright 2010 Cosimo Streppone, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Test::Varnish, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Varnish
CPAN shell
perl -MCPAN -e shell install Test::Varnish
For more information on module installation, please visit the detailed CPAN module installation guide.