Skip to content

Commit

Permalink
improved PSGI support and added "psgi" command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 27, 2010
1 parent 629b68c commit a911b4d
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

0.999923 2010-02-27 00:00:00
- Improved PSGI support and added "psgi" command.

0.999922 2010-02-11 00:00:00
- Added session support.
- Added signed cookie support.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo.pm
Expand Up @@ -47,7 +47,7 @@ __PACKAGE__->attr(

# DEPRECATED in Snowman!
# Use $Mojolicious::VERSION instead.
our $VERSION = '0.999922';
our $VERSION = '0.999923';

# Oh, so they have internet on computers now!
sub new {
Expand Down Expand Up @@ -80,7 +80,7 @@ sub start {
$ENV{MOJO_APP} ||= $class;

# Start!
Mojo::Commands->start(@_);
return Mojo::Commands->start(@_);
}

1;
Expand Down
89 changes: 89 additions & 0 deletions lib/Mojo/Command/Psgi.pm
@@ -0,0 +1,89 @@
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojo::Command::Psgi;

use strict;
use warnings;

use base 'Mojo::Command';

use Mojo::Server::PSGI;

use Getopt::Long 'GetOptions';

__PACKAGE__->attr(description => <<'EOF');
Start application with PSGI backend.
EOF
__PACKAGE__->attr(usage => <<"EOF");
usage: $0 psgi [OPTIONS]
These options are available:
--reload Automatically reload application when the source code changes.
EOF

# D’oh.
sub run {
my $self = shift;
my $psgi = Mojo::Server::PSGI->new;

# Options
@ARGV = @_ if @_;
GetOptions(reload => sub { $psgi->reload(1) });

# Return app callback
return sub { $psgi->run(@_) };
}

1;
__END__
=head1 NAME
Mojo::Command::Psgi - PSGI Command
=head1 SYNOPSIS
use Mojo::Command::Psgi;
my $psgi = Mojo::Command::Psgi->new;
my $app = $psgi->run(@ARGV);
=head1 DESCRIPTION
L<Mojo::Command::Psgi> is a command interface to L<Mojo::Server::PSGI>.
=head1 ATTRIBUTES
L<Mojo::Command::Psgi> inherits all attributes from L<Mojo::Command> and
implements the following new ones.
=head2 C<description>
my $description = $psgi->description;
$psgi = $psgi->description('Foo!');
Short description of this command, used for the command list.
=head2 C<usage>
my $usage = $psgi->usage;
$psgi = $psgi->usage('Foo!');
Usage information for this command, used for the help screen.
=head1 METHODS
L<Mojo::Command::Psgi> inherits all methods from L<Mojo::Command> and
implements the following new ones.
=head2 C<run>
my $app = $psgi->run(@ARGV);
Run this command.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
9 changes: 4 additions & 5 deletions lib/Mojo/Commands.pm
Expand Up @@ -70,8 +70,7 @@ sub run {

# Run
my $command = $module->new;
$help ? $command->help : $command->run(@args);
return $self;
return $help ? $command->help : $command->run(@args);
}

# Try all namspaces
Expand Down Expand Up @@ -138,7 +137,7 @@ sub start {
my @args = @_ ? @_ : @ARGV;

# Run
ref $self ? $self->run(@args) : $self->new->run(@args);
return ref $self ? $self->run(@args) : $self->new->run(@args);
}

sub _detect {
Expand Down Expand Up @@ -307,8 +306,8 @@ the following new ones.
=head2 C<run>
$commands = $commands->run;
$commands = $commands->run(@ARGV);
$commands->run;
$commands->run(@ARGV);
Load and run commands.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -26,7 +26,7 @@ __PACKAGE__->attr(static => sub { MojoX::Dispatcher::Static->new });
__PACKAGE__->attr(types => sub { MojoX::Types->new });

our $CODENAME = 'Snowman';
our $VERSION = '0.999922';
our $VERSION = '0.999923';

sub new {
my $self = shift->SUPER::new(@_);
Expand Down Expand Up @@ -192,7 +192,7 @@ sub start {
$ENV{MOJO_APP} ||= $class;

# Start!
Mojolicious::Commands->start(@_);
return Mojolicious::Commands->start(@_);
}

# This will run once at startup
Expand Down
50 changes: 40 additions & 10 deletions t/mojo/psgi.t
Expand Up @@ -5,15 +5,15 @@
use strict;
use warnings;

use Test::More tests => 9;

use_ok('Mojo::Server::PSGI');
use Test::More tests => 18;

# We need some more secret sauce. Put the mayonnaise in the sun.
my $psgi = Mojo::Server::PSGI->new;
my $app = sub { $psgi->run(@_) };
use_ok('Mojo::Server::PSGI');
use_ok('Mojo::Command::Psgi');

# Request
# Binding
my $psgi = Mojo::Server::PSGI->new;
my $app = sub { $psgi->run(@_) };
my $content = 'hello=world';
open my $body, '<', \$content;
my $env = {
Expand All @@ -33,11 +33,7 @@ my $env = {
'psgi.multiprocess' => 1,
'psgi.run_once' => 0
};

# Process
my $res = $app->($env);

# Response
is($res->[0], 200);
is($res->[1]->[0], 'Date');
ok($res->[1]->[1]);
Expand All @@ -49,3 +45,37 @@ my $params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
$params = eval "my $params";
is_deeply($params, {bar => 'baz', hello => 'world', lalala => 23});

# Command
$content = 'world=hello';
open $body, '<', \$content;
$env = {
CONTENT_LENGTH => 11,
CONTENT_TYPE => 'application/x-www-form-urlencoded',
PATH_INFO => '/diag/dump_params',
QUERY_STRING => 'lalala=23&bar=baz',
REQUEST_METHOD => 'POST',
SCRIPT_NAME => '/',
HTTP_HOST => 'localhost:8080',
SERVER_PROTOCOL => 'HTTP/1.0',
'psgi.version' => [1, 0],
'psgi.url_scheme' => 'http',
'psgi.input' => $body,
'psgi.errors' => *STDERR,
'psgi.multithread' => 0,
'psgi.multiprocess' => 1,
'psgi.run_once' => 0
};
$app = Mojo::Command::Psgi->new->run;
$res = $app->($env);
is($res->[0], 200);
is($res->[1]->[0], 'Date');
ok($res->[1]->[1]);
is($res->[1]->[2], 'Content-Length');
is($res->[1]->[3], 104);
is($res->[1]->[4], 'Content-Type');
is($res->[1]->[5], 'text/plain');
$params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
$params = eval "my $params";
is_deeply($params, {bar => 'baz', world => 'hello', lalala => 23});

0 comments on commit a911b4d

Please sign in to comment.