File: 022-body-encoding.t

package info (click to toggle)
libweb-machine-perl 0.17-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: perl: 5,481; makefile: 2
file content (275 lines) | stat: -rw-r--r-- 6,369 bytes parent folder | download | duplicates (5)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use Encode qw( decode is_utf8 );
use HTTP::Message::PSGI;
use HTTP::Request::Common qw( GET );
use Plack::Util;

use Web::Machine;

my $tb = Test::Builder->new;
binmode $_, ':encoding(UTF-8)'
    for $tb->output, $tb->failure_output, $tb->todo_output;

{
    package My::Resource::Test022::Base;
    use strict;
    use warnings;

    use Encode qw( encode );

    use parent 'Web::Machine::Resource';

    sub allowed_methods { [qw[ GET ]] }
    sub content_types_provided { [ { 'text/plain' => 'body' } ] }

    # The o with umlauts is encoded as 0xc3 0xb6 in UTF-8 and as 0xf6 in
    # ISO-8859-1.
    our $Body = do {
        use utf8;
        "Hellö Wörld";
    };

    sub body {
        my $self = shift;

        if ( $self->request->parameters->{stream} ) {
            my $bytes = encode( 'UTF-8', $Body );
            open my $fh, '<:encoding(UTF-8)', \$bytes;
            return $fh;
        }
        else {
            return $Body;
        }
    }
}

{
    package My::Resource::Test022::Pairs;
    use strict;
    use warnings;

    use Encode qw( encode );

    use parent -norequire, 'My::Resource::Test022::Base';

    sub encodings_provided {
        return {
            identity => sub { $_[1] },
            'add-x'  => sub { $_[1] . 'x' },
        };
    }

    sub charsets_provided {
        return [
            {
                'UTF-8' => sub { encode( 'UTF-8', $_[1] ) }
            },
            {
                'ISO-8859-1' => sub { encode( 'ISO-8859-1', $_[1] ) }
            },
        ];
    }

    sub default_charset {
        return {
            'UTF-8' => sub { encode( 'UTF-8', $_[1] ) }
        };
    }
}

{
    package My::Resource::Test022::Strings;
    use strict;
    use warnings;

    use parent -norequire, 'My::Resource::Test022::Base';

    sub charsets_provided {
        return [qw( UTF-8 ISO-8859-1 )];
    }

    sub default_charset {
        return 'UTF-8';
    }
}

# In order to test this properly we can't use test_psgi. That passes the
# response through HTTP::Response, which ends up doing an unconditional call
# to utf8::downgrade on the reponse body. That makes it hard to test how
# encodings are being handled!
ok(
    is_utf8($My::Resource::Test022::Base::Body),
    'text in resource is marked as UTF-8'
);

my %tests = (
    'UTF-8' => [
        0x48,    # H
        0x65,    # e
        0x6c,    # l
        0x6c,    # l
        0xc3,    # [UTF-8 o with umlauts - byte 1]
        0xb6,    # [UTF-8 o with umlauts - byte 2]
        0x20,    # [space]
        0x57,    # W
        0xc3,    # [UTF-8 o with umlauts - byte 1]
        0xb6,    # [UTF-8 o with umlauts - byte 2]
        0x72,    # r
        0x6c,    # l
        0x64,    # d
    ],
    'ISO-8859-1' => [
        0x48,    # H
        0x65,    # e
        0x6c,    # l
        0x6c,    # l
        0xf6,    # [ISO-8859-1 o with umlauts]
        0x20,    # [space]
        0x57,    # W
        0xf6,    # [ISO-8859-1 o with umlauts]
        0x72,    # r
        0x6c,    # l
        0x64,    # d
    ],
);

for my $resource (qw( Pairs Strings )) {
    my $app = Web::Machine->new(
        resource => 'My::Resource::Test022::' . $resource )->to_app;

    my $desc = $resource;
    for my $stream ( 0, 1 ) {
        $desc .= $stream ? ' - body as stream' : ' - body as arrayref';

        for my $charset ( sort keys %tests ) {
            test_charset(
                app            => $app,
                charset        => $charset,
                charset_header => 1,
                bytes          => $tests{$charset},
                stream         => $stream,
                description    => "$desc - $charset",
            );
        }

        test_charset(
            app            => $app,
            charset        => 'UTF-8',
            charset_header => 0,
            bytes          => $tests{'UTF-8'},
            stream         => $stream,
            description    => "$desc - no Accept-Charset header",
        );

        next if $resource eq 'Strings';

        test_encoding(
            app         => $app,
            stream      => $stream,
            description => "$desc - encoding test",
        );
    }
}

done_testing;

sub test_charset {
    my %args = @_;

    my $uri = _uri(%args);

    my @headers
        = $args{charset_header} ? ( 'Accept-Charset' => $args{charset} ) : ();
    my $env = GET( $uri, @headers )->to_psgi;

    my $response = $args{app}->($env);

    ok(
        $response->[0],
        "status code is 200 - $args{description}"
    );

    my $body = _body( $response, $args{stream} );

    ok(
        !is_utf8($body),
        "body is bytes, not characters -  - $args{description}"
    );

    is(
        decode( $args{charset}, $body ),
        $My::Resource::Test022::Base::Body,
        "body decoded as $args{charset} matches original - $args{description}"
    );

    is_deeply(
        [ map { ord($_) } split //, $body ],
        $args{bytes},
        "body contains the expected $args{charset} bytes - $args{description}"
    );

    unless ($args{stream}) {
        is(
            Plack::Util::header_get($response->[1], "content-length"),
            scalar @{$args{bytes}},
            "content-length matches the expected number of $args{charset} bytes - $args{description}"
        );
    }
}

sub test_encoding {
    my %args = @_;

    my $uri = _uri(%args);
    my $env = GET(
        $uri,
        'Accept-Charset'  => 'UTF-8',
        'Accept-Encoding' => 'add-x',
    )->to_psgi;

    my $response = $args{app}->($env);

    ok(
        $response->[0],
        "status code is 200 - $args{description}"
    );

    my $body = _body( $response, $args{stream} );
    ok(
        !is_utf8($body),
        "body is bytes, not characters - $args{description}"
    );

    is(
        decode( 'UTF-8', $body ),
        $My::Resource::Test022::Base::Body . 'x',
        "body has an x at the end with add-x encoding - $args{description}"
    );
}

sub _uri {
    my %args = @_;
    return $args{stream} ? '/?stream=1' : '/';
}

sub _body {
    my $response = shift;
    my $stream   = shift;

    if ($stream) {
        return do {
            my $fh = $response->[2];
            local $/;
            <$fh>;
        };
    }
    else {
        return join q{}, @{ $response->[2] };
    }
}