diff --git a/lib/redis/namespace.rb b/lib/redis/namespace.rb index 0705321..0fac1da 100644 --- a/lib/redis/namespace.rb +++ b/lib/redis/namespace.rb @@ -488,7 +488,14 @@ def rem_namespace(key) key.each { |k| yielder.yield rem_namespace(k) } end else - key.to_s.sub(/\A#{@namespace}:/, '') + string_key = key.to_s.dup + if string_key.respond_to? :force_encoding + # force_encoding is not available in ruby 1.8.7 + rem_key = string_key.force_encoding('BINARY') + else + rem_key = string_key + end + rem_key.sub(/\A#{@namespace}:/, '') end end diff --git a/spec/redis_spec.rb b/spec/redis_spec.rb index 0499518..22ec375 100644 --- a/spec/redis_spec.rb +++ b/spec/redis_spec.rb @@ -366,6 +366,22 @@ @namespaced['foo'].should eq(nil) end + it "should not throw exception on invalid UTF-8 sequences in keys" do + @namespaced.set("f\xFCo", 'bar') + @namespaced.set("foo", 'bar') + @namespaced.get("f\xFCo").should eq('bar') + keys = @namespaced.keys.sort + # force_encoding is not available in ruby 1.8.7 + if "".respond_to? :force_encoding + keys.should eq ["f\xFCo".force_encoding('BINARY'), "foo"].sort + else + keys.should eq ["f\xFCo", "foo"].sort + end + keys.each do |k| + @namespaced.get(k).should eq('bar') + end + end + it "should respond to :namespace=" do @namespaced.respond_to?(:namespace=).should eq(true) end