my
@exception_class_classes
= Exception::Class::Classes();
my
%exception_class_classes
;
@exception_class_classes
{
@exception_class_classes
} =
undef
;
ok
exists
$exception_class_classes
{
'HTTP::Exception'
},
'HTTP::Exception acts as loader and exception'
;
throws_ok
sub
{ HTTP::Exception->throw(200) },
'HTTP::Exception::200'
;
throws_ok
sub
{ HTTP::Exception->throw },
qr/HTTP::Exception->throw needs a HTTP-Statuscode to throw/
;
throws_ok
sub
{ HTTP::Exception->throw(1) },
qr/Unknown HTTP-Statuscode:/
;
eval
{ HTTP::Exception->throw };
my
$e0
= HTTP::Exception->caught;
ok (!
defined
$e0
,
'HTTP::Exception is not caught when no Errorcode is given'
);
eval
{ HTTP::Exception->throw(1) };
my
$e00
= HTTP::Exception->caught;
ok (!
defined
$e00
,
'HTTP::Exception is not caught when wrong Errorcode is given'
);
my
$e2
= HTTP::Exception->new(200);
__PACKAGE__->_run_tests_for_exception_object(
$e2
);
delete
$exception_class_classes
{
'HTTP::Exception'
};
for
my
$exception_name
(
keys
%exception_class_classes
) {
throws_ok
sub
{
$exception_name
->throw },
$exception_name
;
my
$e
=
$exception_name
->new;
isa_ok
$e
,
$exception_name
;
__PACKAGE__->_run_tests_for_exception_object(
$e
);
eval
{
$exception_name
->throw() };
my
$e1
= HTTP::Exception->caught;
isa_ok
$e1
,
$exception_name
;
__PACKAGE__->_run_tests_for_exception_object(
$e1
);
my
$error_code_range
=
$e1
->code;
$error_code_range
=~ s/\d{2}$/XX/;
eval
{
$exception_name
->throw() };
my
$e2
=
"HTTP::Exception::$error_code_range"
->caught;
__PACKAGE__->_run_tests_for_exception_object(
$e2
);
eval
{
$exception_name
->throw() };
my
$e3
=
$exception_name
->caught;
isa_ok
$e3
,
$exception_name
;
__PACKAGE__->_run_tests_for_exception_object(
$e3
);
}
sub
_run_tests_for_exception_object {
my
$class
=
shift
;
my
$e
=
shift
;
like
$e
->code,
qr/\d+/
,
'code is a number ('
.
$e
->code .
')'
;
is
$e
->status_message,
HTTP::Status::status_message(
$e
->code),
'status_message is same as HTTP::Status'
;
is
$e
->as_string,
HTTP::Status::status_message(
$e
->code),
'to_string as well'
;
is ((
$e
->Fields)[0],
'status_message'
,
'field status_message found'
);
can_ok
$e
,
qw(code status_message as_string)
;
ok !
$e
->can(
'_make_exceptions'
),
'_make_exceptions is not imported from Loader'
;
SKIP: {
skip
q(can't reliably determine expected Errorcode)
, 2
unless
(
ref
(
$e
) =~ /(\d+)$/);
is
$e
->code,
$1,
'Errorcode in Classname and expected HTTP-Errorcode match'
;
};
}
done_testing;