Print correct invalid character after unwrapping value in CookieEncoder

Motivation:

If a wrapped cookie value with an invalid charcater is passed to the strict
encoder, an exception is thrown on validation but the error message contains
a character at the wrong position.

Modifications:

Print `unwrappedValue.charAt(pos)` instead of `value.charAt(pos)`.

Result:

The exception indicates the correct invalid character in the unwrapped cookie.
This commit is contained in:
Stefan Lance 2018-06-20 10:24:29 -07:00 committed by Norman Maurer
parent 9b95b8ee62
commit 4c709be1ab
2 changed files with 13 additions and 1 deletions

View File

@ -44,7 +44,8 @@ public abstract class CookieEncoder {
}
if ((pos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
throw new IllegalArgumentException("Cookie value contains an invalid char: " + value.charAt(pos));
throw new IllegalArgumentException("Cookie value contains an invalid char: " +
unwrappedValue.charAt(pos));
}
}
}

View File

@ -18,7 +18,9 @@ package io.netty.handler.codec.http.cookie;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;
import io.netty.handler.codec.DateFormatter;
import java.text.ParseException;
@ -131,6 +133,15 @@ public class ServerCookieEncoderTest {
assertEquals(illegalChars.size(), exceptions);
}
@Test
public void illegalCharInWrappedValueAppearsInException() {
try {
ServerCookieEncoder.STRICT.encode(new DefaultCookie("name", "\"value,\""));
} catch (IllegalArgumentException e) {
assertThat(e.getMessage().toLowerCase(), containsString("cookie value contains an invalid char: ,"));
}
}
@Test
public void testEncodingMultipleCookiesLax() {
List<String> result = new ArrayList<String>();