|
2 | 2 | # frozen_string_literal: true
|
3 | 3 |
|
4 | 4 | require 'mechanize/test_case'
|
5 |
| -require "brotli" unless RUBY_PLATFORM == "java" |
| 5 | +unless RUBY_PLATFORM == 'java' |
| 6 | + require 'brotli' |
| 7 | + require 'zstd-ruby' |
| 8 | +end |
6 | 9 |
|
7 | 10 | class TestMechanizeHttpAgent < Mechanize::TestCase
|
8 | 11 |
|
@@ -965,6 +968,46 @@ def test_response_content_encoding_brotli_corrupt
|
965 | 968 | assert(body_io.closed?)
|
966 | 969 | end
|
967 | 970 |
|
| 971 | + def test_response_content_encoding_zstd_when_zstd_not_loaded |
| 972 | + skip("only test this on jruby which doesn't have zstd support") unless RUBY_ENGINE == 'jruby' |
| 973 | + |
| 974 | + @res.instance_variable_set :@header, 'content-encoding' => %w[zstd] |
| 975 | + body_io = StringIO.new("content doesn't matter for this test") |
| 976 | + |
| 977 | + e = assert_raises(Mechanize::Error) do |
| 978 | + @agent.response_content_encoding(@res, body_io) |
| 979 | + end |
| 980 | + assert_includes(e.message, 'cannot deflate zstd-encoded response') |
| 981 | + |
| 982 | + assert(body_io.closed?) |
| 983 | + end |
| 984 | + |
| 985 | + def test_response_content_encoding_zstd |
| 986 | + skip('jruby does not have zstd support') if RUBY_ENGINE == 'jruby' |
| 987 | + |
| 988 | + @res.instance_variable_set :@header, 'content-encoding' => %w[zstd] |
| 989 | + body_io = StringIO.new(Zstd.compress('this is compressed by zstd')) |
| 990 | + |
| 991 | + body = @agent.response_content_encoding(@res, body_io) |
| 992 | + |
| 993 | + assert_equal('this is compressed by zstd', body.read) |
| 994 | + assert(body_io.closed?) |
| 995 | + end |
| 996 | + |
| 997 | + def test_response_content_encoding_zstd_corrupt |
| 998 | + skip('jruby does not have zstd support') if RUBY_ENGINE == 'jruby' |
| 999 | + |
| 1000 | + @res.instance_variable_set :@header, 'content-encoding' => %w[zstd] |
| 1001 | + body_io = StringIO.new('not a zstd payload') |
| 1002 | + |
| 1003 | + e = assert_raises(Mechanize::Error) do |
| 1004 | + @agent.response_content_encoding(@res, body_io) |
| 1005 | + end |
| 1006 | + assert_includes(e.message, 'error decompressing zstd-encoded response') |
| 1007 | + assert_kind_of(RuntimeError, e.cause) |
| 1008 | + assert(body_io.closed?) |
| 1009 | + end |
| 1010 | + |
968 | 1011 | def test_response_content_encoding_gzip_corrupt
|
969 | 1012 | log = StringIO.new
|
970 | 1013 | logger = Logger.new log
|
|
0 commit comments