File: 12_utf8_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 (69 lines) | stat: -rw-r--r-- 1,786 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
use strict;
use warnings;

use utf8;
use Encode;
use Test::More import => ['!pass'];
use Dancer::ModuleLoader;
use HTTP::Tiny::NoProxy;

use FindBin qw($Bin);
use lib "$Bin/../../";

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

plan tests => 4;

Test::TCP::test_tcp(
    client => sub {
        my $port = shift;
        my $res;

        $res = _get_http_response(GET => '/string', $port);
        is d($res->{content}), "\x{1A9}", "utf8 static response";

        $res = _get_http_response(GET => '/other/string', $port);
        is d($res->{content}), "\x{1A9}", "utf8 response through forward";

        $res = _get_http_response(GET => "/param/".u("\x{1A9}"), $port);
        is d($res->{content}), "\x{1A9}", "utf8 route param";

        $res = _get_http_response(GET => "/view?string1=".u("\x{E9}"), $port);
        is d($res->{content}), "sigma: 'Ʃ'\npure_token: 'Ʃ'\nparam_token: '\x{E9}'\n",
            "params and tokens are valid unicode";
    },
    server => sub {
        my $port = shift;

        use Dancer;
        use t::lib::TestAppUnicode;

        set( charset      => 'utf8',
             host         => '127.0.0.1',
             port         => $port,
             show_errors  => 1,
             startup_info => 0,
             log          => 'debug',
             logger       => 'console');

        Dancer->dance();
    },
);

sub u {
    encode('UTF-8', $_[0]);
}

sub d {
    decode('UTF-8', $_[0]);
}

sub _get_http_response {
    my ($method, $path, $port) = @_;

    my $ua = HTTP::Tiny::NoProxy->new;
    return $ua->request($method => "http://127.0.0.1:$port${path}");
}