Skip to content

Commit

Permalink
updated generator scripts and added prepare/finalize methods to Mojol…
Browse files Browse the repository at this point in the history
…icious
  • Loading branch information
kraih committed Jul 3, 2009
1 parent ac7bc8d commit 6722355
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,5 +1,9 @@
This file documents the revision history for Perl extension Mojo.

0.991234 2009-07-03 00:00:00
- Added prepare/finalize methods to Mojolicious.
- Updated generator scripts to play nice with MM->parse_version.

0.991233 2009-07-01 00:00:00
- Rewrote Mojo::Client::process_local to use the new state machine.
- Added Server and X-Powered-By headers.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo.pm
Expand Up @@ -18,7 +18,7 @@ __PACKAGE__->attr(home => (default => sub { Mojo::Home->new }));
__PACKAGE__->attr(log => (default => sub { Mojo::Log->new }));

# Oh, so they have internet on computers now!
our $VERSION = '0.991233';
our $VERSION = '0.991234';

sub new {
my $self = shift->SUPER::new(@_);
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/Script/Generate/App.pm
Expand Up @@ -116,8 +116,6 @@ use warnings;
use base 'Mojo';
our $VERSION = '0.1';
sub handler {
my ($self, $tx) = @_;
Expand Down
3 changes: 0 additions & 3 deletions lib/Mojo/Script/Generate/Makefile.pm
Expand Up @@ -83,9 +83,6 @@ WriteMakefile(
NAME => '<%= $class %>',
VERSION_FROM => 'lib/<%= $path %>',
AUTHOR => 'A Good Programmer <nospam@cpan.org>',
($ExtUtils::MakeMaker::VERSION >= 6.3002
? ('LICENSE' => 'perl')
: ()),
EXE_FILES => ['bin/<%= $name %>'],
PREREQ_PM => { 'Mojo' => '0.9003' },
test => {TESTS => 't/*.t t/*/*.t t/*/*/*.t'}
Expand Down
27 changes: 25 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -97,15 +97,27 @@ sub dispatch {
elsif ($e) { $self->static->serve_404($c) }
}

# This will run for every request after dispatch
sub finalize { }

# Bite my shiny metal ass!
sub handler {
my ($self, $tx) = @_;

# Start timer
my $start = [Time::HiRes::gettimeofday()];

# Build context and dispatch
$self->dispatch($self->build_ctx($tx));
# Context
my $c = $self->build_ctx($tx);

# Prepare
$self->prepare($c);

# Dispatch unless we already have a response code
$self->dispatch($c) unless $c->res->code;

# Finalize
$self->finalize($c);

# End timer
my $elapsed = sprintf '%f',
Expand All @@ -116,6 +128,9 @@ sub handler {
return $tx;
}

# This will run for every request before dispatch
sub prepare { }

# This will run once at startup
sub startup { }

Expand Down Expand Up @@ -206,10 +221,18 @@ For example in production mode, C<production_mode> will be called.
$mojo->dispatch($c);
=head2 C<finalize>
$mojo->finalize($c);
=head2 C<handler>
$tx = $mojo->handler($tx);
=head2 C<prepare>
$mojo->prepare($c);
=head2 C<startup>
$mojo->startup($tx);
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojolicious/Script/Generate/App.pm
Expand Up @@ -140,8 +140,6 @@ use warnings;
use base 'Mojolicious';
our $VERSION = '0.1';
# This method will run once at server start
sub startup {
my $self = shift;
Expand Down
24 changes: 23 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -5,7 +5,7 @@
use strict;
use warnings;

use Test::More tests => 54;
use Test::More tests => 76;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand All @@ -28,6 +28,8 @@ $client->process_local('MojoliciousTest', $tx);
is($tx->res->code, 500);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/Missing right curly/);

# Foo::test
Expand All @@ -37,6 +39,8 @@ is($tx->res->code, 200);
is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/\/bar\/test/);

# Foo::index
Expand All @@ -46,6 +50,8 @@ is($tx->res->code, 200);
is($tx->res->headers->content_type, 'text/html');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/<body>\n23Hello Mojo from the template \/foo! He/);

# Foo::Bar::index
Expand All @@ -55,6 +61,8 @@ is($tx->res->code, 200);
is($tx->res->headers->content_type, 'text/html');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/Hello Mojo from the other template \/foo-bar!/);

# Foo::templateless
Expand All @@ -64,6 +72,8 @@ $client->process_local('MojoliciousTest', $tx);
is($tx->res->code, 200);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/Hello Mojo from a templateless renderer!/);

# MojoliciousTest2::Foo::test
Expand All @@ -73,6 +83,8 @@ is($tx->res->code, 200);
is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/\/test2/);

# MojoliciousTestController::index
Expand All @@ -82,6 +94,8 @@ is($tx->res->code, 200);
is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/No class works!/);

# 404
Expand All @@ -90,6 +104,8 @@ $client->process_local('MojoliciousTest', $tx);
is($tx->res->code, 404);
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->body, qr/File Not Found/);

# Static file /hello.txt in a production mode
Expand All @@ -101,6 +117,8 @@ is($tx->res->code, 200);
is($tx->res->headers->content_type, 'text/plain');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->content->file->slurp, qr/Hello Mojo from a static file!/);
$ENV{MOJO_MODE} = $backup;

Expand All @@ -122,6 +140,8 @@ is($tx->res->headers->content_length,
$stat->size, 'Content-Length is set correctly');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
like($tx->res->content->file->slurp,
qr/Hello Mojo from a development static file!/);
$ENV{MOJO_MODE} = $backup;
Expand All @@ -134,6 +154,8 @@ $client->process_local('MojoliciousTest', $tx);
is($tx->res->code, 304, 'Setting If-Modified-Since triggers 304');
is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
is($tx->res->headers->header('X-Finalized'), 'works!');
is($tx->res->headers->header('X-Prepared'), 'works!');
$ENV{MOJO_MODE} = $backup;

# Make sure we can override attributes with constructor arguments
Expand Down
4 changes: 4 additions & 0 deletions t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -14,6 +14,10 @@ sub development_mode {
$self->static->root($self->home->rel_dir('t/mojolicious/public_dev'));
}

sub finalize { $_[1]->res->headers->header('X-Finalized' => 'works!') }

sub prepare { $_[1]->res->headers->header('X-Prepared' => 'works!') }

sub production_mode {
my $self = shift;

Expand Down

0 comments on commit 6722355

Please sign in to comment.