From 1c7881eda34550e98331ce6be5f7bcd7625a7410 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Sun, 12 Nov 2023 23:12:09 +0000 Subject: [PATCH] Options#== improvement: bailout early when there's mismatch of ivars --- lib/httpx/options.rb | 24 ++++++++++++------------ test/options_test.rb | 7 +++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/httpx/options.rb b/lib/httpx/options.rb index 3a9f6e18..4d736907 100644 --- a/lib/httpx/options.rb +++ b/lib/httpx/options.rb @@ -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 diff --git a/test/options_test.rb b/test/options_test.rb index d0309a12..39dc89fd 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -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