Commits

Toby Inkster  committed 872b694

test IO failure

  • Participants
  • Parent commits b8fa9e1

Comments (0)

Files changed (2)

File lib/IO/Callback/HTTP.pm

 	$IO::Callback::HTTP::VERSION   = '0.002';
 }
 
+use Carp                     qw();
 use Encode                   qw( encode_utf8 );
 use Errno                    qw( EIO );
 use HTTP::Request::Common    qw( GET PUT );
 
 sub USER_AGENT ()
 {
-	our $USER_AGENT ||= LWP::UserAgent->new(
+	our $USER_AGENT ||= LWP::UserAgent::->new(
 		agent => sprintf('%s/%s ', __PACKAGE__, __PACKAGE__->VERSION),
 	);
 }
 	$self->SUPER::open($mode, $code);
 }
 
+
+sub _process_arg
+{
+	my ($self, $arg) = @_;
+	
+	if (defined $arg->{failure} and not ref $arg->{failure})
+	{
+		my $carpage = Carp::->can($arg->{failure})
+			or Carp::croak("Unknown failure mode: '$arg->{failure}'");
+		$arg->{failure} = sub
+		{
+			my $res = shift;
+			$carpage->(sprintf(
+				'HTTP %s request for <%s> failed: %s',
+				$res->request->method,
+				$res->request->uri,
+				$res->status_line,
+			));
+		}
+	}
+}
+
 sub _mk_reader
 {
 	my ($self, $code, %args) = @_;
+	$self->_process_arg(\%args);
 	
 	if ((not ref $code)
 	or  (blessed $code and $code->isa('URI'))
 				return $res->decoded_content;
 			}
 			
+			$! = EIO;
 			$args{failure}->($res) if $args{failure};
-			$! = EIO;
 			return IO::Callback::Error;
 		}
 	}
 sub _mk_writer
 {
 	my ($self, $code, %args) = @_;
+	$self->_process_arg(\%args);
 	
 	if ((not ref $code)
 	or  (blessed $code and $code->isa('URI'))
 				return;
 			}
 			
+			$! = EIO;
 			$args{failure}->($res) if $args{failure};
-			$! = EIO;
 			return IO::Callback::Error;
 		}
 	}
 times out or non-2XX HTTP response code). It is passed a single
 parameter, which is the L<HTTP::Response> object. 
 
+As a shortcut, the strings 'croak', 'confess', 'carp' and 'cluck' are
+also accepted, with the same meanings as defined in L<Carp>.
+
 Either way, IO::Callback::HTTP should do the correct thing, setting
 C<< $! >> and so on.
 
 
 is($found_it => 1, 'another lines seems fine');
 
+sub Test::HTTP::Server::Request::not_found {
+	my $self = shift;
+	$self->{out_code} = '404 Not Found';
+	'Not found';
+}
+
+my $fh3 = IO::Callback::HTTP::->new(
+	'<',
+	POST( $server->uri.'not_found' ),
+	failure => 'croak',
+);
+
+my $data = eval { <$fh3> };
+like(
+	$@,
+	qr{HTTP POST request for \<\S+not_found\> failed: 404 Not Found},
+	'failure callback works',
+);
+is(
+	$!+0,
+	5,
+	'sets $! correctly',
+);
+
 done_testing;