File: encoding.t

package info (click to toggle)
libpithub-perl 0.01043-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: perl: 9,098; makefile: 7
file content (91 lines) | stat: -rwxr-xr-x 2,811 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;

use JSON::MaybeXS     qw( JSON );
use Pithub            ();
use Pithub::Users     ();
use Test::Differences qw( eq_or_diff );
use Test::More import => [qw( done_testing is isa_ok like ok )];

use lib 't/lib';
use Pithub::Test::Factory ();

{
    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;