+ Support official application/json and carp about text/x-json
+
Thu Jan 3 17:23:58 PST 2008 (adam) - Release 0.60
Updated my contact information.
Prepped for release.
my $content_type = $c->request->preferred_content_type || '';
+ # carp about old text/x-json
+ if ($content_type eq 'text/x-json') {
+ $c->log->info('Using deprecated text/x-json content-type.');
+ $c->log->info('Use application/json instead!');
+ }
+
# Finally, we load the class. If you have a default serializer,
# and we still don't have a content-type that exists in the map,
# we'll use it.
'text/html' => 'YAML::HTML',
'text/xml' => 'XML::Simple',
'text/x-yaml' => 'YAML',
+ 'application/json' => 'JSON',
'text/x-json' => 'JSON',
'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
'text/html' => 'YAML::HTML',
'text/xml' => 'XML::Simple',
'text/x-yaml' => 'YAML',
+ 'application/json' => 'JSON',
'text/x-json' => 'JSON',
'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
=head1 SYNOPSIS
- if ( $c->request->accepts('text/x-json') ) {
+ if ( $c->request->accepts('application/json') ) {
...
}
use strict;
use warnings;
-use Test::More tests => 21;
+use Test::More tests => 24;
use FindBin;
use lib ( "$FindBin::Bin/../lib" );
$request->headers( HTTP::Headers->new );
$request->parameters( {} );
$request->method('GET');
+ $request->content_type('application/json');
+ $request->headers->header(
+ 'Accept' =>
+ # From Firefox 2.0 when it requests an html page
+ 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
+ );
+
+ is_deeply( $request->accepted_content_types,
+ [ qw( application/json
+ text/xml application/xml application/xhtml+xml
+ image/png
+ text/html
+ text/plain
+ */*
+ ) ],
+ 'accept header is parsed properly, and content-type header has precedence over accept' );
+ ok( ! $request->accept_only, 'accept_only is false' );
+}
+
+{
+ my $request = Catalyst::Request::REST->new;
+ $request->{_context} = 'MockContext';
+ $request->headers( HTTP::Headers->new );
+ $request->parameters( {} );
+ $request->method('GET');
$request->content_type('text/x-json');
$request->headers->header(
'Accept' => 'text/plain,text/x-json',
'each type appears only once' );
}
+{
+ my $request = Catalyst::Request::REST->new;
+ $request->{_context} = 'MockContext';
+ $request->headers( HTTP::Headers->new );
+ $request->parameters( {} );
+ $request->method('GET');
+ $request->content_type('application/json');
+ $request->headers->header(
+ 'Accept' => 'text/plain,application/json',
+ );
+
+ is_deeply( $request->accepted_content_types,
+ [ qw( application/json
+ text/plain
+ ) ],
+ 'each type appears only once' );
+}
package MockContext;
use strict;
use warnings;
-use Test::More tests => 5;
+use Test::More tests => 9;
use FindBin;
use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib");
use_ok 'Catalyst::Test', 'Test::Serialize';
-my $t = Test::Rest->new('content_type' => 'text/x-json');
-
-my $has_serializer = eval "require JSON::Syck";
SKIP: {
- skip "JSON::Syck not available", 4, unless $has_serializer;
+ my $has_serializer = eval "require JSON::Syck";
+
+ skip "JSON::Syck not available", 8, unless $has_serializer;
- my $monkey_template = {
- monkey => 'likes chicken!',
- };
- my $mres = request($t->get(url => '/monkey_get'));
- ok( $mres->is_success, 'GET the monkey succeeded' );
- is_deeply(JSON::Syck::Load($mres->content), $monkey_template, "GET returned the right data");
+ for ('text/x-json', 'application/json') {
+ my $t = Test::Rest->new('content_type' => $_);
+ my $monkey_template = {
+ monkey => 'likes chicken!',
+ };
+ my $mres = request($t->get(url => '/monkey_get'));
+ ok( $mres->is_success, 'GET the monkey succeeded' );
+ is_deeply(JSON::Syck::Load($mres->content), $monkey_template, "GET returned the right data");
- my $post_data = {
- 'sushi' => 'is good for monkey',
- };
- my $mres_post = request($t->post(url => '/monkey_put', data => JSON::Syck::Dump($post_data)));
- ok( $mres_post->is_success, "POST to the monkey succeeded");
- is_deeply($mres_post->content, "is good for monkey", "POST data matches");
+ my $post_data = {
+ 'sushi' => 'is good for monkey',
+ };
+ my $mres_post = request($t->post(url => '/monkey_put', data => JSON::Syck::Dump($post_data)));
+ ok( $mres_post->is_success, "POST to the monkey succeeded");
+ is_deeply($mres_post->content, "is good for monkey", "POST data matches");
+ }
};
1;
'text/html' => 'YAML::HTML',
'text/xml' => 'XML::Simple',
'text/x-yaml' => 'YAML',
+ 'application/json' => 'JSON',
'text/x-json' => 'JSON',
'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],