Navigation Menu

Skip to content

Commit

Permalink
added Mojolicious::Lite support to ojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 21, 2010
1 parent 6a4b980 commit 2d6ca3b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
30 changes: 26 additions & 4 deletions lib/ojo.pm
Expand Up @@ -19,13 +19,20 @@ sub import {
no strict 'refs';
no warnings 'redefine';

# Mojolicious::Lite
eval "package $caller; use Mojolicious::Lite;";

# Functions
*{"${caller}::Oo"} = *{"${caller}::b"} = \&b;
*{"${caller}::oO"} = sub { _request(@_) };
*{"${caller}::d"} = sub { _request('delete', @_) };
*{"${caller}::g"} = sub { _request('get', @_) };
*{"${caller}::p"} = sub { _request('post', @_) };
*{"${caller}::u"} = sub { _request('put', @_) };
*{"${caller}::a"} =
sub { *{"${caller}::any"}->(@_) and return *{"${caller}::app"}->() };
*{"${caller}::d"} = sub { _request('delete', @_) };
*{"${caller}::g"} = sub { _request('get', @_) };
*{"${caller}::p"} = sub { _request('post', @_) };
*{"${caller}::u"} = sub { _request('put', @_) };
*{"${caller}::w"} =
sub { Mojo::Client->singleton->websocket(@_)->process }
}

sub _request {
Expand Down Expand Up @@ -56,6 +63,15 @@ Note that this module is EXPERIMENTAL and might change without warning!
L<ojo> implements the following functions.
=head2 C<a>
my $app = a('/' => sub { shift->render(json => {hello => 'world'}) });
Create L<Mojolicious::Lite> route accepting all request methods and return
the application.
perl -Mojo -e 'a("/" => {text => "Hello Mojo!"})->start' daemon
=head2 C<b>
my $stream = b('lalala');
Expand Down Expand Up @@ -116,6 +132,12 @@ object.
Perform C<PUT> request and turn response into a L<Mojo::Message::Response>
object.
=head2 C<w>
w('ws://mojolicio.us' => sub {...});
Open a WebSocket connection.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
Expand Down
10 changes: 8 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -16,7 +16,7 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 430;
plan tests => 432;

# Pollution
123 =~ m/(\d+)/;
Expand All @@ -37,7 +37,7 @@ use Mojolicious::Lite;
use Test::Mojo;

# Mojolicious::Lite and ojo
use_ok('ojo');
use ojo;

# Silence
app->log->level('error');
Expand All @@ -54,6 +54,9 @@ app->defaults(default => 23);
# GET /
get '/' => 'root';

# /ojo
a '/ojo' => {json => {hello => 'world'}};

# GET /null/0
get '/null/:null' => sub {
my $self = shift;
Expand Down Expand Up @@ -481,6 +484,9 @@ $t->head_ok('/')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
$t->get_ok('/', '1234' x 1024)->status_is(200)
->content_is('/root.html/root.html/root.html/root.html/root.html');

# GET /ojo (ojo)
$t->get_ok('/ojo')->status_is(200)->json_content_is({hello => 'world'});

# GET /null/0
$t->get_ok('/null/0')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down
22 changes: 20 additions & 2 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -14,14 +14,17 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 12;
plan tests => 13;

# Oh, dear. She’s stuck in an infinite loop and he’s an idiot.
# Well, that’s love for you.
use IO::Socket::INET;
use Mojolicious::Lite;
use Mojo::Client;

# Mojolicious::Lite and ojo
use ojo;

# Silence
app->log->level('fatal');

Expand Down Expand Up @@ -74,7 +77,7 @@ websocket '/deadcallback' => sub {
$self->receive_message(sub { die 'i see dead callbacks' });
};

my $client = Mojo::Client->new->app(app);
my $client = Mojo::Client->singleton->app(app);

# WebSocket /
my $result;
Expand All @@ -93,6 +96,21 @@ $client->websocket(
)->process;
is($result, 'test1test2', 'right result');

# WebSocket / (ojo)
$result = undef;
w '/' => sub {
my $self = shift;
$self->receive_message(
sub {
my ($self, $message) = @_;
$result = $message;
$self->finish;
}
);
$self->send_message('test1');
};
is($result, 'test1test2', 'right result');

# WebSocket /socket (using an already prepared socket)
my $peer = $client->test_server;
my $local = $client->ioloop->generate_port;
Expand Down

0 comments on commit 2d6ca3b

Please sign in to comment.