Skip to content

Commit

Permalink
better custom response tests and custom request examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 31, 2012
1 parent fdd4a95 commit b5485d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.17 2012-08-01
- Improved documentation.
- Improved tests.

3.16 2012-07-31
- Improved documentation.
Expand Down
10 changes: 8 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -565,6 +565,11 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
my $tx = $ua->cert('tls.crt')->key('tls.key')
->post_json('https://mojolicio.us' => {top => 'secret'});
# Custom JSON PUT request
my $tx = $ua->build_json_tx('http://mojolicious/foo' => {hi => 'there'});
$tx->req->method('PUT');
say $ua->start($tx)->res->body;
# Blocking parallel requests (does not work inside a running event loop)
my $delay = Mojo::IOLoop->delay;
for my $url ('mojolicio.us', 'cpan.org') {
Expand Down Expand Up @@ -1020,9 +1025,10 @@ append a callback to perform requests non-blocking.
my $tx = $ua->start(Mojo::Transaction::HTTP->new);
Process blocking transaction. You can also append a callback to perform
transactions non-blocking.
Process blocking request. You can also append a callback to perform requests
non-blocking.
my $tx = $ua->build_tx(GET => 'http://kraih.com');
$ua->start($tx => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Expand Down
14 changes: 13 additions & 1 deletion t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 24;
use Test::More tests => 27;

# "Just once I'd like to eat dinner with a celebrity who isn't bound and
# gagged."
Expand Down Expand Up @@ -69,6 +69,15 @@ get '/' => sub { shift->render_text('works') };
# GET /custom (never called if custom dispatchers work)
get '/custom' => sub { shift->render_text('does not work') };

# GET /res (custom response)
get '/res' => sub {
my $self = shift;
my $res
= Mojo::Message::Response->new(code => 200)->body('Custom response!');
$self->tx->res($res);
$self->tx->resume;
};

my $t = Test::Mojo->new;

# GET /
Expand All @@ -81,6 +90,9 @@ $t->get_ok('/hello.txt')->status_is(200)
# GET /custom
$t->get_ok('/custom?a=works+too')->status_is(205)->content_is('works too');

# GET /res (custom response)
$t->get_ok('/res')->status_is(200)->content_is('Custom response!');

# GET /custom_too
$t->get_ok('/custom_too')->status_is(200)->content_is('this works too');

Expand Down

0 comments on commit b5485d2

Please sign in to comment.