Permalink
Browse files

Merge pull request #290 from amcvitty/master

Allow RFC2965-style attributes in Cookie header
  • Loading branch information...
2 parents a6ac636 + f10ba07 commit c2bde15cce66ac54c1c0ebab19b360ba9e251145 @jamesdbloom committed on GitHub Oct 26, 2016
@@ -1,13 +1,11 @@
package org.mockserver.codec;
-import com.google.common.base.Splitter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.cookie.*;
-import org.apache.commons.lang3.StringUtils;
import org.mockserver.mappers.ContentTypeMapper;
import org.mockserver.model.*;
import org.mockserver.model.Cookie;
@@ -17,6 +15,7 @@
import java.nio.charset.Charset;
import java.util.List;
+import java.util.Set;
import static io.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
@@ -91,18 +90,16 @@ private void setHeaders(HttpRequest httpRequest, FullHttpRequest fullHttpRespons
httpRequest.withHeader(new Header(headerName, headers.getAll(headerName)));
}
}
-
+
private void setCookies(HttpRequest httpRequest, FullHttpRequest fullHttpResponse) {
for (String cookieHeader : fullHttpResponse.headers().getAll(COOKIE)) {
- for (String cookie : Splitter.on(";").split(cookieHeader)) {
- if (!cookie.trim().isEmpty()) {
- io.netty.handler.codec.http.cookie.Cookie decodedCookie =
- ClientCookieDecoder.LAX.decode(cookie);
- httpRequest.withCookie(new Cookie(
- decodedCookie.name(),
- decodedCookie.value()
- ));
- }
+ Set<io.netty.handler.codec.http.cookie.Cookie> decodedCookies =
+ ServerCookieDecoder.LAX.decode(cookieHeader);
+ for (io.netty.handler.codec.http.cookie.Cookie decodedCookie: decodedCookies) {
+ httpRequest.withCookie(new Cookie(
+ decodedCookie.name(),
+ decodedCookie.value()
+ ));
}
}
}
@@ -169,6 +169,27 @@ public void shouldDecodeCookiesWithEmbeddedEquals() {
));
}
+ /*
+ * Test is significant because popular Java REST library Jersey adds $Version=1 to all cookies
+ * in line with RFC2965's recommendation (even though RFC2965 is now marked "Obsolete" by
+ * RFC6265, this is still common and not hard to handle).
+ */
+ @Test
+ public void shouldDecodeCookiesWithRFC2965StyleAttributes() {
+ // given
+ fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/uri");
+ fullHttpRequest.headers().add("Cookie", "$Version=1; Customer=WILE_E_COYOTE; $Path=/acme");
+
+ // when
+ mockServerRequestDecoder.decode(null, fullHttpRequest, output);
+
+ // then
+ List<Cookie> cookies = ((HttpRequest) output.get(0)).getCookies();
+ assertThat(cookies, containsInAnyOrder(
+ cookie("Customer", "WILE_E_COYOTE")
+ ));
+ }
+
@Test
public void shouldDecodeBodyWithContentTypeAndNoCharset() {
// given

0 comments on commit c2bde15

Please sign in to comment.