|
| 1 | +#! perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use English qw( -no_match_vars ); |
| 7 | + |
| 8 | +use Test::More; |
| 9 | +use Test::Needs 'IO::Compress::Zstd'; |
| 10 | +use Path::Tiny qw( path ); |
| 11 | + |
| 12 | +require HTTP::Message; |
| 13 | + |
| 14 | +my $files = path($PROGRAM_NAME)->parent->child('files'); |
| 15 | +my $lorem_ipsum_clear = $files->child('lorem_ipsum.txt')->slurp_utf8; |
| 16 | +my $lorem_ipsum_zstd = $files->child('lorem_ipsum.txt.zst')->slurp_raw; |
| 17 | +# my $lorem_ipsum_zstd = $files->child('lorem_ipsum.txt.zst.from_perl')->slurp_raw; |
| 18 | +my $lorem_ipsum_zstd_b64 = $files->child('lorem_ipsum.txt.zst.b64')->slurp_raw; |
| 19 | + |
| 20 | +subtest "no decoding" => sub { |
| 21 | + |
| 22 | + my $m = HTTP::Message->new( |
| 23 | + [ |
| 24 | + "Content-Type" => "text/plain", |
| 25 | + "Content-Encoding" => "", |
| 26 | + ], |
| 27 | + $lorem_ipsum_clear |
| 28 | + ); |
| 29 | + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works, is same as content" ); |
| 30 | + ok( $m->decode, "decode() works" ); |
| 31 | + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); |
| 32 | +}; |
| 33 | + |
| 34 | +subtest "decoding zstd" => sub { |
| 35 | + |
| 36 | + my $m = HTTP::Message->new( |
| 37 | + [ |
| 38 | + "Content-Type" => "text/plain", |
| 39 | + "Content-Encoding" => "zstd", |
| 40 | + ], |
| 41 | + $lorem_ipsum_zstd |
| 42 | + ); |
| 43 | + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); |
| 44 | + ok( $m->decode, "decode() works" ); |
| 45 | + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); |
| 46 | +}; |
| 47 | + |
| 48 | +subtest "decoding zstd in base64" => sub { |
| 49 | + |
| 50 | + my $m = HTTP::Message->new( |
| 51 | + [ |
| 52 | + "Content-Type" => "text/plain", |
| 53 | + "Content-Encoding" => "zstd, base64", |
| 54 | + ], |
| 55 | + $lorem_ipsum_zstd_b64 |
| 56 | + ); |
| 57 | + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); |
| 58 | + ok( $m->decode, "decode() works" ); |
| 59 | + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); |
| 60 | +}; |
| 61 | + |
| 62 | +subtest "encoding to zstd" => sub { |
| 63 | + my $m = HTTP::Message->new( |
| 64 | + [ |
| 65 | + "Content-Type" => "text/plain", |
| 66 | + ], |
| 67 | + $lorem_ipsum_clear |
| 68 | + ); |
| 69 | + is( $m->content, $lorem_ipsum_clear, "the content is the original" ); |
| 70 | + ok( $m->encode("zstd"), "set encoding to 'zstd'" ); |
| 71 | + is( $m->header("Content-Encoding"), |
| 72 | + "zstd", "... and Content-Encoding is set" ); |
| 73 | + isnt( $m->content, $lorem_ipsum_clear, "... and the content has changed" ); |
| 74 | + is( $m->content, $lorem_ipsum_zstd, "... and the content is correct" ); |
| 75 | + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); |
| 76 | + ok( $m->decode, "decode() works" ); |
| 77 | + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); |
| 78 | +}; |
| 79 | + |
| 80 | +subtest "encoding to zstd in base64" => sub { |
| 81 | + my $m = HTTP::Message->new( |
| 82 | + [ |
| 83 | + "Content-Type" => "text/plain", |
| 84 | + ], |
| 85 | + $lorem_ipsum_clear |
| 86 | + ); |
| 87 | + is( $m->content, $lorem_ipsum_clear, "the content is the original" ); |
| 88 | + ok( $m->encode("zstd", "base64"), "set encoding to 'zstd' in 'base64'" ); |
| 89 | + is( $m->header("Content-Encoding"), |
| 90 | + "zstd, base64", "... and Content-Encoding is set" ); |
| 91 | + isnt( $m->content, $lorem_ipsum_clear, "... and the content has changed" ); |
| 92 | + is( $m->content, $lorem_ipsum_zstd_b64, "... and the content is correct" ); |
| 93 | + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); |
| 94 | + ok( $m->decode, "decode() works" ); |
| 95 | + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); |
| 96 | +}; |
| 97 | + |
| 98 | +done_testing; |
0 commit comments