Skip to content

Commit

Permalink
Handle parts of the command using incompatible encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Oct 13, 2021
1 parent a3d8071 commit 2df19ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/redis/connection/command_helper.rb
Expand Up @@ -12,11 +12,13 @@ def build_command(args)
if i.is_a? Array
i.each do |j|
j = j.to_s
j = j.encoding == Encoding::BINARY ? j : j.b
command << "$#{j.bytesize}"
command << j
end
else
i = i.to_s
i = i.encoding == Encoding::BINARY ? i : i.b
command << "$#{i.bytesize}"
command << i
end
Expand All @@ -33,6 +35,10 @@ def build_command(args)

def encode(string)
string.force_encoding(Encoding.default_external)
unless string.valid_encoding?
string.force_encoding(Encoding::BINARY)
end
string
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/client_test.rb
Expand Up @@ -73,4 +73,13 @@ def resolve
end
assert_equal 'Error connecting to Redis on 127.0.0.5:999 (Errno::ECONNREFUSED)', error.message
end

def test_mixed_encoding
r.call("MSET", "fée", "\x00\xFF".b, "じ案".encode(Encoding::SHIFT_JIS), "\t".encode(Encoding::ASCII))
assert_equal "\x00\xFF".b, r.call("GET", "fée")
assert_equal "\t", r.call("GET", "じ案".encode(Encoding::SHIFT_JIS))

r.call("SET", "\x00\xFF".b, "fée")
assert_equal "fée", r.call("GET", "\x00\xFF".b)
end
end
8 changes: 4 additions & 4 deletions test/distributed_commands_requiring_clustering_test.rb
Expand Up @@ -164,13 +164,13 @@ def test_bitop
r.set("{qux}bar", "b")

r.bitop(:and, "{qux}foo&bar", "{qux}foo", "{qux}bar")
assert_equal "\x60", r.get("{qux}foo&bar")
assert_equal "\x60".b, r.get("{qux}foo&bar")
r.bitop(:or, "{qux}foo|bar", "{qux}foo", "{qux}bar")
assert_equal "\x63", r.get("{qux}foo|bar")
assert_equal "\x63".b, r.get("{qux}foo|bar")
r.bitop(:xor, "{qux}foo^bar", "{qux}foo", "{qux}bar")
assert_equal "\x03", r.get("{qux}foo^bar")
assert_equal "\x03".b, r.get("{qux}foo^bar")
r.bitop(:not, "{qux}~foo", "{qux}foo")
assert_equal "\x9E", r.get("{qux}~foo")
assert_equal "\x9E".b, r.get("{qux}~foo")
end
end
end
8 changes: 4 additions & 4 deletions test/lint/strings.rb
Expand Up @@ -386,13 +386,13 @@ def test_bitop
r.set('bar{1}', 'b')

r.bitop(:and, 'foo&bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x60", r.get('foo&bar{1}')
assert_equal "\x60".b, r.get('foo&bar{1}')
r.bitop(:or, 'foo|bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x63", r.get('foo|bar{1}')
assert_equal "\x63".b, r.get('foo|bar{1}')
r.bitop(:xor, 'foo^bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x03", r.get('foo^bar{1}')
assert_equal "\x03".b, r.get('foo^bar{1}')
r.bitop(:not, '~foo{1}', 'foo{1}')
assert_equal "\x9E", r.get('~foo{1}')
assert_equal "\x9E".b, r.get('~foo{1}')
end
end
end
Expand Down

0 comments on commit 2df19ab

Please sign in to comment.