File: data-serializer.t

package info (click to toggle)
libcatalyst-action-serialize-data-serializer-perl 1.08-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 248 kB
  • sloc: perl: 2,409; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,564 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use strict;
use warnings;
use Test::More;
use FindBin;

use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" );
use Test::Rest;

use_ok 'Catalyst::Test', 'Test::Serialize';

my %ctypes =( 
            'text/x-data-dumper' =>   'Data::Dumper' ,
            'text/x-data-denter' =>   'Data::Denter' ,
            'text/x-data-taxi'   =>   'Data::Taxi'   ,
            'application/x-storable'    =>   'Storable'     ,
            'application/x-freezethaw'  =>   'FreezeThaw'   ,
            'text/x-config-general' =>   'Config::General' ,
            'text/x-php-serialization' =>   'PHP::Serialization' ,
        );

my $has_serializer = eval "require Data::Serializer";

foreach my $content_type (keys(%ctypes)) {
    my $dso;
    my $skip = 0;
    my $loadclass = $ctypes{$content_type};
    $loadclass =~ s/::/\//g;
    $loadclass .= '.pm';
    eval {
       require $loadclass 
    };
    if ($@) {
        $skip = 1;
    }
    SKIP: {
        skip "$ctypes{$content_type} not installed", 4 if $skip;
        $dso = Data::Serializer->new( serializer => $ctypes{$content_type} );
        my $t = Test::Rest->new( 'content_type' => $content_type );

        my $monkey_template = { monkey => 'likes chicken!', };
        my $mres = request( $t->get( url => '/monkey_get' ) );
        ok( $mres->is_success, "GET $content_type succeeded" );
        is_deeply( $dso->raw_deserialize( $mres->content ),
            $monkey_template, "GET $content_type has the right data" );

        my $post_data = { 'sushi' => 'is good for monkey', };
        my $mres_post = request(
            $t->post(
                url  => '/monkey_put',
                data => $dso->raw_serialize($post_data)
            )
        );
        ok( $mres_post->is_success, "POST $content_type succeeded" );
        is_deeply(
            $mres_post->content,
            "is good for monkey",
            "POST $content_type data matches"
        );
    }
}

{
        my $t = Test::Rest->new( 'content_type' => 'text/x-data-dumper' );

        my $post_data = "{ 'sushi' => die('hack attempt') }";
        my $mres_post = request(
            $t->post(
                url  => '/monkey_put',
                data => $post_data,
            )
        );
        ok( ! $mres_post->is_success, "POST Data::Dumper fails due to invalid input" );
        like(
            $mres_post->content,
            qr%Content-Type text/x-data-dumper had a problem with your request.*'die' trapped by operation mask%s,
            "POST Data::Dumper data error matches"
        );
}

1;

done_testing;