Skip to content

Commit 65fc361

Browse files
nibintrustin
authored andcommitted
* Fixed the bug in CookieEncoder if there are no cookie's set while
calling encode(). Without the fix, it ended up in calling the exception "java.lang.StringIndexOutOfBoundsException". * Also added test case to verify the patch Change-Id: Ib96425e07ab50be027ade7be0748cceb6438a586
1 parent 0062cb7 commit 65fc361

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ private String encodeServerSide() {
167167
}
168168
}
169169

170-
sb.setLength(sb.length() - 1);
170+
if(sb.length() > 0)
171+
sb.setLength(sb.length() - 1);
171172
return sb.toString();
172173
}
173174

@@ -205,7 +206,8 @@ private String encodeClientSide() {
205206
}
206207
}
207208

208-
sb.setLength(sb.length() - 1);
209+
if(sb.length() > 0)
210+
sb.setLength(sb.length() - 1);
209211
return sb.toString();
210212
}
211213

src/test/java/org/jboss/netty/handler/codec/http/CookieEncoderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.fail;
20+
import static org.junit.Assert.assertNotNull;
2021

2122
import java.text.DateFormat;
2223
import java.util.Date;
@@ -132,4 +133,16 @@ public void testEncodingMultipleCookies() {
132133
String encodedCookie = encoder.encode();
133134
assertEquals(c1 + c2 + c3, encodedCookie);
134135
}
136+
137+
@Test
138+
public void testEncodingWithNoCookies() {
139+
CookieEncoder encoderForServer = new CookieEncoder(true);
140+
String encodedCookie1 = encoderForServer.encode();
141+
CookieEncoder encoderForClient = new CookieEncoder(false);
142+
String encodedCookie2 = encoderForClient.encode();
143+
assertNotNull(encodedCookie1);
144+
assertNotNull(encodedCookie2);
145+
146+
}
147+
135148
}

0 commit comments

Comments
 (0)