Options#== improvement: bailout early when there's mismatch of ivars

This commit is contained in:
HoneyryderChuck 2023-11-12 23:12:09 +00:00
parent 5be39fe60e
commit 1c7881eda3
2 changed files with 19 additions and 12 deletions

View File

@ -226,20 +226,19 @@ module HTTPX
OUT
end
REQUEST_IVARS = %i[@params @form @xml @json @body].freeze
private_constant :REQUEST_IVARS
REQUEST_BODY_IVARS = %i[@headers @params @form @xml @json @body].freeze
private_constant :REQUEST_BODY_IVARS
def ==(other)
ivars = instance_variables | other.instance_variables
ivars.all? do |ivar|
case ivar
when :@headers
# currently, this is used to pick up an available matching connection.
# the headers do not play a role, as they are relevant only for the request.
true
when *REQUEST_IVARS
true
else
super || begin
# headers and other request options do not play a role, as they are
# relevant only for the request.
ivars = instance_variables - REQUEST_BODY_IVARS
other_ivars = other.instance_variables - REQUEST_BODY_IVARS
return false if ivars != other_ivars
ivars.all? do |ivar|
instance_variable_get(ivar) == other.instance_variable_get(ivar)
end
end
@ -273,6 +272,7 @@ module HTTPX
end
def initialize_dup(other)
super
instance_variables.each do |ivar|
instance_variable_set(ivar, other.instance_variable_get(ivar).dup)
end

View File

@ -150,4 +150,11 @@ class OptionsTest < Minitest::Test
opts = Options.new
assert opts.to_hash.is_a?(Hash)
end
def test_options_equals
opts = Options.new(origin: "http://example.com")
assert opts == Options.new(origin: "http://example.com")
assert Options.new(origin: "http://example.com", headers: { "foo" => "bar" }) == Options.new(origin: "http://example.com")
assert Options.new(json: { "foo" => "bar" }) == Options.new
end
end