File: encoding.t

package info (click to toggle)
libpithub-perl 0.01033-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 928 kB
  • ctags: 321
  • sloc: perl: 3,282; makefile: 7
file content (74 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download | duplicates (2)
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
use FindBin;
use lib "$FindBin::Bin/lib";
use JSON::MaybeXS;
use Pithub::Test::Factory;
use Test::Most;

BEGIN {
    use_ok('Pithub');
    use_ok('Pithub::Users');
}

{
    my $obj = Pithub::Test::Factory->create('Pithub::Users');
    ok $obj->utf8, 'enabled en/decoding json';
    $obj->ua->add_response('users/rwstauner.GET');

    isa_ok $obj, 'Pithub::Users';

    {
        my $result = $obj->get( user => 'rwstauner' );
        ok $result->utf8, 'enabled en/decoding json';

        my $string = $result->content->{bio};
        is $string, "\x{26F0}", 'Attribute exists';
        ok utf8::is_utf8($string), 'field is a character string';

        utf8::encode($string);
        ok !utf8::is_utf8($string), 'encoded to a byte string';
        is $string, "\xe2\x9b\xb0", 'string encodes to utf-8';
    }
}

{
    my $json = JSON->new->utf8;
    my $p    = Pithub::Test::Factory->create('Pithub');
    ok $p->utf8, 'enabled en/decoding json';
    $p->token('123');
    my $request = $p->request( method => 'POST', path => '/foo', data => { some => "bullet \x{2022}" } )->request;
    like $request->content, qr/bullet \xe2\x80\xa2/, 'character string utf-8 encoded in request';
    eq_or_diff $json->decode( $request->content ), { some => "bullet \x{2022}" }, 'character strings preserved in json round-trip';
}

{
    my $obj = Pithub::Test::Factory->create('Pithub::Users', utf8 => 0); # disable en/decoding
    ok !$obj->utf8, 'disabled en/decoding json';
    $obj->ua->add_response('users/rwstauner.GET');

    isa_ok $obj, 'Pithub::Users';

    {
        my $result = $obj->get( user => 'rwstauner' );
        ok !$result->utf8, 'disabled en/decoding json';

        my $string = $result->content->{bio};
        is $string, "\xe2\x9b\xb0", 'Attribute exists';
        ok utf8::is_utf8($string), 'field is a character string';

        utf8::decode($string);
        ok utf8::is_utf8($string), 'decoded to a wide character';
        is $string, "\x{26F0}", 'string decodes to a wide character';
    }
}

{
    my $json = JSON->new;
    my $p    = Pithub::Test::Factory->create('Pithub', utf8 => 0); # disable en/decoding
    ok !$p->utf8, 'disabled en/decoding json';
    $p->token('123');
    my $request = $p->request( method => 'POST', path => '/foo', data => { some => "bullet \xe2\x80\xa2" } )->request;
    like $request->content, qr/bullet \xe2\x80\xa2/, 'character string utf-8 encoded in request';
    eq_or_diff $json->decode( $request->content ), { some => "bullet \xe2\x80\xa2" }, 'character strings preserved in json round-trip';
}

done_testing;