Skip to content

Commit 747e792

Browse files
authored
Remove unnecessary "".respond_to?(:encoding) checks (#1043)
* Remove unnecessary "".respond_to?(:encoding) checks * Pin excon < 1.0.0 to fix webmock errors * Simplify forwarding arguments to GzipWriter
1 parent 9850669 commit 747e792

File tree

5 files changed

+101
-137
lines changed

5 files changed

+101
-137
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ gem "codeclimate-test-reporter", "~> 0.4"
1111
gem "cucumber", "~> 9.0"
1212
gem "curb", "~> 1.0.1"
1313
gem "em-http-request"
14-
gem "excon", ">= 0.62.0"
14+
gem "excon", ">= 0.62.0", "< 1.0.0"
1515

1616
if ENV['FARADAY_VERSION'] == "1.0"
1717
gem "faraday", "~> 1.0"

lib/vcr/structs.rb

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,29 @@ def body_from(hash_or_string)
2525
end
2626
end
2727

28-
if "".respond_to?(:encoding)
29-
def force_encode_string(string, encoding)
30-
return string unless encoding
31-
string.force_encoding(encoding)
32-
end
33-
34-
def try_encode_string(string, encoding_name)
35-
return string if encoding_name.nil?
28+
def force_encode_string(string, encoding)
29+
return string unless encoding
30+
string.force_encoding(encoding)
31+
end
3632

37-
encoding = Encoding.find(encoding_name)
38-
return string if string.encoding == encoding
33+
def try_encode_string(string, encoding_name)
34+
return string if encoding_name.nil?
3935

40-
# ASCII-8BIT just means binary, so encoding to it is nonsensical
41-
# and yet "\u00f6".encode("ASCII-8BIT") raises an error.
42-
# Instead, we'll force encode it (essentially just tagging it as binary)
43-
return string.force_encoding(encoding) if encoding == Encoding::BINARY
36+
encoding = Encoding.find(encoding_name)
37+
return string if string.encoding == encoding
4438

45-
string.encode(encoding)
46-
rescue EncodingError => e
47-
struct_type = name.split('::').last.downcase
48-
warn "VCR: got `#{e.class.name}: #{e.message}` while trying to encode the #{string.encoding.name} " +
49-
"#{struct_type} body to the original body encoding (#{encoding}). Consider using the " +
50-
"`:preserve_exact_body_bytes` option to work around this."
51-
return string
52-
end
53-
else
54-
def force_encode_string(string, encoding)
55-
string
56-
end
39+
# ASCII-8BIT just means binary, so encoding to it is nonsensical
40+
# and yet "\u00f6".encode("ASCII-8BIT") raises an error.
41+
# Instead, we'll force encode it (essentially just tagging it as binary)
42+
return string.force_encoding(encoding) if encoding == Encoding::BINARY
5743

58-
def try_encode_string(string, encoding)
59-
string
60-
end
44+
string.encode(encoding)
45+
rescue EncodingError => e
46+
struct_type = name.split('::').last.downcase
47+
warn "VCR: got `#{e.class.name}: #{e.message}` while trying to encode the #{string.encoding.name} " +
48+
"#{struct_type} body to the original body encoding (#{encoding}). Consider using the " +
49+
"`:preserve_exact_body_bytes` option to work around this."
50+
return string
6151
end
6252
end
6353

@@ -91,14 +81,8 @@ def serializable_body
9181
end
9282
end
9383

94-
if ''.respond_to?(:encoding)
95-
def base_body_hash(body)
96-
{ 'encoding' => body.encoding.name }
97-
end
98-
else
99-
def base_body_hash(body)
100-
{ }
101-
end
84+
def base_body_hash(body)
85+
{ 'encoding' => body.encoding.name }
10286
end
10387
end
10488

@@ -416,9 +400,7 @@ def recompress
416400
case type
417401
when 'gzip'
418402
body_str = ''
419-
args = [StringIO.new(body_str)]
420-
args << { :encoding => 'ASCII-8BIT' } if ''.respond_to?(:encoding)
421-
writer = Zlib::GzipWriter.new(*args)
403+
writer = Zlib::GzipWriter.new(StringIO.new(body_str), encoding: 'ASCII-8BIT')
422404
writer.write(body)
423405
writer.close
424406
body_str
@@ -457,10 +439,7 @@ def self.decompress(body, type)
457439

458440
case type
459441
when 'gzip'
460-
gzip_reader_options = {}
461-
gzip_reader_options[:encoding] = 'ASCII-8BIT' if ''.respond_to?(:encoding)
462-
yield Zlib::GzipReader.new(StringIO.new(body),
463-
**gzip_reader_options).read
442+
yield Zlib::GzipReader.new(StringIO.new(body), encoding: 'ASCII-8BIT').read
464443
when 'deflate'
465444
yield Zlib::Inflate.inflate(body)
466445
when 'identity', NilClass

spec/lib/vcr/cassette/migrator_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@
101101
FileUtils.mkdir_p dir
102102
end
103103

104-
before(:each) do
105-
# the encoding won't be set on rubies that don't support it
106-
updated_contents.gsub!(/^\s+encoding:.*$/, '')
107-
end unless ''.respond_to?(:encoding)
108-
109104
# JRuby serializes YAML with some slightly different whitespace.
110105
before(:each) do
111106
[original_contents, updated_contents].each do |contents|
@@ -191,4 +186,3 @@ def load_file(file_name)
191186
it_behaves_like "ignoring invalid YAML"
192187
end if defined?(YAML::ENGINE)
193188
end
194-

spec/lib/vcr/cassette/serializers_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Cassette
6767
it_behaves_like "encoding error handling", :yaml, ArgumentError do
6868
let(:string) { "\xFA".force_encoding("UTF-8") }
6969
before { ::YAML::ENGINE.yamler = 'psych' if defined?(::YAML::ENGINE) }
70-
end if ''.respond_to?(:encoding)
70+
end
7171

7272
it_behaves_like "syntax error handling", :yaml, ::Psych::SyntaxError do
7373
let(:serialized) { <<~YAML }
@@ -83,7 +83,7 @@ class Cassette
8383
it_behaves_like "a serializer", :syck, "yml", :lazily_loaded do
8484
it_behaves_like "encoding error handling", :syck, ArgumentError do
8585
let(:string) { "\xFA".force_encoding("UTF-8") }
86-
end if ''.respond_to?(:encoding)
86+
end
8787

8888
it_behaves_like "syntax error handling", :syck, ::Psych::SyntaxError do
8989
let(:serialized) { <<~YAML }
@@ -98,7 +98,7 @@ class Cassette
9898
it_behaves_like "a serializer", :psych, "yml", :lazily_loaded do
9999
it_behaves_like "encoding error handling", :psych, ArgumentError do
100100
let(:string) { "\xFA".force_encoding("UTF-8") }
101-
end if ''.respond_to?(:encoding)
101+
end
102102

103103
it_behaves_like "syntax error handling", :psych, ::Psych::SyntaxError do
104104
let(:serialized) { <<~YAML }
@@ -113,7 +113,7 @@ class Cassette
113113
it_behaves_like "a serializer", :compressed, "zz", :lazily_loaded do
114114
it_behaves_like "encoding error handling", :compressed, ArgumentError do
115115
let(:string) { "\xFA".force_encoding("UTF-8") }
116-
end if ''.respond_to?(:encoding)
116+
end
117117

118118
it_behaves_like "syntax error handling", :compressed, ::Psych::SyntaxError do
119119
let(:serialized) { Zlib::Deflate.deflate(<<~YAML) }

spec/lib/vcr/structs_spec.rb

Lines changed: 73 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,8 @@ module VCR
6565
include_context "configuration stubbing"
6666
before { allow(config).to receive(:uri_parser) { LimitedURI } }
6767

68-
if ''.respond_to?(:encoding)
69-
def body_hash(key, value)
70-
{ key => value, 'encoding' => 'UTF-8' }
71-
end
72-
else
73-
def body_hash(key, value)
74-
{ key => value }
75-
end
68+
def body_hash(key, value)
69+
{ key => value, 'encoding' => 'UTF-8' }
7670
end
7771

7872
describe "#recorded_at" do
@@ -164,95 +158,93 @@ def body_hash(key, value)
164158
expect(i.response.body).to eq('res body')
165159
end
166160

167-
if ''.respond_to?(:encoding)
168-
it 'force encodes the decoded base64 string as the original encoding' do
169-
string = "café"
170-
string.force_encoding("US-ASCII")
171-
expect(string).not_to be_valid_encoding
161+
it 'force encodes the decoded base64 string as the original encoding' do
162+
string = "café"
163+
string.force_encoding("US-ASCII")
164+
expect(string).not_to be_valid_encoding
172165

173-
hash['request']['body'] = { 'base64_string' => Base64.encode64(string.dup), 'encoding' => 'US-ASCII' }
174-
hash['response']['body'] = { 'base64_string' => Base64.encode64(string.dup), 'encoding' => 'US-ASCII' }
166+
hash['request']['body'] = { 'base64_string' => Base64.encode64(string.dup), 'encoding' => 'US-ASCII' }
167+
hash['response']['body'] = { 'base64_string' => Base64.encode64(string.dup), 'encoding' => 'US-ASCII' }
175168

176-
i = HTTPInteraction.from_hash(hash)
177-
expect(i.request.body.encoding.name).to eq("US-ASCII")
178-
expect(i.response.body.encoding.name).to eq("US-ASCII")
179-
expect(i.request.body.bytes.to_a).to eq(string.bytes.to_a)
180-
expect(i.response.body.bytes.to_a).to eq(string.bytes.to_a)
181-
expect(i.request.body).not_to be_valid_encoding
182-
expect(i.response.body).not_to be_valid_encoding
183-
end
169+
i = HTTPInteraction.from_hash(hash)
170+
expect(i.request.body.encoding.name).to eq("US-ASCII")
171+
expect(i.response.body.encoding.name).to eq("US-ASCII")
172+
expect(i.request.body.bytes.to_a).to eq(string.bytes.to_a)
173+
expect(i.response.body.bytes.to_a).to eq(string.bytes.to_a)
174+
expect(i.request.body).not_to be_valid_encoding
175+
expect(i.response.body).not_to be_valid_encoding
176+
end
184177

185-
it 'does not attempt to force encode the decoded base64 string when there is no encoding given (i.e. if the cassette was recorded on ruby 1.8)' do
186-
hash['request']['body'] = { 'base64_string' => Base64.encode64('foo') }
178+
it 'does not attempt to force encode the decoded base64 string when there is no encoding given (i.e. if the cassette was recorded on ruby 1.8)' do
179+
hash['request']['body'] = { 'base64_string' => Base64.encode64('foo') }
187180

188-
i = HTTPInteraction.from_hash(hash)
189-
expect(i.request.body).to eq('foo')
190-
expect(i.request.body.encoding).to eq(Encoding::BINARY)
191-
end
181+
i = HTTPInteraction.from_hash(hash)
182+
expect(i.request.body).to eq('foo')
183+
expect(i.request.body.encoding).to eq(Encoding::BINARY)
184+
end
192185

193-
it 'tries to encode strings to the original encoding' do
194-
hash['request']['body'] = { 'string' => "abc", 'encoding' => 'ISO-8859-1' }
195-
hash['response']['body'] = { 'string' => "abc", 'encoding' => 'ISO-8859-1' }
186+
it 'tries to encode strings to the original encoding' do
187+
hash['request']['body'] = { 'string' => "abc", 'encoding' => 'ISO-8859-1' }
188+
hash['response']['body'] = { 'string' => "abc", 'encoding' => 'ISO-8859-1' }
196189

197-
i = HTTPInteraction.from_hash(hash)
198-
expect(i.request.body).to eq("abc")
199-
expect(i.response.body).to eq("abc")
200-
expect(i.request.body.encoding.name).to eq("ISO-8859-1")
201-
expect(i.response.body.encoding.name).to eq("ISO-8859-1")
202-
end
190+
i = HTTPInteraction.from_hash(hash)
191+
expect(i.request.body).to eq("abc")
192+
expect(i.response.body).to eq("abc")
193+
expect(i.request.body.encoding.name).to eq("ISO-8859-1")
194+
expect(i.response.body.encoding.name).to eq("ISO-8859-1")
195+
end
203196

204-
it 'does not attempt to encode the string when there is no encoding given (i.e. if the cassette was recorded on ruby 1.8)' do
205-
string = 'foo'
206-
string.force_encoding("ISO-8859-1")
207-
hash['request']['body'] = { 'string' => string }
197+
it 'does not attempt to encode the string when there is no encoding given (i.e. if the cassette was recorded on ruby 1.8)' do
198+
string = 'foo'
199+
string.force_encoding("ISO-8859-1")
200+
hash['request']['body'] = { 'string' => string }
208201

209-
i = HTTPInteraction.from_hash(hash)
210-
expect(i.request.body).to eq('foo')
211-
expect(i.request.body.encoding.name).to eq("ISO-8859-1")
212-
end
202+
i = HTTPInteraction.from_hash(hash)
203+
expect(i.request.body).to eq('foo')
204+
expect(i.request.body.encoding.name).to eq("ISO-8859-1")
205+
end
213206

214-
it 'force encodes to ASCII-8BIT (since it just means "no encoding" or binary)' do
215-
string = "\u00f6"
216-
string.encode("UTF-8")
217-
expect(string).to be_valid_encoding
218-
hash['request']['body'] = { 'string' => string, 'encoding' => 'ASCII-8BIT' }
207+
it 'force encodes to ASCII-8BIT (since it just means "no encoding" or binary)' do
208+
string = "\u00f6"
209+
string.encode("UTF-8")
210+
expect(string).to be_valid_encoding
211+
hash['request']['body'] = { 'string' => string, 'encoding' => 'ASCII-8BIT' }
219212

220-
expect(Request).not_to receive(:warn)
221-
i = HTTPInteraction.from_hash(hash)
222-
expect(i.request.body).to eq(string)
223-
expect(i.request.body.bytes.to_a).to eq(string.bytes.to_a)
224-
expect(i.request.body.encoding).to eq(Encoding::BINARY)
225-
end
213+
expect(Request).not_to receive(:warn)
214+
i = HTTPInteraction.from_hash(hash)
215+
expect(i.request.body).to eq(string)
216+
expect(i.request.body.bytes.to_a).to eq(string.bytes.to_a)
217+
expect(i.request.body.encoding).to eq(Encoding::BINARY)
218+
end
226219

227-
context 'when the string cannot be encoded as the original encoding' do
228-
def verify_encoding_error
229-
expect { "\xFAbc".encode("ISO-8859-1") }.to raise_error(EncodingError)
230-
end
220+
context 'when the string cannot be encoded as the original encoding' do
221+
def verify_encoding_error
222+
expect { "\xFAbc".encode("ISO-8859-1") }.to raise_error(EncodingError)
223+
end
231224

232-
before do
233-
allow(Request).to receive(:warn)
234-
allow(Response).to receive(:warn)
225+
before do
226+
allow(Request).to receive(:warn)
227+
allow(Response).to receive(:warn)
235228

236-
hash['request']['body'] = { 'string' => "\xFAbc", 'encoding' => 'ISO-8859-1' }
237-
hash['response']['body'] = { 'string' => "\xFAbc", 'encoding' => 'ISO-8859-1' }
229+
hash['request']['body'] = { 'string' => "\xFAbc", 'encoding' => 'ISO-8859-1' }
230+
hash['response']['body'] = { 'string' => "\xFAbc", 'encoding' => 'ISO-8859-1' }
238231

239-
verify_encoding_error
240-
end
232+
verify_encoding_error
233+
end
241234

242-
it 'does not force the encoding' do
243-
i = HTTPInteraction.from_hash(hash)
244-
expect(i.request.body).to eq("\xFAbc")
245-
expect(i.response.body).to eq("\xFAbc")
246-
expect(i.request.body.encoding.name).not_to eq("ISO-8859-1")
247-
expect(i.response.body.encoding.name).not_to eq("ISO-8859-1")
248-
end
235+
it 'does not force the encoding' do
236+
i = HTTPInteraction.from_hash(hash)
237+
expect(i.request.body).to eq("\xFAbc")
238+
expect(i.response.body).to eq("\xFAbc")
239+
expect(i.request.body.encoding.name).not_to eq("ISO-8859-1")
240+
expect(i.response.body.encoding.name).not_to eq("ISO-8859-1")
241+
end
249242

250-
it 'prints a warning and informs users of the :preserve_exact_body_bytes option' do
251-
expect(Request).to receive(:warn).with(/ISO-8859-1.*preserve_exact_body_bytes/)
252-
expect(Response).to receive(:warn).with(/ISO-8859-1.*preserve_exact_body_bytes/)
243+
it 'prints a warning and informs users of the :preserve_exact_body_bytes option' do
244+
expect(Request).to receive(:warn).with(/ISO-8859-1.*preserve_exact_body_bytes/)
245+
expect(Response).to receive(:warn).with(/ISO-8859-1.*preserve_exact_body_bytes/)
253246

254-
HTTPInteraction.from_hash(hash)
255-
end
247+
HTTPInteraction.from_hash(hash)
256248
end
257249
end
258250
end
@@ -316,7 +308,7 @@ def verify_encoding_error
316308
expect(hash['response']['body']).to eq(body_hash('base64_string', Base64.encode64('res body')))
317309
end
318310

319-
it "sets the string's original encoding", :if => ''.respond_to?(:encoding) do
311+
it "sets the string's original encoding" do
320312
interaction.request.body.force_encoding('ISO-8859-10')
321313
interaction.response.body.force_encoding(Encoding::BINARY)
322314

@@ -744,4 +736,3 @@ def instance(body)
744736
end
745737
end
746738
end
747-

0 commit comments

Comments
 (0)