mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-10 00:02:10 -04:00
Options#== improvement: bailout early when there's mismatch of ivars
This commit is contained in:
parent
5be39fe60e
commit
1c7881eda3
@ -226,20 +226,19 @@ module HTTPX
|
|||||||
OUT
|
OUT
|
||||||
end
|
end
|
||||||
|
|
||||||
REQUEST_IVARS = %i[@params @form @xml @json @body].freeze
|
REQUEST_BODY_IVARS = %i[@headers @params @form @xml @json @body].freeze
|
||||||
private_constant :REQUEST_IVARS
|
private_constant :REQUEST_BODY_IVARS
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
ivars = instance_variables | other.instance_variables
|
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|
|
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
|
|
||||||
instance_variable_get(ivar) == other.instance_variable_get(ivar)
|
instance_variable_get(ivar) == other.instance_variable_get(ivar)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -273,6 +272,7 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize_dup(other)
|
def initialize_dup(other)
|
||||||
|
super
|
||||||
instance_variables.each do |ivar|
|
instance_variables.each do |ivar|
|
||||||
instance_variable_set(ivar, other.instance_variable_get(ivar).dup)
|
instance_variable_set(ivar, other.instance_variable_get(ivar).dup)
|
||||||
end
|
end
|
||||||
|
@ -150,4 +150,11 @@ class OptionsTest < Minitest::Test
|
|||||||
opts = Options.new
|
opts = Options.new
|
||||||
assert opts.to_hash.is_a?(Hash)
|
assert opts.to_hash.is_a?(Hash)
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user