From f929afaa7b6529ebe364df5eba7c388805af3a33 Mon Sep 17 00:00:00 2001 From: Adrian Setyadi Date: Thu, 17 Oct 2019 19:00:09 +0700 Subject: Fix stringify_keys error when other is SessionHash --- lib/rack/session/abstract/id.rb | 8 ++++++++ test/spec_session_abstract_session_hash.rb | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/lib/rack/session/abstract/id.rb b/lib/rack/session/abstract/id.rb index c9258644..b15ee3b8 100644 --- a/lib/rack/session/abstract/id.rb +++ b/lib/rack/session/abstract/id.rb @@ -29,6 +29,14 @@ module Rack end } unless {}.respond_to?(:transform_keys) + def transform_keys(&block) + hash = dup + each do |key, value| + hash[block.call(key)] = value + end + hash + end + include Enumerable attr_writer :id diff --git a/test/spec_session_abstract_session_hash.rb b/test/spec_session_abstract_session_hash.rb index 206e2c1b..5d0d10ce 100644 --- a/test/spec_session_abstract_session_hash.rb +++ b/test/spec_session_abstract_session_hash.rb @@ -44,4 +44,10 @@ describe Rack::Session::Abstract::SessionHash do lambda { hash.fetch(:unknown) }.must_raise KeyError end end + + describe "#stringify_keys" do + it "returns hash or session hash with keys stringified" do + assert_equal({ "foo" => :bar, "baz" => :qux }, hash.send(:stringify_keys, hash).to_h) + end + end end -- cgit v1.2.3-24-ge0c7