Skip to content

Commit

Permalink
Encode ScanArgs.matches using UTF-8 #792
Browse files Browse the repository at this point in the history
Lettuce now uses UTF-8 encoding to encode the matches string to bytes. Previously, we used ASCII which is more limiting when using non-ascii chars.
  • Loading branch information
mp911de committed Jun 11, 2018
1 parent 25f54d0 commit 6980d4f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/lambdaworks/redis/ScanArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.lambdaworks.redis.protocol.CommandKeyword.COUNT;
import static com.lambdaworks.redis.protocol.CommandKeyword.MATCH;

import java.nio.charset.StandardCharsets;

import com.lambdaworks.redis.internal.LettuceAssert;
import com.lambdaworks.redis.protocol.CommandArgs;

Expand Down Expand Up @@ -99,7 +101,7 @@ public ScanArgs limit(long count) {
<K, V> void build(CommandArgs<K, V> args) {

if (match != null) {
args.add(MATCH).add(match);
args.add(MATCH).add(match.getBytes(StandardCharsets.UTF_8));
}

if (count != null) {
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/lambdaworks/redis/ScanArgsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lambdaworks.redis;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import com.lambdaworks.redis.codec.StringCodec;
import com.lambdaworks.redis.protocol.CommandArgs;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
* @author Mark Paluch
*/
public class ScanArgsTest {

@Test
public void shouldEncodeMatchUsingUtf8() {

ScanArgs args = ScanArgs.Builder.matches("ö");

CommandArgs<String, String> commandArgs = new CommandArgs<>(StringCodec.UTF8);
args.build(commandArgs);

ByteBuf buffer = Unpooled.buffer();
commandArgs.encode(buffer);

ByteBuf expected = Unpooled.buffer();
expected.writeBytes("$5\r\nMATCH\r\n".getBytes());
expected.writeBytes("$2\r\n".getBytes());
expected.writeByte(-61); // encoded ö
expected.writeByte(-74);
expected.writeBytes("\r\n".getBytes());

assertThat(buffer).isEqualTo(expected);
}
}

0 comments on commit 6980d4f

Please sign in to comment.