Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add result method to Mojo::Transaction
  • Loading branch information
kraih committed Dec 20, 2016
1 parent 367ff24 commit 97f16ad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

7.13 2016-12-21
- Deprecated Mojo::Message::Response in favor of new is_* methods.
- Added result method to Mojo::Transaction.
- Added is_client_error, is_error, is_info, is_redirect, is_server_error and
is_success methods to Mojo::Message::Response.

Expand Down
20 changes: 20 additions & 0 deletions lib/Mojo/Transaction.pm
Expand Up @@ -41,6 +41,12 @@ sub remote_address {
: $self->original_remote_address;
}

sub result {
my $self = shift;
my $err = $self->error;
return !$err || $err->{code} ? $self->res : croak $err->{message};
}

sub server_read { croak 'Method "server_read" not implemented by subclass' }
sub server_write { croak 'Method "server_write" not implemented by subclass' }

Expand Down Expand Up @@ -221,6 +227,20 @@ Same as L</"original_remote_address"> or the last value of the
C<X-Forwarded-For> header if L</"req"> has been performed through a reverse
proxy.
=head2 result
my $res = $tx->result;
Returns the L<Mojo::Message::Response> object from L</"res"> or dies if a
connection error has occured.
# Fine grained response handling
my $res = $tx->result;
if ($res->is_success) { say $res->body }
elsif ($res->is_error) { say $res->message }
elsif ($res->code == 301) { say $res->headers->location }
else { say 'Whatever...' }
=head2 server_read
$tx->server_read($bytes);
Expand Down
23 changes: 15 additions & 8 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -342,14 +342,12 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
my $ua = Mojo::UserAgent->new;
say $ua->get('www.☃.net?hello=there' => {Accept => '*/*'})->res->body;
# Form POST (application/x-www-form-urlencoded) with exception handling
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
if (my $res = $tx->success) { say $res->body }
else {
my $err = $tx->error;
die "$err->{code} response: $err->{message}" if $err->{code};
die "Connection error: $err->{message}";
}
# Fine grained response handling (dies on connection errors)
my $res = $ua->get('mojolicious.org/perldoc')->result;
if ($res->is_success) { say $res->body }
elsif ($res->is_error) { say $res->message }
elsif ($res->code == 301) { say $res->headers->location }
else { say 'Whatever...' }
# Extract data from HTML and XML resources with CSS selectors
say $ua->get('www.perl.org')->res->dom->at('title')->text;
Expand Down Expand Up @@ -378,6 +376,15 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
->get('https://www.github.com/kraih/mojo/tarball/master')
->res->content->asset->move_to('/home/sri/mojo.tar.gz');
# Form POST (application/x-www-form-urlencoded) with exception handling
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
if (my $res = $tx->success) { say $res->body }
else {
my $err = $tx->error;
die "$err->{code} response: $err->{message}" if $err->{code};
die "Connection error: $err->{message}";
}
# Non-blocking request
$ua->get('mojolicious.org' => sub {
my ($ua, $tx) = @_;
Expand Down
12 changes: 10 additions & 2 deletions t/mojo/user_agent.t
Expand Up @@ -327,6 +327,8 @@ ok !$tx->success, 'not successful';
is $tx->error->{message}, 'Premature connection close', 'right error';
is $timeout, 1, 'finish event has been emitted';
like $log, qr/Inactivity timeout/, 'right log message';
eval { $tx->result };
like $@, qr/Premature connection close/, 'right error';

# Client times out
$ua->once(
Expand All @@ -343,6 +345,8 @@ $ua->once(
$tx = $ua->get('/timeout?timeout=5');
ok !$tx->success, 'not successful';
is $tx->error->{message}, 'Inactivity timeout', 'right error';
eval { $tx->result };
like $@, qr/Inactivity timeout/, 'right error';

# Keep alive connection times out
my $id;
Expand Down Expand Up @@ -372,7 +376,9 @@ ok $tx->res->is_limit_exceeded, 'limit is exceeded';

# 404 response
$tx = $ua->get('/does_not_exist');
ok !$tx->success, 'not successful';
ok !$tx->success, 'not successful';
ok $tx->result, 'has a result';
is $tx->result->code, 404, 'right status';
ok !$tx->kept_alive, 'kept connection not alive';
ok $tx->keep_alive, 'keep connection alive';
is $tx->error->{message}, 'Not Found', 'right error';
Expand All @@ -388,7 +394,9 @@ is $tx->error->{code}, 404, 'right status';
$tx = $ua->build_tx(GET => '/echo' => 'Hello GZip!');
$tx = $ua->start($ua->build_tx(GET => '/echo' => 'Hello GZip!'));
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
ok $tx->result, 'has a result';
is $tx->result->code, 200, 'right status';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->content_encoding, undef, 'no "Content-Encoding" value';
is $tx->res->body, 'Hello GZip!', 'right content';
$tx = $ua->build_tx(GET => '/echo' => 'Hello GZip!');
Expand Down

0 comments on commit 97f16ad

Please sign in to comment.