File: 03_charset.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (89 lines) | stat: -rw-r--r-- 2,565 bytes parent folder | download | duplicates (6)
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
83
84
85
86
87
88
89
use Test::More import => ['!pass'];
use strict;
use warnings;

use Dancer;
use Dancer::ModuleLoader;

use Encode;

plan tests => 16;

set environment => 'production';

my $res = Dancer::Response->new(headers => [ 'Content-Type' => 'text/html' ], content_type => 'text/html');
my $psgi_res = Dancer::Handler->render_response($res);
is(@$psgi_res, 3);
is($psgi_res->[0], 200, 'default status');
is_deeply($psgi_res->[1], [ 'Content-Length', 0, 'Content-Type' => 'text/html' ], 'default headers');
is_deeply($psgi_res->[2], [''], 'default content');

ok $res->content_type('text/plain');
ok $res->content('123');

is_deeply(Dancer::Handler->render_response($res),
    [
        200,
        [ 'Content-Length', 0, 'Content-Type', 'text/plain' ],
        [ '123' ],
    ],
);

setting charset => 'utf-8';

is_deeply(Dancer::Handler->render_response($res),
    [
        200,
        [ 'Content-Length', 0, 'Content-Type', 'text/plain; charset=utf-8' ],
        [ '123' ],
    ],
);

ok $res->content("\x{0429}");   # cyrillic shcha -- way beyond latin1

is_deeply(Dancer::Handler->render_response(Dancer::Serializer->process_response($res)),
    [
        200,
        [ 'Content-Length', 0, 'Content-Type', 'text/plain; charset=utf-8' ],
        [ Encode::encode('utf-8', "\x{0429}") ],
    ],
);

SKIP: {
    skip "JSON is needed for this test" , 3
        unless Dancer::ModuleLoader->load('JSON');

    setting serializer => 'JSON';
    ok $res->content_type('application/json');
    ok $res->content({ key => 'value'});

    is_deeply(Dancer::Handler->render_response(Dancer::Serializer->process_response($res)),
        [
            200,
            [ 'Content-Length', 0, 'Content-Type', 'application/json; charset=utf-8' ],
            [ JSON::to_json({ key => 'value' }) ],
        ],
    );
}

SKIP: {
    skip "XML::Simple is needed for this test" , 3
        unless Dancer::ModuleLoader->load('XML::Simple');

    skip "XML::Parser or XML::SAX are needed to run this test", 3
        unless Dancer::ModuleLoader->load('XML::Parser') or
               Dancer::ModuleLoader->load('XML::SAX');

    setting serializer => 'XML';
    ok $res->content_type('text/xml');
    ok $res->content({ key => "\x{0429}" });

    is_deeply(Dancer::Handler->render_response(Dancer::Serializer->process_response($res)),
        [
            200,
            [ 'Content-Length', 0, 'Content-Type', 'text/xml; charset=utf-8' ],
            [ Encode::encode('utf-8', XML::Simple::XMLout( { key => "\x{0429}"
                    }, RootName => 'data' )) ],
        ],
    );
}