Test::HTTPStatus - check an HTTP status


Test-HTTPStatus documentation Contained in the Test-HTTPStatus distribution.

Index


Code Index:

NAME

Top

Test::HTTPStatus - check an HTTP status

SYNOPSIS

Top

	use Test::HTTPStatus tests => 2;
	use Apache::Constants qw(:http);

	http_ok( 'http://www.perl.org', HTTP_OK );

	http_ok( $url, $status );

DESCRIPTION

Top

Check the HTTP status for a resource.

FUNCTIONS

Top

http_ok( URL [, HTTP_STATUS] )

Print the ok message if the URL's HTTP status matches the specified HTTP_STATUS. If you don't specify a status, it assumes you mean HTTP_OK (from Apache::Constants).

SEE ALSO

Top

Apache::Constants, HTTP::SimpleLinkChecker

SOURCE AVAILABILITY

Top

This source is part of a SourceForge project which always has the latest sources in CVS, as well as all of the previous releases.

	http://sourceforge.net/projects/brian-d-foy/

If, for some reason, I disappear from the world, one of the other members of the project can shepherd this module appropriately.

AUTHOR

Top

brian d foy, <bdfoy@cpan.org>

COPYRIGHT AND LICENSE

Top


Test-HTTPStatus documentation Contained in the Test-HTTPStatus distribution.
#$Id: HTTPStatus.pm 2672 2008-08-15 15:28:00Z comdog $
package Test::HTTPStatus;
use strict;

use warnings;
no warnings;

use 5.006;
use vars qw($VERSION);
$VERSION = 1.08;

use Carp qw(carp);
use HTTP::SimpleLinkChecker;
use Test::Builder;
use URI;

my $Test = Test::Builder->new;

use constant NO_URL             =>  -1;
use constant INVALID_URL        =>  -2;
use constant HTTP_OK            => 200;
use constant HTTP_NOT_FOUND     => 404;

sub import
	{
    my $self = shift;
    my $caller = caller;
    no strict 'refs';
    *{$caller.'::http_ok'}         = \&http_ok;
    *{$caller.'::NO_URL'}          = \&NO_URL;
    *{$caller.'::INVALID_URL'}     = \&INVALID_URL;
    *{$caller.'::HTTP_OK'}         = \&HTTP_OK;
    *{$caller.'::HTTP_NOT_FOUND'}  = \&HTTP_NOT_FOUND;

    $Test->exported_to($caller);
    $Test->plan(@_);
	}

sub http_ok
	{
	my $url      = shift;
	my $expected = shift || HTTP_OK;

	my $hash = _get_status( $url );

	my $status = $hash->{status};

	if( defined $expected and $expected eq $status )
		{
		$Test->ok( 1, "Expected [$expected], got [$status] for [$url]");
		}
	elsif( $status == NO_URL )
		{
		$Test->ok( 0, "[$url] does not appear to be anything");
		}
	elsif( $status == INVALID_URL )
		{
		$Test->ok( 0, "[$url] does not appear to be a valid URL");
		}
	else
		{
		$Test->ok( 0, "Mysterious failure for [$url]" );
		}
	}

sub _get_status
	{
	my $string = shift;

	return { status => NO_URL } unless defined $string;

	my $url = URI->new($string)->canonical;
	return { result => INVALID_URL }
		unless UNIVERSAL::isa( $url, 'URI' );

	my $status = HTTP::SimpleLinkChecker::check_link( $url );

	return { url => $url, status => $status };
	}


1;