740a24e66912a4f04140cb63111dc6d219fece3a
[catagits/Catalyst-Action-REST.git] / t / data-serializer.t
1 use strict;
2 use warnings;
3 use Test::More qw(no_plan);
4 use FindBin;
5
6 use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" );
7 use Test::Rest;
8
9 use_ok 'Catalyst::Test', 'Test::Serialize';
10
11 my %ctypes =( 
12             'text/x-data-dumper' =>   'Data::Dumper' ,
13             'text/x-data-denter' =>   'Data::Denter' ,
14             'text/x-data-taxi'   =>   'Data::Taxi'   ,
15             'application/x-storable'    =>   'Storable'     ,
16             'application/x-freezethaw'  =>   'FreezeThaw'   ,
17             'text/x-config-general' =>   'Config::General' ,
18             'text/x-php-serialization' =>   'PHP::Serialization' ,
19         );
20
21 my $has_serializer = eval "require Data::Serializer";
22
23 foreach my $content_type (keys(%ctypes)) {
24     my $dso;
25     my $skip = 0;
26     my $loadclass = $ctypes{$content_type};
27     $loadclass =~ s/::/\//g;
28     $loadclass .= '.pm';
29     eval {
30        require $loadclass 
31     };
32     if ($@) {
33         $skip = 1;
34     }
35     SKIP: {
36         skip "$ctypes{$content_type} not installed", 4 if $skip;
37         $dso = Data::Serializer->new( serializer => $ctypes{$content_type} );
38         my $t = Test::Rest->new( 'content_type' => $content_type );
39
40         my $monkey_template = { monkey => 'likes chicken!', };
41         my $mres = request( $t->get( url => '/monkey_get' ) );
42         ok( $mres->is_success, "GET $content_type succeeded" );
43         is_deeply( $dso->raw_deserialize( $mres->content ),
44             $monkey_template, "GET $content_type has the right data" );
45
46         my $post_data = { 'sushi' => 'is good for monkey', };
47         my $mres_post = request(
48             $t->post(
49                 url  => '/monkey_put',
50                 data => $dso->raw_serialize($post_data)
51             )
52         );
53         ok( $mres_post->is_success, "POST $content_type succeeded" );
54         is_deeply(
55             $mres_post->content,
56             "is good for monkey",
57             "POST $content_type data matches"
58         );
59     }
60 }
61
62 1;