File: 04_charset_server.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 (122 lines) | stat: -rw-r--r-- 3,477 bytes parent folder | download | duplicates (3)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use Test::More import => ['!pass'];
use strict;
use warnings;
use Dancer::ModuleLoader;
use Dancer;
use Encode;

# Ensure a recent version of HTTP::Headers
my $min_hh = 5.827;

plan skip_all => "skip test with Test::TCP in win32" if $^O eq 'MSWin32';
plan skip_all => "Test::TCP is needed for this test"
    unless Dancer::ModuleLoader->load("Test::TCP" => "1.30");

plan skip_all => "HTTP::Headers $min_hh required (use of content_type_charset)"
    unless Dancer::ModuleLoader->load( 'HTTP::Headers', $min_hh );

use HTTP::Tiny::NoProxy;

plan tests => 10;

Test::TCP::test_tcp(
    client => sub {
        my $port = shift;
        my $ua = HTTP::Tiny::NoProxy->new;
        my $res = $ua->post_form("http://127.0.0.1:$port/name", [ name => 'vasya' ]);

        my $headers = HTTP::Headers->new(%{$res->{headers}});
        is $headers->content_type, 'text/html';
        ok $headers->content_type_charset; # we always have charset if the setting is set
        is $res->{content}, 'Your name: vasya';

        $res = $ua->get("http://127.0.0.1:$port/unicode");

        $headers = HTTP::Headers->new(%{$res->{headers}});
        is $headers->content_type, 'text/html';
        is $headers->content_type_charset, 'UTF-8';
        is $res->{content}, Encode::encode('utf-8', "cyrillic shcha \x{0429}");
    },
    server => sub {
        my $port = shift;

        use lib "t/lib";
        use TestApp;
        Dancer::Config->load;

        set( charset      => 'utf-8',
             environment  => 'production',
             port         => $port,
             server       => '127.0.0.1',
             startup_info => 0 );
        Dancer->dance();
    },
);

Test::TCP::test_tcp(
    client => sub {
        my $port = shift;
        my $ua = HTTP::Tiny::NoProxy->new;

        my $res = $ua->get("http://127.0.0.1:$port/unicode-content-length");

        my $headers = HTTP::Headers->new(%{$res->{headers}});
        is $headers->content_type, 'text/html';
        # UTF-8 seems to be Dancer's default encoding
        my $v = "\x{100}0123456789";
        utf8::encode($v);
        is $res->{content}, $v;
    },
    server => sub {
        my $port = shift;

        use lib "t/lib";
        use TestAppUnicode;
        Dancer::Config->load;

        set(
            # no charset
            environment  => 'production',
            port         => $port,
            server       => '127.0.0.1',
            startup_info => 0,
        );
        Dancer->dance;
    },
);

SKIP: {
    skip "JSON module required for test", 2 
        unless Dancer::ModuleLoader->load('JSON');

    Test::TCP::test_tcp(
        client => sub {
            my $port = shift;
            my $ua = HTTP::Tiny::NoProxy->new;

            my $res = $ua->get("http://127.0.0.1:$port/unicode-content-length-json");

            my $headers = HTTP::Headers->new(%{$res->{headers}});
            is $headers->content_type, 'application/json';
            is_deeply(from_json($res->{content}), { test => "\x{100}" });
        },
        server => sub {
            my $port = shift;

            use lib "t/lib";
            use TestAppUnicode;
            Dancer::Config->load;

            set(
                # no charset
                environment  => 'production',
                port         => $port,
                server       => '127.0.0.1',
                startup_info => 0,
                serializer   => 'JSON',
            );
            Dancer->dance;
        },
    );

}