File: 02_prepare_serializer_for_format.t

package info (click to toggle)
libdancer-plugin-rest-perl 0.11-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 172 kB
  • sloc: perl: 151; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (5)
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
use strict;
use warnings;
use Dancer::ModuleLoader;
use Test::More import => ['!pass'];

plan skip_all => "JSON is needed for this test"
    unless Dancer::ModuleLoader->load('JSON');
plan skip_all => "YAML is needed for this test"
    unless Dancer::ModuleLoader->load('YAML');


my $data = { foo => 42 };
my $json = JSON::encode_json($data);
my $yaml = YAML::Dump($data);

{
    package Webservice;
    use Dancer;
    use Dancer::Plugin::REST;

    setting environment => 'testing';

    prepare_serializer_for_format;

    get '/' => sub { "root" };
    get '/:something.:format' => sub {
        $data;
    };
}
use Dancer::Test;

my @tests = (
    {
        request => [GET => '/'],
        content_type => 'text/html',
        response => 'root',
    },
    { 
        request => [GET => '/foo.json'],
        content_type => 'application/json',
        response => $json
    },
    { 
        request => [GET => '/foo.yml'],
        content_type => 'text/x-yaml',
        response => $yaml,
    },
    {
        request => [GET => '/'],
        content_type => 'text/html',
        response => 'root',
    },
);

plan tests => scalar(@tests) * 2;

for my $test ( @tests ) {
    my $response = dancer_response(@{ $test->{request} });
    is($response->header('Content-Type'), 
       $test->{content_type},
       "headers have content_type set to ".$test->{content_type});

    is( $response->{content}, $test->{response},
        "\$data has been encoded" );
}