Merge branch 'issue-355' into 'master'

plugin init fixes: enforce usage of Options or hash for the second argument;...

Closes #355

See merge request os85/httpx!403
This commit is contained in:
HoneyryderChuck 2025-09-01 09:18:14 +00:00
commit 92e891919f
3 changed files with 18 additions and 3 deletions

View File

@ -271,11 +271,12 @@ module HTTPX
def merge(other)
ivar_map = nil
other_ivars = case other
when Hash
when Options
other.instance_variables
else
other = Hash[other] unless other.is_a?(Hash)
ivar_map = other.keys.to_h { |k| [:"@#{k}", k] }
ivar_map.keys
else
other.instance_variables
end
return self if other_ivars.empty?

View File

@ -488,6 +488,8 @@ module HTTPX
label = pl
# raise Error, "Cannot add a plugin to a frozen config" if frozen?
pl = Plugins.load_plugin(pl) if pl.is_a?(Symbol)
raise ArgumentError, "Invalid plugin type: #{pl.class.inspect}" unless pl.is_a?(Module)
if !@plugins.include?(pl)
@plugins << pl
pl.load_dependencies(self, &block) if pl.respond_to?(:load_dependencies)

View File

@ -46,6 +46,18 @@ class SessionTest < Minitest::Test
body = response.body
assert body.respond_to?(:foo), "response body methods haven't been added"
assert body.foo == "response-body-foo", "response body method is unexpected"
# set default options via .plugin
klient_class2 = Class.new(HTTPX::Session)
klient_class2.plugin(TestPlugin, foo: "options-foo-2")
session2 = klient_class2.new
assert session2.options.foo == "options-foo-2", ":foo option was not overridden"
return if defined?(RBS)
# break if receiving something else
assert_raises(ArgumentError) { klient_class2.plugin(TestPlugin, :smth) }
assert_raises(ArgumentError) { klient_class2.plugin([TestPlugin, TestPlugin]) }
end
def test_session_subplugin