Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 16, 2009
1 parent 38086ac commit b9c9035
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 80 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojo.

0.999915 2009-12-10 00:00:00
- Added routes captures to params in Mojolicious.
- Added charset plugin to Mojolicious. (charsbar)
- Made param decoding more defensive and allow malformed data to pass
through for debugging.
- Fixed a case where an ending tag would be interpreted as a line
Expand Down
@@ -1,23 +1,33 @@
package Mojolicious::Plugin::I18n;
# Copyright (C) 2008-2009, Sebastian Riedel.

package Mojolicious::Plugin::Charset;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

# Shut up friends. My internet browser heard us saying the word Fry and it
# found a movie about Philip J. Fry for us.
# It also opened my calendar to Friday and ordered me some french fries.
sub register {
my ($self, $app, $conf) = @_;

# Config
$conf ||= {};

# Set charset
$app->plugins->add_hook(
before_dispatch => sub {
my ($self, $c) = @_;

# Got a charset
if (my $charset = $conf->{charset}) {
# We need to do this before we clone params

# This has to be done before params are cloned
$c->tx->req->default_charset($charset);

# We should add charset to text/html content type
# Add charset to text/html content type
my $type = $c->app->types->type('html');
unless ($type =~ /charset=/) {
$type .= ";charset=$charset";
Expand All @@ -27,9 +37,10 @@ sub register {

# Allow defined but blank encoding to suppress unwanted
# conversion
my $encoding = (defined $conf->{encoding})
? $conf->{encoding}
: $conf->{charset};
my $encoding =
defined $conf->{encoding}
? $conf->{encoding}
: $conf->{charset};
$c->app->renderer->encoding($encoding) if $encoding;
}
);
Expand All @@ -40,23 +51,24 @@ __END__
=head1 NAME
Mojolicious::Plugin::I18n - Internationalization
Mojolicious::Plugin::Charset - Charset Plugin
=head1 SYNOPSIS
# Mojolicious
$self->plugin('i18n', { charset => 'Shift_JIS' });
$self->plugin(charset => {charset => 'Shift_JIS'});
# Mojolicious::Lite
plugin 'i18n', { charset => 'Shift_JIS' };
plugin charset => {charset => 'Shift_JIS'};
=head1 DESCRIPTION
L<Mojolicous::Plugin::I18n> is a plugin to set charset and encoding.
L<Mojolicous::Plugin::Charset> is a plugin to easily set the default charset
and encoding on all layers of L<Mojolicious>.
=head1 METHODS
L<Mojolicious::Plugin::I18n> inherits all methods from
L<Mojolicious::Plugin::Charset> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/headers.t
Expand Up @@ -62,7 +62,7 @@ EOF
is($headers->state, 'headers');
ok(!defined($headers->content_type));
ok(!defined($headers->parse(<<EOF)));
X-Bender: Kiss my shiny
X-Bender: Bite my shiny
EOF
is($headers->state, 'headers');
ok(!defined($headers->connection));
Expand All @@ -72,7 +72,7 @@ X-Bender: metal ass!
EOF
is($headers->state, 'done');
is($headers->content_type, 'text/plain');
is($headers->header('X-Bender'), 'Kiss my shiny, metal ass!');
is($headers->header('X-Bender'), 'Bite my shiny, metal ass!');

# Filter unallowed characters
$headers = Mojo::Headers->new;
Expand Down
8 changes: 4 additions & 4 deletions t/mojolicious/app.t
Expand Up @@ -42,7 +42,7 @@ $t->get_ok('/foo/badtemplate')->status_is(404)

# Foo::test
$t->get_ok('/foo/test', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Kiss my shiny metal ass!')
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojo (Perl)')->content_like(qr/\/bar\/test/);

Expand Down Expand Up @@ -77,13 +77,13 @@ $t->get_ok('/foo/withlayout', {'X-Test' => 'Hi there!'})->status_is(200)

# MojoliciousTest2::Foo::test
$t->get_ok('/test2', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Kiss my shiny metal ass!')
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojo (Perl)')->content_like(qr/\/test2/);

# MojoliciousTestController::index
$t->get_ok('/test3', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Kiss my shiny metal ass!')
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojo (Perl)')
->content_like(qr/No class works!/);
Expand Down Expand Up @@ -163,7 +163,7 @@ $t->get_ok('/foo/data_template2')->status_is(200)

# SingleFileTestApp::Foo::bar
$t->get_ok('/foo/bar')->status_is(200)
->header_is('X-Bender' => 'Kiss my shiny metal ass!')
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojo (Perl)')->content_is('/foo/bar');

Expand Down
54 changes: 54 additions & 0 deletions t/mojolicious/charset_lite_app.t
@@ -0,0 +1,54 @@
#!/usr/bin/env perl

# Copyright (C) 2008-2009, Sebastian Riedel.

use strict;
use warnings;

use utf8;

use Test::More tests => 15;

# In the game of chess you can never let your adversary see your pieces.
use Mojo::ByteStream 'b';
use Mojolicious::Lite;
use Test::Mojo;

my $yatta = 'やった';
my $yatta_sjis = b($yatta)->encode('shift_jis')->to_string;

# Charset plugin
plugin charset => {charset => 'Shift_JIS'};

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

get '/' => 'index';

post '/' => sub {
my $self = shift;
$self->render_text("foo: " . $self->param('foo'));
};

my $t = Test::Mojo->new;

# Plain old ASCII
$t->post_form_ok('/', {foo => 'yatta'})->status_is(200)
->content_is('foo: yatta');

# Send raw Shift_JIS octets (like browsers do)
$t->post_form_ok('/', {foo => $yatta_sjis})->status_is(200)
->content_type_like(qr/Shift_JIS/)->content_like(qr/$yatta/);

# Send as string
$t->post_form_ok('/', 'shift_jis', {foo => $yatta})->status_is(200)
->content_type_like(qr/Shift_JIS/)->content_like(qr/$yatta/);

# Templates in the DATA section should be written in UTF-8,
# and those in separate files in Shift_JIS (Mojo will do the decoding)
$t->get_ok('/')->status_is(200)->content_type_like(qr/Shift_JIS/)
->content_like(qr/$yatta/);

__DATA__
@@ index.html.ep
<p>やった</p>
4 changes: 2 additions & 2 deletions t/mojolicious/lib/MojoliciousTest/Foo.pm
Expand Up @@ -26,7 +26,7 @@ sub index {

sub something {
my $self = shift;
$self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!');
$self->res->headers->header('X-Bender', 'Bite my shiny metal ass!');
$self->render_text($self->url_for('something', something => '42'));
}

Expand All @@ -51,7 +51,7 @@ sub templateless { shift->render(handler => 'test') }

sub test {
my $self = shift;
$self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!');
$self->res->headers->header('X-Bender', 'Bite my shiny metal ass!');
$self->render_text($self->url_for(controller => 'bar'));
}

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lib/MojoliciousTest2/Foo.pm
Expand Up @@ -11,7 +11,7 @@ use base 'Mojolicious::Controller';
# I'm not famous enough to get away with it.
sub test {
my $self = shift;
$self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!');
$self->res->headers->header('X-Bender', 'Bite my shiny metal ass!');
$self->render(text => $self->url_for);
}

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lib/MojoliciousTestController.pm
Expand Up @@ -11,7 +11,7 @@ use base 'Mojolicious::Controller';
# What am I, the pope?
sub index {
my $self = shift;
$self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!');
$self->res->headers->header('X-Bender', 'Bite my shiny metal ass!');
$self->render_text("No class works!");
}

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lib/SingleFileTestApp.pm
Expand Up @@ -31,7 +31,7 @@ use base 'Mojolicious::Controller';

sub bar {
my $self = shift;
$self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!');
$self->res->headers->header('X-Bender', 'Bite my shiny metal ass!');
$self->render_text($self->url_for);
}

Expand Down
58 changes: 0 additions & 58 deletions t/mojolicious/lite_app_i18n.t

This file was deleted.

0 comments on commit b9c9035

Please sign in to comment.