Skip to content

Commit

Permalink
fixed a few small route bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 21, 2011
1 parent 73274fe commit abee750
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -12,6 +12,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved documentation.
- Fixed small redirect_to bug. (judofyr, sri)
- Fixed small attribute selector bug in Mojo::DOM::CSS.
- Fixed a few small route bugs.
- Fixed Perl 5.8.7 compatibility.
- Fixed typos.

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -343,7 +343,7 @@ sub waypoint { shift->route(@_)->block(1) }

sub websocket {
my $self = shift;
my $route = $self->any(@_);
my $route = $self->get(@_);
$route->{websocket} = 1;
return $route;
}
Expand Down
8 changes: 2 additions & 6 deletions lib/Mojolicious/Routes/Match.pm
Expand Up @@ -41,10 +41,7 @@ sub match {
my $captures = $pattern->shape_match(\$path);
return unless $captures;
$self->{path} = $path;

# Merge captures
$captures = {%{$self->captures}, %$captures};
$self->captures($captures);

# Method
if (my $methods = $r->via) {
Expand Down Expand Up @@ -72,17 +69,16 @@ sub match {
# WebSocket
return if $r->is_websocket && !$self->{websocket};

# Empty path
my $empty = !length $path || $path eq '/' ? 1 : 0;

# Partial
my $empty = !length $path || $path eq '/' ? 1 : 0;
if ($r->partial) {
$captures->{path} = $path;
$self->endpoint($r);
$empty = 1;
}

# Update stack
$self->captures($captures);
my $endpoint = $r->is_endpoint;
if ($r->inline || ($endpoint && $empty)) {
push @{$self->stack}, {%$captures};
Expand Down
38 changes: 37 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {

# "Oh, dear. She’s stuck in an infinite loop and he’s an idiot.
# Well, that’s love for you."
use Test::More tests => 65;
use Test::More tests => 78;

# "Your mistletoe is no match for my *tow* missile."
use Mojo::ByteStream 'b';
Expand All @@ -29,6 +29,9 @@ websocket '/echo' => sub {
);
};

# GET /echo
get '/echo' => {text => 'plain echo!'};

# GET /plain
get '/plain' => {text => 'Nothing to see here!'};

Expand Down Expand Up @@ -62,6 +65,26 @@ websocket '/bytes' => sub {
);
};

# /nested
under '/nested';

# WebSocket /nested
websocket sub {
my $self = shift;
$self->on_message(
sub {
my ($self, $message) = @_;
$self->send_message("nested echo: $message");
}
);
};

# GET /nested
get {text => 'plain nested!'};

# POST /nested
post {data => 'plain nested too!'};

# "I was a hero to broken robots 'cause I was one of them, but how can I sing
# about being damaged if I'm not?
# That's like Christina Aguilera singing Spanish.
Expand All @@ -81,6 +104,9 @@ $t->websocket_ok('/echo')->send_message_ok('hello again')
$t->websocket_ok('/echo')->send_message_ok(0)->message_is('echo: 0')
->finish_ok;

# GET /echo (plain alternative)
$t->get_ok('/echo')->status_is(200)->content_is('plain echo!');

# GET /plain
$t->get_ok('/plain')->status_is(200)->content_is('Nothing to see here!');

Expand Down Expand Up @@ -124,3 +150,13 @@ $t->websocket_ok('/bytes')->send_message_ok([$bytes])->message_is($bytes)
# WebSocket /bytes (multiple times)
$t->websocket_ok('/bytes')->send_message_ok([$bytes])->message_is($bytes)
->send_message_ok([$bytes])->message_is($bytes)->finish_ok;

# WebSocket /nested
$t->websocket_ok('/nested')->send_message_ok('hello')
->message_is('nested echo: hello')->finish_ok;

# GET /nested (plain alternative)
$t->get_ok('/nested')->status_is(200)->content_is('plain nested!');

# POST /nested (another plain alternative)
$t->post_ok('/nested')->status_is(200)->content_is('plain nested too!');

0 comments on commit abee750

Please sign in to comment.