Skip to content

Commit

Permalink
better content negotiation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 14, 2012
1 parent eaf1402 commit 75b17c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Types.pm
Expand Up @@ -38,7 +38,7 @@ sub detect {
# Extract and prioritize MIME types
my %types;
for my $type (split /,/, $accept // '') {
next unless $type =~ /^\s*([^,; ]+)(?:.*\;\s*q=([\d.]+))?.*$/i;
next unless $type =~ /^\s*([^,; ]+)(?:\s*\;\s*q=([\d.]+))?\s*$/i;
$types{lc $1} = $2 // 1;
}
my @types = sort { $types{$b} <=> $types{$a} } keys %types;
Expand Down
9 changes: 8 additions & 1 deletion t/mojolicious/restful_lite_app.t
Expand Up @@ -434,6 +434,13 @@ $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 (Ajax)
my $ajax = 'text/html;q=0.1,application/xml';
$t->get_ok(
'/rest' => {Accept => $ajax, 'X-Requested-With' => 'XMLHttpRequest'})
->status_is(200)->content_type_is('application/xml')
->text_is(just => 'works');

# GET /rest.html (Internet Explorer 8)
my $ie
= 'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml'
Expand All @@ -460,7 +467,7 @@ $t->get_ok('/rest?format=html' => {Accept => $chrome})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (jQuery Ajax)
# GET /rest (jQuery 1.8)
my $jquery = 'application/json, text/javascript, */*; q=0.01';
$t->get_ok(
'/rest' => {Accept => $jquery, 'X-Requested-With' => 'XMLHttpRequest'})
Expand Down
5 changes: 2 additions & 3 deletions t/mojolicious/types.t
Expand Up @@ -39,7 +39,7 @@ is_deeply $t->detect('application/zip'), ['zip'], 'right format';

# Detect special cases
is_deeply $t->detect('Application/Xml'), ['xml'], 'right formats';
is_deeply $t->detect('Text/Xml'), ['xml'], 'right formats';
is_deeply $t->detect(' Text/Xml '), ['xml'], 'right formats';
is_deeply $t->detect('APPLICATION/XML'), ['xml'], 'right formats';
is_deeply $t->detect('TEXT/XML'), ['xml'], 'right formats';
is_deeply $t->detect('text/html;q=0.9'), ['htm', 'html'], 'right formats';
Expand Down Expand Up @@ -68,8 +68,7 @@ is $t->type('html'), 'text/html;charset=UTF-8', 'right type';

# Prioritize
is_deeply $t->detect('text/plain', 1), ['txt'], 'right formats';
is_deeply $t->detect('TEXT/HTML;V=1.0; q=0.8 ; foo=bar', 1), ['htm', 'html'],
'right formats';
is_deeply $t->detect('TEXT/HTML; q=0.8 ', 1), ['htm', 'html'], 'right formats';
is_deeply $t->detect('TEXT/HTML;Q=0.8,text/plain;Q=0.9', 1),
['txt', 'htm', 'html'], 'right formats';
is_deeply $t->detect(' TEXT/HTML , text/plain;Q=0.9', 1),
Expand Down

0 comments on commit 75b17c6

Please sign in to comment.