Skip to content

Commit

Permalink
Should honor Encoding.default_external.
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Janowski & Michel Martens committed Jul 22, 2010
1 parent 03cee15 commit 61fa1f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def reconnect
end

def read

# We read the first byte using read() mainly because gets() is
# immune to raw socket timeouts.
begin
Expand Down Expand Up @@ -182,7 +181,7 @@ def format_integer_reply(line)
def format_bulk_reply(line)
bulklen = line.to_i
return if bulklen == -1
reply = @sock.read(bulklen)
reply = encode(@sock.read(bulklen))
@sock.read(2) # Discard CRLF.
reply
end
Expand All @@ -191,9 +190,7 @@ def format_multi_bulk_reply(line)
n = line.to_i
return if n == -1

reply = []
n.times { reply << read }
reply
Array.new(n) { read }
end

def logging(commands)
Expand Down Expand Up @@ -291,5 +288,15 @@ def with_timeout(*args)
end
end
end

if defined?(Encoding)
def encode(string)
string.force_encoding(Encoding::default_external)
end
else
def encode(string)
string
end
end
end
end
19 changes: 19 additions & 0 deletions test/redis_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: UTF-8

require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
require File.expand_path(File.join(File.dirname(__FILE__), "redis_mock"))

Expand Down Expand Up @@ -1551,6 +1553,23 @@ class RedisTest < Test::Unit::TestCase
end
end

context "Encoding" do
should "return properly encoded strings" do
original_encoding = Encoding.default_external

begin
capture_stderr { Encoding.default_external = Encoding::UTF_8 }

@r.set "foo", "שלום"

assert_equal "Shalom שלום", "Shalom " + @r.get("foo")

ensure
capture_stderr { Encoding.default_external = original_encoding }
end
end
end if defined?(Encoding)

teardown do
@r.client.disconnect if @r
end
Expand Down

0 comments on commit 61fa1f8

Please sign in to comment.