Skip to content

Commit

Permalink
fixed small detection bug in Mojolicious::Types
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 4, 2011
1 parent 6e069eb commit 7b0cc8b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Changes
Expand Up @@ -7,9 +7,10 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved Mojo::IOLoop to die if started twice.
- Improved documentation.
- Improved tests.
- Fixed many portability issues.
- Fixed small formatting bug in Mojo::Headers.
- Fixed small proxy message generation bug.
- Fixed many portability issues.
- Fixed small detection bug in Mojolicious::Types.

1.99 2011-09-29 00:00:00
- Deprecated direct hash access to the flash in
Expand Down
20 changes: 15 additions & 5 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -254,8 +254,12 @@ For resources with different representations and that require truly
C<RESTful> content negotiation you can also use C<respond_to> instead of
C<render>.

# "Accept: application/json" -> "json"
# "Accept: text/xml" -> "xml"
# /hello (Accept: application/json) -> "json"
# /hello (Accept: text/xml) -> "xml"
# /hello.json -> "json"
# /hello.xml -> "xml"
# /hello?format=json -> "json"
# /hello?format=xml -> "xml"
$self->respond_to(
json => {json => {hello => 'world'}},
xml => {text => '<hello>world</hello>'}
Expand All @@ -276,9 +280,15 @@ parameter.
Callbacks can be used for representations that are too complex to fit into a
single C<render> call.

# "Accept: application/json" -> "json"
# "Accept: text/html" -> "html"
# "Accept: image/png" -> "any"
# /hello (Accept: application/json) -> "json"
# /hello (Accept: text/html) -> "html"
# /hello (Accept: image/png) -> "any"
# /hello.json -> "json"
# /hello.html -> "html"
# /hello.png -> "any"
# /hello?format=json -> "json"
# /hello?format=html -> "html"
# /hello?format=png -> "any"
$self->respond_to(
json => {json => {hello => 'world'}},
html => {template => 'hello', message => 'world'},
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Types.pm
Expand Up @@ -39,9 +39,9 @@ sub detect {
my $pattern = $1;
my @exts;
my $types = $self->types;
for my $ext (keys %$types) {
my $type = $types->{$ext};
$type =~ s/\;.*$//;
for my $ext (sort keys %$types) {
my $type = quotemeta $types->{$ext};
$type =~ s/\\\;.*$//;
push @exts, $ext if $pattern =~ /^$type$/i;
}

Expand Down
14 changes: 7 additions & 7 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -402,7 +402,7 @@ $t->post_ok('/rest.png?format=json')->status_is(201)
# GET /nothing (does not exist)
$t->get_ok('/nothing', {Accept => 'image/png'})->status_is(404);

# GET /rest.html (Internet Explorer)
# GET /rest.html (Internet Explorer 8)
my $ie =
'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml'
. ', image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash'
Expand All @@ -411,19 +411,19 @@ $t->get_ok('/rest.html' => {Accept => $ie})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (Internet Explorer with query)
# GET /rest (Internet Explorer 8 with query)
$t->get_ok('/rest?format=html' => {Accept => $ie})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest.html (WebKit)
my $webkit = 'application/xml,application/xhtml+xml,text/html;q=0.9'
# GET /rest.html (Chrome 11)
my $chrome = 'application/xml,application/xhtml+xml,text/html;q=0.9'
. ',text/plain;q=0.8,image/png,*/*;q=0.5';
$t->get_ok('/rest.html' => {Accept => $webkit})->status_is(200)
$t->get_ok('/rest.html' => {Accept => $chrome})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (WebKit with query)
$t->get_ok('/rest?format=html' => {Accept => $webkit})->status_is(200)
# GET /rest (Chrome 11 with query)
$t->get_ok('/rest?format=html' => {Accept => $chrome})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');
25 changes: 25 additions & 0 deletions t/mojolicious/types.t
@@ -0,0 +1,25 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 13;

# "Your mistletoe is no match for my *tow* missile."
use_ok 'Mojolicious::Types';

# Basics
my $t = Mojolicious::Types->new;
is $t->type('json'), 'application/json', 'right type';
is $t->type('foo'), undef, 'no type';
$t->type(foo => 'foo/bar');
is $t->type('foo'), 'foo/bar', 'right type';

# Detection
is_deeply $t->detect('text/html'), ['htm', 'html'], 'right formats';
is_deeply $t->detect('text/html;q=0.9'), ['htm', 'html'], 'right formats';
is_deeply $t->detect('text/html,*/*'), [], 'no formats';
is_deeply $t->detect('text/html;q=0.9,*/*'), [], 'no formats';
is_deeply $t->detect('text/html,*/*;q=0.9'), [], 'no formats';
is_deeply $t->detect('text/html;q=0.8,*/*;q=0.9'), [], 'no formats';
is_deeply $t->detect('application/octet-stream'), ['bin'], 'right formats';
is_deeply $t->detect('application/x-font-woff'), ['woff'], 'right formats';
is_deeply $t->detect('application/atom+xml'), ['atom'], 'right formats';
1 change: 0 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -13,7 +13,6 @@ BEGIN {

use Test::More tests => 78;

# "Your mistletoe is no match for my *tow* missile."
use Mojo::ByteStream 'b';
use Mojolicious::Lite;
use Test::Mojo;
Expand Down

0 comments on commit 7b0cc8b

Please sign in to comment.