File: 104-filter_web.t

package info (click to toggle)
libdata-printer-perl 1.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,364; makefile: 7; sh: 1
file content (493 lines) | stat: -rw-r--r-- 14,938 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use strict;
use warnings;
use Test::More tests => 21;
use Data::Printer::Object;

test_json();
test_cookies();
test_http_request();
test_http_response();
exit;

sub test_http_request {
    SKIP: {
        my $error = !eval { require HTTP::Request; 1 };
        skip 'HTTP::Request not available', 1 if $error;
        my $r = HTTP::Request->new(
            'POST',
            'http://www.example.com/ddp',
            [
                'Content-Type'  => 'application/json; charset=UTF-8',
                'Cache-Control' => 'no-cache, must-revalidate',
            ],
            '{"foo":"bar","baz":42}'
        );
        skip 'HTTP::Headers is too old', 1 unless $r->headers->can('flatten');
        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            filters   => ['Web'],
        );
        is(
            $ddp->parse($r),
            'POST http://www.example.com/ddp {
    headers: {
        Cache-Control   "no-cache, must-revalidate",
        Content-Type    "application/json; charset=UTF-8"
    }
    content: {"foo":"bar","baz":42}
}',
            'HTTP::Request'
        );
    };
}

sub test_http_response {
    SKIP: {
        my $error = !eval { require HTTP::Response; 1 };
        skip 'HTTP::Response not available', 1 if $error;
        my $r = HTTP::Response->new(
            '200',
            'OK',
            [
                'Content-Type'  => 'application/json; charset=UTF-8',
                'Cache-Control' => 'no-cache, must-revalidate',
            ],
            '{"foo":"bar","baz":42}'
        );
        skip 'HTTP::Headers is too old', 1 unless $r->headers->can('flatten');
        $r->previous(
            HTTP::Response->new(
                '302', 'Moved Temporarily',[
                    'Location' => 'https://example.com/original'
                ]
            )
        );
        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            filters   => ['Web'],
        );
        is(
            $ddp->parse($r),
            "\x{e2}\x{a4}\x{bf}" . ' 302 Moved Temporarily (https://example.com/original)
200 OK {
    headers: {
        Cache-Control   "no-cache, must-revalidate",
        Content-Type    "application/json; charset=UTF-8"
    }
    content: {"foo":"bar","baz":42}
}',
            'HTTP::Response'
        );
    };
}


sub test_cookies {
    test_mojo_cookie();
    test_dancer_cookie();
    test_dancer2_cookie();
}

sub test_dancer_cookie {
    SKIP: {
        my $error = !eval { require Dancer::Cookie; 1 };
        skip 'Dancer::Cookie not available', 1 if $error;
        my $c = Dancer::Cookie->new(
            name      => 'ddp',
            value     => 'test',
            expires   => time,
            domain    => 'localhost',
            path      => '/test',
            secure    => 1,
            http_only => 1,
        );

        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            filters   => ['Web'],
        );

        like(
            $ddp->parse($c),
            qr{ddp=test; expires=(?:[^;]+); domain=localhost; path=/test; secure; http-only \(Dancer::Cookie\)},
            'Dancer::Cookie parsed correctly'
        );
    };
}

sub test_dancer2_cookie {
    SKIP: {
        my $error = !eval { require Dancer2::Core::Cookie; 1 };
        skip 'Dancer2::Core::Cookie not available', 1 if $error;
        my $c = Dancer2::Core::Cookie->new(
            name      => 'ddp',
            value     => 'test',
            expires   => time,
            domain    => 'localhost',
            path      => '/test',
            secure    => 1,
            http_only => 1,
        );

        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            filters   => ['Web'],
        );

        like(
            $ddp->parse($c),
            qr{ddp=test; expires=(?:[^;]+); domain=localhost; path=/test; secure; http-only \(Dancer2::Core::Cookie\)},
            'Dancer2::Core::Cookie parsed correctly'
        );
    };
}


sub test_mojo_cookie {
    SKIP: {
        my $error = !eval { require Mojo::Cookie::Response; 1 };
        skip 'Mojo::Cookie::Response not available', 1 if $error;
        my $c = Mojo::Cookie::Response->new;
        $c->name('ddp');
        $c->value('test');
        $c->expires( time );
        $c->httponly(1);
        $c->max_age(60);
        $c->path('/test');
        $c->secure(1);
        $c->host_only(0) if $c->can('host_only');
        $c->domain('localhost');

        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            filters   => ['Web'],
        );

        like(
            $ddp->parse($c),
            qr{ddp=test; expires=.+?; domain=localhost; path=/test; secure; http-only; max-age=60 \(Mojo::Cookie\)},
            'Mojo::Cookie parsed correctly'
        );
    };
}

sub test_json {
    my $json = '{"alpha":true,"bravo":false,"charlie":true,"delta":false}';
    my $expected = '{ alpha:true, bravo:false, charlie:true, delta:false }';
    test_json_pp($json, $expected);
    test_json_xs($json, $expected);
    test_json_json($json, $expected);
    test_json_any($json, $expected);
    test_json_maybexs($json, $expected);
    test_json_dwiw($json, $expected);
    test_json_parser($json, $expected);
    test_json_sl($json, $expected);
    test_json_mojo($json, $expected);
    test_json_pegex($json, $expected);
    test_json_cpanel($json, $expected);
    test_json_tiny($json, $expected);
    test_json_typist();
}

sub test_json_typist {
    SKIP: {
        my $error = !eval { require JSON::Typist; require JSON; 1 };
        skip 'JSON::Typist (or JSON, or both) not available', 1 if $error;
        diag('filter for JSON::Typist ' . $JSON::Typist::VERSION);

        my $ddp = Data::Printer::Object->new(
            colored       => 0,
            multiline     => 0,
            show_readonly => 0,
            filters       => ['Web'],
        );

        my $json = '{ "trueVal": true, "falseVal": false, "strVal": "123", "numVal": 123 }';
        my $obj = JSON->new;
        my $payload;
        if ($obj->can('convert_blessed') && $obj->can('canonical') && $obj->can('decode')) {
            $payload = $obj->convert_blessed->canonical->decode($json);
        }
        else {
            skip 'not sure how to load JSON object for JSON::Typist', 1;
        }
        my $typist =  JSON::Typist->new->apply_types( $payload );
        is(
            $ddp->parse($typist),
            '{ falseVal:false, numVal:123, strVal:"123", trueVal:true }',
            'JSON::Typist properly parsed'
        );
    };
}

sub test_json_pp {
    my ($json, $expected) = @_;
    SKIP: {
        my $error = !eval { require JSON::PP; 1 };
        skip 'JSON::PP not available', 1 if $error;
        diag('filter tests for ' . $JSON::PP::VERSION);

        my $ddp = Data::Printer::Object->new(
            colored   => 0,
            multiline => 0,
            filters   => ['Web'],
        );

        my $data = JSON::PP::decode_json($json);
        is( $ddp->parse($data), $expected, 'JSON::PP booleans parsed' );
    };
}

sub test_json_xs {
    my ($json, $expected) = @_;
    SKIP: {
        my $error = !eval { require JSON::XS; 1 };
        skip 'JSON::XS not available', 1 if $error;
        diag('filter tests for JSON::XS ' . $JSON::XS::VERSION);

        my $ddp = Data::Printer::Object->new(
            colored       => 0,
            multiline     => 0,
            show_readonly => 0,
            filters       => ['Web'],
        );

        my $data = JSON::XS::decode_json($json);
        is( $ddp->parse($data), $expected, 'JSON::XS booleans parsed' );
    };
}

sub test_json_json {
    my ($json, $expected) = @_;
    SKIP: {
        my $error = !eval { require JSON; 1 };
        skip 'JSON not available', 1 if $error;
        diag('filter tests for JSON ' . $JSON::VERSION);

        my $ddp = Data::Printer::Object->new(
            colored       => 0,
            multiline     => 0,
            show_readonly => 0,
            filters       => ['Web'],
        );

        my $data;
        my $obj = JSON->new;
        if ($obj->can('decode')) {
            $data = $obj->decode($json);
        }
        elsif ($obj->can('jsonToObj')) {
            $data = $obj->jsonToObj($json);
        }
        else {
            skip 'not sure how to load JSON object', 1;
        }
        is( $ddp->parse($data), $expected, 'parsed whatever powered JSON' );
    };
}

sub test_json_any {
    my ($json, $expected) = @_;
    SKIP: {
        my $error = !eval { require JSON::Any; JSON::Any->import(); 1 };
        skip 'JSON::Any not available', 1 if $error;
        {no strict 'refs';
        diag('filter tests for JSON::Any ' . $JSON::Any::VERSION . ' with ' . JSON::Any->handlerType . ' ' . ${ JSON::Any->handlerType . '::VERSION' });
        }

        my $ddp = Data::Printer::Object->new(
            colored       => 0,
            multiline     => 0,
            show_readonly => 0,
            filters       => ['Web'],
        );

        my $data = JSON::Any->new->decode($json);
        is( $ddp->parse($data), $expected, 'parsed whatever powered JSON::Any' );
    };
}

sub test_json_maybexs {
    my ($json, $expected) = @_;
    SKIP: {
        my $error = !eval { require JSON::MaybeXS; 1 };
        skip 'JSON::MaybeXS not available', 1 if $error;

        {no strict 'refs'; diag('filter tests for JSON::MaybeXS ' . $JSON::MaybeXS::VERSION . ' with ' . JSON::MaybeXS::JSON() . ' ' . ${ JSON::MaybeXS::JSON() . '::VERSION' });
        }

        my $ddp = Data::Printer::Object->new(
            colored       => 0,
            multiline     => 0,
            show_readonly => 0,
            filters       => ['Web'],
        );

        my $data = JSON::MaybeXS::decode_json($json);
        is( $ddp->parse($data), $expected, 'parsed whatever powered JSON::MaybeXS' );
    };
}

sub test_json_dwiw {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require JSON::DWIW; 1 };
        skip 'JSON::DWIW not available', 1 if $error;
        diag('filter for JSON::DWIW ' . $JSON::DWIW::VERSION);

        my $data = JSON::DWIW::from_json($json, { convert_bool => 1 });
        is( $ddp->parse($data), $expected, 'JSON::DWIW live booleans' );
    };
    my $emulated = {
        alpha   => bless( do { \( my $v = 1 ) }, 'JSON::DWIW::Boolean' ),
        bravo   => bless( do { \( my $v = 0 ) }, 'JSON::DWIW::Boolean' ),
        charlie => bless( do { \( my $v = 1 ) }, 'JSON::DWIW::Boolean' ),
        delta   => bless( do { \( my $v = 0 ) }, 'JSON::DWIW::Boolean' ),
    };
    is($ddp->parse($emulated), $expected, 'JSON::DWIW, emulated');
}

sub test_json_parser {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require JSON::Parser; 1 };
        skip 'JSON::Parser not available', 1 if $error;
        diag('filter for JSON::Parser ' . $JSON::Parser::VERSION);

        my $data = JSON::Parser->new->jsonToObj($json);
        is( $ddp->parse($data), $expected, 'JSON::Parser live booleans' );
    };
    my $emulated = {
        alpha   => bless({value => 'true' }, 'JSON::NotString' ),
        bravo   => bless({value => 'false'}, 'JSON::NotString' ),
        charlie => bless({value => 'true' }, 'JSON::NotString' ),
        delta   => bless({value => 'false'}, 'JSON::NotString' ),
    };
    is($ddp->parse($emulated), $expected, 'JSON::Parser, emulated');
}

sub test_json_sl {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require JSON::SL; 1 };
        skip 'JSON::SL not available', 1 if $error;

        diag('filter for JSON::SL ' . $JSON::SL::VERSION);

        my $data = JSON::SL::decode_json($json);
        is( $ddp->parse($data), $expected, 'JSON::SL live booleans' );
    };
    my $emulated = {
        alpha   => bless(do { \(my $o = 1) }, 'JSON::SL::Boolean' ),
        bravo   => bless(do { \(my $o = 0) }, 'JSON::SL::Boolean' ),
        charlie => bless(do { \(my $o = 1) }, 'JSON::SL::Boolean' ),
        delta   => bless(do { \(my $o = 0) }, 'JSON::SL::Boolean' ),
    };
    is($ddp->parse($emulated), $expected, 'JSON::SL, emulated');
}

sub test_json_mojo {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        show_readonly => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require Mojo::JSON; require Mojolicious; 1 };
        skip 'Mojo::JSON not available', 1 if $error;

        diag('filter for Mojo::JSON ' . $Mojolicious::VERSION);

        my $data = Mojo::JSON->can('new')
                 ? Mojo::JSON->new->decode($json)
                 : Mojo::JSON::decode_json($json)
                 ;
        is( $ddp->parse($data), $expected, 'Mojo::JSON live booleans' );
    };
}

sub test_json_pegex {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require Pegex::JSON; 1 };
        skip 'Pegex::JSON not available', 1 if $error;
        diag('filter for Pegex::JSON ' . $Pegex::JSON::VERSION);

        my $data = Pegex::JSON->can('parse')
                 ? Pegex::JSON->parse($json)
                 : Pegex::JSON->new->load($json)
                 ;
        is( $ddp->parse($data), $expected, 'Pegex::JSON live booleans' );
    };
}

sub test_json_cpanel {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        show_readonly => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require Cpanel::JSON::XS; 1 };
        skip 'Cpanel::JSON::XS not available', 1 if $error;
        diag('filter for Cpanel::JSON::XS ' . $Cpanel::JSON::XS::VERSION);

        my $data = Cpanel::JSON::XS::decode_json($json);
        is( $ddp->parse($data), $expected, 'Cpanel::JSON::XS live booleans' );
    };
}

sub test_json_tiny {
    my ($json, $expected) = @_;

    my $ddp = Data::Printer::Object->new(
        colored       => 0,
        multiline     => 0,
        filters       => ['Web'],
    );

    SKIP: {
        my $error = !eval { require JSON::Tiny; 1 };
        skip 'JSON::Tiny not available', 1 if $error;
        diag('filter for JSON::Tiny ' . $JSON::Tiny::VERSION);

        my $data = JSON::Tiny::decode_json($json);
        is( $ddp->parse($data), $expected, 'JSON::Tiny live booleans' );
    };
}