Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed two custom response bugs
  • Loading branch information
kraih committed Aug 1, 2012
1 parent b5485d2 commit 5652255
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -2,6 +2,8 @@
3.17 2012-08-01
- Improved documentation.
- Improved tests.
- Fixed bug in after_static_dispatch hook that prevented custom responses.
- Fixed bug that prevented conditions from generating responses.

3.16 2012-07-31
- Improved documentation.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -1025,7 +1025,7 @@ append a callback to perform requests non-blocking.
my $tx = $ua->start(Mojo::Transaction::HTTP->new);
Process blocking request. You can also append a callback to perform requests
Perform blocking request. You can also append a callback to perform requests
non-blocking.
my $tx = $ua->build_tx(GET => 'http://kraih.com');
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -119,15 +119,15 @@ sub dispatch {
my $plugins = $self->plugins->emit_hook(before_dispatch => $c);

# Try to find a static file
my $res = $tx->res;
$self->static->dispatch($c) unless $res->code;
$self->static->dispatch($c) unless $tx->res->code;
$plugins->emit_hook_reverse(after_static_dispatch => $c);

# Routes
my $res = $tx->res;
return if $res->code;
if (my $code = ($tx->req->error)[1]) { $res->code($code) }
elsif ($tx->is_websocket) { $res->code(426) }
$c->render_not_found unless $self->routes->dispatch($c) || $res->code;
$c->render_not_found unless $self->routes->dispatch($c) || $tx->res->code;
}

# "Bite my shiny metal ass!"
Expand Down
47 changes: 41 additions & 6 deletions t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

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

# "Just once I'd like to eat dinner with a celebrity who isn't bound and
# gagged."
Expand Down Expand Up @@ -63,19 +63,43 @@ hook after_static_dispatch => sub {
if $self->req->url->path->contains('/custom_too');
};

# Cleared response for /res.txt
hook after_static_dispatch => sub {
my $self = shift;
return
unless $self->req->url->path->contains('/res.txt')
&& $self->param('route');
my $content = $self->res->body;
$self->tx->res(Mojo::Message::Response->new);
$self->res->headers->header('X-Res' => $content);
};

# Response generating condition "res" for /res.txt
app->routes->add_condition(
res => sub {
my ($r, $c) = @_;
return 1 unless $c->param('res');
my $res = Mojo::Message::Response->new(code => 200)
->body('Conditional response!');
$c->tx->res($res);
$c->rendered;
return;
}
);

# GET /
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 {
# GET /res.txt (custom response)
get '/res.txt' => (res => 1) => sub {
my $self = shift;
my $res
= Mojo::Message::Response->new(code => 200)->body('Custom response!');
$self->tx->res($res);
$self->tx->resume;
$self->rendered;
};

my $t = Test::Mojo->new;
Expand All @@ -90,8 +114,15 @@ $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 /res.txt (static file)
$t->get_ok('/res.txt')->status_is(200)->content_is("Hello!\n");

# GET /res.txt?route=1 (custom response)
$t->get_ok('/res.txt?route=1')->status_is(200)->content_is('Custom response!');

# GET /res.txt?route=1&res=1 (conditional response)
$t->get_ok('/res.txt?route=1&res=1')->status_is(200)
->content_is('Conditional response!');

# GET /custom_too
$t->get_ok('/custom_too')->status_is(200)->content_is('this works too');
Expand All @@ -107,3 +138,7 @@ $t->get_ok('/not_found')->status_is(200)->content_is('works');

# GET /not_found (internal redirect to second wrapper)
$t->get_ok('/not_found?wrap=1')->status_is(200)->content_is('Wrapped again!');

__DATA__
@@ res.txt
Hello!

0 comments on commit 5652255

Please sign in to comment.