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
|
require 'spec_helper'
RSpec.describe HTTParty::Decompressor do
describe '.SupportedEncodings' do
it 'returns a hash' do
expect(HTTParty::Decompressor::SupportedEncodings).to be_instance_of(Hash)
end
end
describe '#decompress' do
let(:body) { 'body' }
let(:encoding) { 'none' }
let(:decompressor) { described_class.new(body, encoding) }
subject { decompressor.decompress }
shared_examples 'returns nil' do
it { expect(subject).to be_nil }
end
shared_examples 'returns the body' do
it { expect(subject).to eq 'body' }
end
context 'when body is nil' do
let(:body) { nil }
it_behaves_like 'returns nil'
end
context 'when body is blank' do
let(:body) { ' ' }
it { expect(subject).to eq ' ' }
end
context 'when encoding is nil' do
let(:encoding) { nil }
it_behaves_like 'returns the body'
end
context 'when encoding is blank' do
let(:encoding) { ' ' }
it_behaves_like 'returns the body'
end
context 'when encoding is "none"' do
let(:encoding) { 'none' }
it_behaves_like 'returns the body'
end
context 'when encoding is "identity"' do
let(:encoding) { 'identity' }
it_behaves_like 'returns the body'
end
context 'when encoding is unsupported' do
let(:encoding) { 'invalid' }
it_behaves_like 'returns nil'
end
context 'when encoding is "br"' do
let(:encoding) { 'br' }
context 'when brotli gem not included' do
it_behaves_like 'returns nil'
end
context 'when brotli included' do
before do
dbl = double('Brotli')
expect(dbl).to receive(:inflate).with('body').and_return('foobar')
stub_const('Brotli', dbl)
end
it { expect(subject).to eq 'foobar' }
end
context 'when brotli raises error' do
before do
dbl = double('brotli')
expect(dbl).to receive(:inflate).with('body') { raise RuntimeError.new('brotli error') }
stub_const('Brotli', dbl)
end
it { expect(subject).to eq nil }
end
end
context 'when encoding is "compress"' do
let(:encoding) { 'compress' }
context 'when LZW gem not included' do
it_behaves_like 'returns nil'
end
context 'when ruby-lzws included' do
before do
dbl = double('lzws')
expect(dbl).to receive(:decompress).with('body').and_return('foobar')
stub_const('LZWS::String', dbl)
end
it { expect(subject).to eq 'foobar' }
end
context 'when ruby-lzws raises error' do
before do
dbl = double('lzws')
expect(dbl).to receive(:decompress).with('body') { raise RuntimeError.new('brotli error') }
stub_const('LZWS::String', dbl)
end
it { expect(subject).to eq nil }
end
context 'when compress-lzw included' do
before do
dbl2 = double('lzw2')
dbl = double('lzw1', new: dbl2)
expect(dbl2).to receive(:decompress).with('body').and_return('foobar')
stub_const('LZW::Simple', dbl)
end
it { expect(subject).to eq 'foobar' }
end
context 'when compress-lzw raises error' do
before do
dbl2 = double('lzw2')
dbl = double('lzw1', new: dbl2)
expect(dbl2).to receive(:decompress).with('body') { raise RuntimeError.new('brotli error') }
stub_const('LZW::Simple', dbl)
end
end
end
context 'when encoding is "zstd"' do
let(:encoding) { 'zstd' }
context 'when zstd-ruby gem not included' do
it_behaves_like 'returns nil'
end
context 'when zstd-ruby included' do
before do
dbl = double('Zstd')
expect(dbl).to receive(:decompress).with('body').and_return('foobar')
stub_const('Zstd', dbl)
end
it { expect(subject).to eq 'foobar' }
end
context 'when zstd raises error' do
before do
dbl = double('Zstd')
expect(dbl).to receive(:decompress).with('body') { raise RuntimeError.new('zstd error') }
stub_const('Zstd', dbl)
end
it { expect(subject).to eq nil }
end
end
end
end
|