From 6cf93c8b8da76aebc0bce8b2fe7ff2cb8f928176 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 21 Jan 2015 22:24:46 +0900 Subject: [PATCH] Remove Rfc6265 prefix from cookie encoders and decoders Motivation: Rfc6265Client/ServerCookieEncoder is a better replacement of the old Client/ServerCookieEncoder, and thus there's no point of keeping both. Modifications: - Remove the old Client/ServerCookieEncoder - Remove the 'Rfc6265' prefix from the new cookie encoder/decoder classes - Deprecate CookieDecoder Result: We have much better cookie encoder/decoder implementation now. --- ...eDecoder.java => ClientCookieDecoder.java} | 10 +- .../codec/http/ClientCookieEncoder.java | 89 ++-- .../handler/codec/http/CookieDecoder.java | 3 + .../http/Rfc6265ClientCookieEncoder.java | 128 ----- .../http/Rfc6265ServerCookieEncoder.java | 172 ------- ...eDecoder.java => ServerCookieDecoder.java} | 10 +- .../codec/http/ServerCookieEncoder.java | 117 ++--- ...Test.java => ClientCookieDecoderTest.java} | 34 +- ...Test.java => ClientCookieEncoderTest.java} | 4 +- .../handler/codec/http/CookieDecoderTest.java | 474 ------------------ .../handler/codec/http/CookieEncoderTest.java | 131 ----- ...Test.java => ServerCookieDecoderTest.java} | 14 +- ...Test.java => ServerCookieEncoderTest.java} | 12 +- 13 files changed, 154 insertions(+), 1044 deletions(-) rename codec-http/src/main/java/io/netty/handler/codec/http/{Rfc6265ClientCookieDecoder.java => ClientCookieDecoder.java} (98%) delete mode 100644 codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieEncoder.java delete mode 100644 codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoder.java rename codec-http/src/main/java/io/netty/handler/codec/http/{Rfc6265ServerCookieDecoder.java => ServerCookieDecoder.java} (98%) rename codec-http/src/test/java/io/netty/handler/codec/http/{Rfc6265ClientCookieDecoderTest.java => ClientCookieDecoderTest.java} (92%) rename codec-http/src/test/java/io/netty/handler/codec/http/{Rfc6265ClientCookieEncoderTest.java => ClientCookieEncoderTest.java} (93%) delete mode 100644 codec-http/src/test/java/io/netty/handler/codec/http/CookieDecoderTest.java delete mode 100644 codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java rename codec-http/src/test/java/io/netty/handler/codec/http/{Rfc6265ServerCookieDecoderTest.java => ServerCookieDecoderTest.java} (94%) rename codec-http/src/test/java/io/netty/handler/codec/http/{Rfc6265ServerCookieEncoderTest.java => ServerCookieEncoderTest.java} (83%) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieDecoder.java similarity index 98% rename from codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoder.java rename to codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieDecoder.java index ffc31da746..c4b0025837 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieDecoder.java @@ -15,20 +15,20 @@ */ package io.netty.handler.codec.http; -import static io.netty.handler.codec.http.CookieEncoderUtil.stringBuilder; - import java.text.ParsePosition; import java.util.Date; +import static io.netty.handler.codec.http.CookieEncoderUtil.*; + /** * A RFC6265 compliant cookie decoder to be used client side. * * It will store the raw value in {@link Cookie#setRawValue(String)} so it can be * eventually sent back to the Origin server as is. * - * @see Rfc6265ClientCookieEncoder + * @see ClientCookieEncoder */ -public final class Rfc6265ClientCookieDecoder { +public final class ClientCookieDecoder { /** * Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}. @@ -307,7 +307,7 @@ public final class Rfc6265ClientCookieDecoder { } } - private Rfc6265ClientCookieDecoder() { + private ClientCookieDecoder() { // unused } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java index 308e890f1a..6cefdb674c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2014 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance @@ -18,25 +18,41 @@ package io.netty.handler.codec.http; import static io.netty.handler.codec.http.CookieEncoderUtil.*; /** - * Encodes client-side {@link Cookie}s into an HTTP header value. This encoder can encode - * the HTTP cookie version 0, 1, and 2. + * A RFC6265 compliant cookie encoder to be used client side, + * so only name=value pairs are sent. + * + * User-Agents are not supposed to interpret cookies, so, if present, {@link Cookie#rawValue()} will be used. + * Otherwise, {@link Cookie#value()} will be used unquoted. + * + * Note that multiple cookies are supposed to be sent at once in a single "Cookie" header. + * *
  * // Example
  * {@link HttpRequest} req = ...;
  * res.setHeader("Cookie", {@link ClientCookieEncoder}.encode("JSESSIONID", "1234"));
  * 
* - * @see CookieDecoder + * @see ClientCookieDecoder */ public final class ClientCookieEncoder { /** - * Encodes the specified cookie into an HTTP header value. + * Encodes the specified cookie into a Cookie header value. + * + * @param name the cookie name + * @param value the cookie value + * @return a Rfc6265 style Cookie header value */ public static String encode(String name, String value) { return encode(new DefaultCookie(name, value)); } + /** + * Encodes the specified cookie into a Cookie header value. + * + * @param specified the cookie + * @return a Rfc6265 style Cookie header value + */ public static String encode(Cookie cookie) { if (cookie == null) { throw new NullPointerException("cookie"); @@ -47,71 +63,66 @@ public final class ClientCookieEncoder { return stripTrailingSeparator(buf); } + /** + * Encodes the specified cookies into a single Cookie header value. + * + * @param cookies some cookies + * @return a Rfc6265 style Cookie header value, null if no cookies are passed. + */ public static String encode(Cookie... cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } + if (cookies.length == 0) { + return null; + } + StringBuilder buf = stringBuilder(); - for (Cookie c: cookies) { + for (Cookie c : cookies) { if (c == null) { break; } encode(buf, c); } - return stripTrailingSeparator(buf); + return stripTrailingSeparatorOrNull(buf); } + /** + * Encodes the specified cookies into a single Cookie header value. + * + * @param cookies some cookies + * @return a Rfc6265 style Cookie header value, null if no cookies are passed. + */ public static String encode(Iterable cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } + if (!cookies.iterator().hasNext()) { + return null; + } + StringBuilder buf = stringBuilder(); - for (Cookie c: cookies) { + for (Cookie c : cookies) { if (c == null) { break; } encode(buf, c); } - return stripTrailingSeparator(buf); + return stripTrailingSeparatorOrNull(buf); } private static void encode(StringBuilder buf, Cookie c) { - if (c.version() >= 1) { - add(buf, '$' + CookieHeaderNames.VERSION, 1); - } - - add(buf, c.name(), c.value()); - - if (c.path() != null) { - add(buf, '$' + CookieHeaderNames.PATH, c.path()); - } - - if (c.domain() != null) { - add(buf, '$' + CookieHeaderNames.DOMAIN, c.domain()); - } - - if (c.version() >= 1) { - if (!c.ports().isEmpty()) { - buf.append('$'); - buf.append(CookieHeaderNames.PORT); - buf.append((char) HttpConstants.EQUALS); - buf.append((char) HttpConstants.DOUBLE_QUOTE); - for (int port: c.ports()) { - buf.append(port); - buf.append((char) HttpConstants.COMMA); - } - buf.setCharAt(buf.length() - 1, (char) HttpConstants.DOUBLE_QUOTE); - buf.append((char) HttpConstants.SEMICOLON); - buf.append((char) HttpConstants.SP); - } - } + // rawValue > value > "" + String value = c.rawValue() != null ? c.rawValue() + : c.value() != null ? c.value() : ""; + addUnquoted(buf, c.name(), value); } private ClientCookieEncoder() { - // Unused + // unused } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java index 3923302b87..d05a1cc274 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/CookieDecoder.java @@ -25,6 +25,8 @@ import java.util.Set; import java.util.TreeSet; /** + * @deprecated Use {@link ClientCookieDecoder} or {@link ServerCookieDecoder} instead. + * * Decodes an HTTP header value into {@link Cookie}s. This decoder can decode * the HTTP cookie version 0, 1, and 2. * @@ -37,6 +39,7 @@ import java.util.TreeSet; * @see ClientCookieEncoder * @see ServerCookieEncoder */ +@Deprecated public final class CookieDecoder { private static final char COMMA = ','; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieEncoder.java deleted file mode 100644 index 0834a5405b..0000000000 --- a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ClientCookieEncoder.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2014 The Netty Project - * - * The Netty Project licenses this file to you 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 io.netty.handler.codec.http; - -import static io.netty.handler.codec.http.CookieEncoderUtil.*; - -/** - * A RFC6265 compliant cookie encoder to be used client side, - * so only name=value pairs are sent. - * - * User-Agents are not supposed to interpret cookies, so, if present, {@link Cookie#rawValue()} will be used. - * Otherwise, {@link Cookie#value()} will be used unquoted. - * - * Note that multiple cookies are supposed to be sent at once in a single "Cookie" header. - * - *
- * // Example
- * {@link HttpRequest} req = ...;
- * res.setHeader("Cookie", {@link Rfc6265ClientCookieEncoder}.encode("JSESSIONID", "1234"));
- * 
- * - * @see Rfc6265ClientCookieDecoder - */ -public final class Rfc6265ClientCookieEncoder { - - /** - * Encodes the specified cookie into a Cookie header value. - * - * @param name the cookie name - * @param value the cookie value - * @return a Rfc6265 style Cookie header value - */ - public static String encode(String name, String value) { - return encode(new DefaultCookie(name, value)); - } - - /** - * Encodes the specified cookie into a Cookie header value. - * - * @param specified the cookie - * @return a Rfc6265 style Cookie header value - */ - public static String encode(Cookie cookie) { - if (cookie == null) { - throw new NullPointerException("cookie"); - } - - StringBuilder buf = stringBuilder(); - encode(buf, cookie); - return stripTrailingSeparator(buf); - } - - /** - * Encodes the specified cookies into a single Cookie header value. - * - * @param cookies some cookies - * @return a Rfc6265 style Cookie header value, null if no cookies are passed. - */ - public static String encode(Cookie... cookies) { - if (cookies == null) { - throw new NullPointerException("cookies"); - } - - if (cookies.length == 0) { - return null; - } - - StringBuilder buf = stringBuilder(); - for (Cookie c : cookies) { - if (c == null) { - break; - } - - encode(buf, c); - } - return stripTrailingSeparatorOrNull(buf); - } - - /** - * Encodes the specified cookies into a single Cookie header value. - * - * @param cookies some cookies - * @return a Rfc6265 style Cookie header value, null if no cookies are passed. - */ - public static String encode(Iterable cookies) { - if (cookies == null) { - throw new NullPointerException("cookies"); - } - - if (!cookies.iterator().hasNext()) { - return null; - } - - StringBuilder buf = stringBuilder(); - for (Cookie c : cookies) { - if (c == null) { - break; - } - - encode(buf, c); - } - return stripTrailingSeparatorOrNull(buf); - } - - private static void encode(StringBuilder buf, Cookie c) { - // rawValue > value > "" - String value = c.rawValue() != null ? c.rawValue() - : c.value() != null ? c.value() : ""; - addUnquoted(buf, c.name(), value); - } - - private Rfc6265ClientCookieEncoder() { - // unused - } -} diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoder.java deleted file mode 100644 index 20dad0fba0..0000000000 --- a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoder.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2014 The Netty Project - * - * The Netty Project licenses this file to you 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 io.netty.handler.codec.http; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import static io.netty.handler.codec.http.CookieEncoderUtil.*; - -/** - * A RFC6265 compliant cookie encoder to be used server side, - * so some fields are sent (Version is typically ignored). - * - * As Netty's Cookie merges Expires and MaxAge into one single field, only Max-Age field is sent. - * - * Note that multiple cookies are supposed to be sent at once in a single "Set-Cookie" header. - * - *
- * // Example
- * {@link HttpRequest} req = ...;
- * res.setHeader("Cookie", {@link Rfc6265ServerCookieEncoder}.encode("JSESSIONID", "1234"));
- * 
- * - * @see Rfc6265ServerCookieDecoder - */ -public final class Rfc6265ServerCookieEncoder { - - /** - * Encodes the specified cookie name-value pair into a Set-Cookie header value. - * - * @param name the cookie name - * @param value the cookie value - * @return a single Set-Cookie header value - */ - public static String encode(String name, String value) { - return encode(new DefaultCookie(name, value)); - } - - /** - * Encodes the specified cookie into a Set-Cookie header value. - * - * @param cookie the cookie - * @return a single Set-Cookie header value - */ - public static String encode(Cookie cookie) { - if (cookie == null) { - throw new NullPointerException("cookie"); - } - - StringBuilder buf = stringBuilder(); - - addUnquoted(buf, cookie.name(), cookie.value()); - - if (cookie.maxAge() != Long.MIN_VALUE) { - add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge()); - } - - if (cookie.path() != null) { - addUnquoted(buf, CookieHeaderNames.PATH, cookie.path()); - } - - if (cookie.domain() != null) { - addUnquoted(buf, CookieHeaderNames.DOMAIN, cookie.domain()); - } - if (cookie.isSecure()) { - buf.append(CookieHeaderNames.SECURE); - buf.append((char) HttpConstants.SEMICOLON); - buf.append((char) HttpConstants.SP); - } - if (cookie.isHttpOnly()) { - buf.append(CookieHeaderNames.HTTPONLY); - buf.append((char) HttpConstants.SEMICOLON); - buf.append((char) HttpConstants.SP); - } - - return stripTrailingSeparator(buf); - } - - /** - * Batch encodes cookies into Set-Cookie header values. - * - * @param cookies a bunch of cookies - * @return the corresponding bunch of Set-Cookie headers - */ - public static List encode(Cookie... cookies) { - if (cookies == null) { - throw new NullPointerException("cookies"); - } - - if (cookies.length == 0) { - return Collections.emptyList(); - } - - List encoded = new ArrayList(cookies.length); - for (Cookie c : cookies) { - if (c == null) { - break; - } - encoded.add(encode(c)); - } - return encoded; - } - - /** - * Batch encodes cookies into Set-Cookie header values. - * - * @param cookies a bunch of cookies - * @return the corresponding bunch of Set-Cookie headers - */ - public static List encode(Collection cookies) { - if (cookies == null) { - throw new NullPointerException("cookies"); - } - - if (cookies.isEmpty()) { - return Collections.emptyList(); - } - - List encoded = new ArrayList(cookies.size()); - for (Cookie c : cookies) { - if (c == null) { - break; - } - encoded.add(encode(c)); - } - return encoded; - } - - /** - * Batch encodes cookies into Set-Cookie header values. - * - * @param cookies a bunch of cookies - * @return the corresponding bunch of Set-Cookie headers - */ - public static List encode(Iterable cookies) { - if (cookies == null) { - throw new NullPointerException("cookies"); - } - - if (!cookies.iterator().hasNext()) { - return Collections.emptyList(); - } - - List encoded = new ArrayList(); - for (Cookie c : cookies) { - if (c == null) { - break; - } - encoded.add(encode(c)); - } - return encoded; - } - - private Rfc6265ServerCookieEncoder() { - // Unused - } -} diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieDecoder.java similarity index 98% rename from codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoder.java rename to codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieDecoder.java index ab53b46798..d5361da4c4 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieDecoder.java @@ -15,12 +15,12 @@ */ package io.netty.handler.codec.http; -import static io.netty.handler.codec.http.CookieEncoderUtil.*; - import java.util.Collections; import java.util.Set; import java.util.TreeSet; +import static io.netty.handler.codec.http.CookieEncoderUtil.*; + /** * A RFC6265 compliant cookie decoder to be used server side. * @@ -29,9 +29,9 @@ import java.util.TreeSet; * Old RFC2965 cookies are still supported, * old fields will simply be ignored. * - * @see Rfc6265ServerCookieEncoder + * @see ServerCookieEncoder */ -public final class Rfc6265ServerCookieDecoder { +public final class ServerCookieDecoder { /** * Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}. @@ -176,7 +176,7 @@ public final class Rfc6265ServerCookieDecoder { return cookies; } - private Rfc6265ServerCookieDecoder() { + private ServerCookieDecoder() { // unused } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java index 3c71ddcc66..13b301fd4f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2014 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance @@ -17,31 +17,46 @@ package io.netty.handler.codec.http; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; +import java.util.Collections; import java.util.List; import static io.netty.handler.codec.http.CookieEncoderUtil.*; /** - * Encodes server-side {@link Cookie}s into HTTP header values. This encoder can encode - * the HTTP cookie version 0, 1, and 2. + * A RFC6265 compliant cookie encoder to be used server side, + * so some fields are sent (Version is typically ignored). + * + * As Netty's Cookie merges Expires and MaxAge into one single field, only Max-Age field is sent. + * + * Note that multiple cookies are supposed to be sent at once in a single "Set-Cookie" header. + * *
  * // Example
  * {@link HttpRequest} req = ...;
- * res.setHeader("Set-Cookie", {@link ServerCookieEncoder}.encode("JSESSIONID", "1234"));
+ * res.setHeader("Cookie", {@link ServerCookieEncoder}.encode("JSESSIONID", "1234"));
  * 
* - * @see CookieDecoder + * @see ServerCookieDecoder */ public final class ServerCookieEncoder { /** - * Encodes the specified cookie into an HTTP header value. + * Encodes the specified cookie name-value pair into a Set-Cookie header value. + * + * @param name the cookie name + * @param value the cookie value + * @return a single Set-Cookie header value */ public static String encode(String name, String value) { return encode(new DefaultCookie(name, value)); } + /** + * Encodes the specified cookie into a Set-Cookie header value. + * + * @param cookie the cookie + * @return a single Set-Cookie header value + */ public static String encode(Cookie cookie) { if (cookie == null) { throw new NullPointerException("cookie"); @@ -49,33 +64,18 @@ public final class ServerCookieEncoder { StringBuilder buf = stringBuilder(); - add(buf, cookie.name(), cookie.value()); + addUnquoted(buf, cookie.name(), cookie.value()); if (cookie.maxAge() != Long.MIN_VALUE) { - if (cookie.version() == 0) { - addUnquoted(buf, CookieHeaderNames.EXPIRES, - HttpHeaderDateFormat.get().format( - new Date(System.currentTimeMillis() + - cookie.maxAge() * 1000L))); - } else { - add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge()); - } + add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge()); } if (cookie.path() != null) { - if (cookie.version() > 0) { - add(buf, CookieHeaderNames.PATH, cookie.path()); - } else { - addUnquoted(buf, CookieHeaderNames.PATH, cookie.path()); - } + addUnquoted(buf, CookieHeaderNames.PATH, cookie.path()); } if (cookie.domain() != null) { - if (cookie.version() > 0) { - add(buf, CookieHeaderNames.DOMAIN, cookie.domain()); - } else { - addUnquoted(buf, CookieHeaderNames.DOMAIN, cookie.domain()); - } + addUnquoted(buf, CookieHeaderNames.DOMAIN, cookie.domain()); } if (cookie.isSecure()) { buf.append(CookieHeaderNames.SECURE); @@ -87,46 +87,27 @@ public final class ServerCookieEncoder { buf.append((char) HttpConstants.SEMICOLON); buf.append((char) HttpConstants.SP); } - if (cookie.version() >= 1) { - if (cookie.comment() != null) { - add(buf, CookieHeaderNames.COMMENT, cookie.comment()); - } - - add(buf, CookieHeaderNames.VERSION, 1); - - if (cookie.commentUrl() != null) { - addQuoted(buf, CookieHeaderNames.COMMENTURL, cookie.commentUrl()); - } - - if (!cookie.ports().isEmpty()) { - buf.append(CookieHeaderNames.PORT); - buf.append((char) HttpConstants.EQUALS); - buf.append((char) HttpConstants.DOUBLE_QUOTE); - for (int port: cookie.ports()) { - buf.append(port); - buf.append((char) HttpConstants.COMMA); - } - buf.setCharAt(buf.length() - 1, (char) HttpConstants.DOUBLE_QUOTE); - buf.append((char) HttpConstants.SEMICOLON); - buf.append((char) HttpConstants.SP); - } - if (cookie.isDiscard()) { - buf.append(CookieHeaderNames.DISCARD); - buf.append((char) HttpConstants.SEMICOLON); - buf.append((char) HttpConstants.SP); - } - } return stripTrailingSeparator(buf); } + /** + * Batch encodes cookies into Set-Cookie header values. + * + * @param cookies a bunch of cookies + * @return the corresponding bunch of Set-Cookie headers + */ public static List encode(Cookie... cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } + if (cookies.length == 0) { + return Collections.emptyList(); + } + List encoded = new ArrayList(cookies.length); - for (Cookie c: cookies) { + for (Cookie c : cookies) { if (c == null) { break; } @@ -135,13 +116,23 @@ public final class ServerCookieEncoder { return encoded; } + /** + * Batch encodes cookies into Set-Cookie header values. + * + * @param cookies a bunch of cookies + * @return the corresponding bunch of Set-Cookie headers + */ public static List encode(Collection cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } + if (cookies.isEmpty()) { + return Collections.emptyList(); + } + List encoded = new ArrayList(cookies.size()); - for (Cookie c: cookies) { + for (Cookie c : cookies) { if (c == null) { break; } @@ -150,13 +141,23 @@ public final class ServerCookieEncoder { return encoded; } + /** + * Batch encodes cookies into Set-Cookie header values. + * + * @param cookies a bunch of cookies + * @return the corresponding bunch of Set-Cookie headers + */ public static List encode(Iterable cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } + if (!cookies.iterator().hasNext()) { + return Collections.emptyList(); + } + List encoded = new ArrayList(); - for (Cookie c: cookies) { + for (Cookie c : cookies) { if (c == null) { break; } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/ClientCookieDecoderTest.java similarity index 92% rename from codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoderTest.java rename to codec-http/src/test/java/io/netty/handler/codec/http/ClientCookieDecoderTest.java index 5f7d3f1287..c254d8447c 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ClientCookieDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/ClientCookieDecoderTest.java @@ -31,14 +31,14 @@ import java.util.TimeZone; import org.junit.Test; -public class Rfc6265ClientCookieDecoderTest { +public class ClientCookieDecoderTest { @Test public void testDecodingSingleCookieV0() { String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;"; cookieString = cookieString.replace("XXX", HttpHeaderDateFormat.get() .format(new Date(System.currentTimeMillis() + 50000))); - Cookie cookie = Rfc6265ClientCookieDecoder.decode(cookieString); + Cookie cookie = ClientCookieDecoder.decode(cookieString); assertNotNull(cookie); assertEquals("myValue", cookie.value()); assertEquals(".adomainsomewhere", cookie.domain()); @@ -63,7 +63,7 @@ public class Rfc6265ClientCookieDecoderTest { String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + "domain=.adomainsomewhere;secure;comment=this is a comment;version=0;" + "commentURL=http://aurl.com;port=\"80,8080\";discard;"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(cookieString); + Cookie cookie = ClientCookieDecoder.decode(cookieString); assertNotNull(cookie); assertEquals("myValue", cookie.value()); assertEquals(".adomainsomewhere", cookie.domain()); @@ -76,7 +76,7 @@ public class Rfc6265ClientCookieDecoderTest { public void testDecodingSingleCookieV1() { String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere" + ";secure;comment=this is a comment;version=1;"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(cookieString); + Cookie cookie = ClientCookieDecoder.decode(cookieString); assertEquals("myValue", cookie.value()); assertNotNull(cookie); assertEquals(".adomainsomewhere", cookie.domain()); @@ -90,7 +90,7 @@ public class Rfc6265ClientCookieDecoderTest { String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + "domain=.adomainsomewhere;secure;comment=this is a comment;version=1;" + "commentURL=http://aurl.com;port='80,8080';discard;"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(cookieString); + Cookie cookie = ClientCookieDecoder.decode(cookieString); assertNotNull(cookie); assertEquals("myValue", cookie.value()); assertEquals(".adomainsomewhere", cookie.domain()); @@ -104,7 +104,7 @@ public class Rfc6265ClientCookieDecoderTest { String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + "domain=.adomainsomewhere;secure;comment=this is a comment;version=2;" + "commentURL=http://aurl.com;port=\"80,8080\";discard;"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(cookieString); + Cookie cookie = ClientCookieDecoder.decode(cookieString); assertNotNull(cookie); assertEquals("myValue", cookie.value()); assertEquals(".adomainsomewhere", cookie.domain()); @@ -119,7 +119,7 @@ public class Rfc6265ClientCookieDecoderTest { + "domain=.adomainsomewhere;secure;comment=this is a comment;version=2;" + "commentURL=\"http://aurl.com\";port='80,8080';discard;"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(c1); + Cookie cookie = ClientCookieDecoder.decode(c1); assertNotNull(cookie); assertEquals("myValue", cookie.value()); assertEquals(".adomainsomewhere", cookie.domain()); @@ -142,7 +142,7 @@ public class Rfc6265ClientCookieDecoderTest { Collection cookies = new ArrayList(); for (String source : sources) { - cookies.add(Rfc6265ClientCookieDecoder.decode(source)); + cookies.add(ClientCookieDecoder.decode(source)); } Iterator it = cookies.iterator(); @@ -191,7 +191,7 @@ public class Rfc6265ClientCookieDecoderTest { + "__utmb=48461872.13.10.1258140131; __utmc=48461872; " + "__utmz=48461872.1258140131.1.1.utmcsr=overstock.com|utmccn=(referral)|" + "utmcmd=referral|utmcct=/Home-Garden/Furniture/Clearance,/clearance,/32/dept.html"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(source); + Cookie cookie = ClientCookieDecoder.decode(source); assertEquals("ARPT", cookie.name()); assertEquals("LWUKQPSWRTUN04CKKJI", cookie.value()); @@ -206,7 +206,7 @@ public class Rfc6265ClientCookieDecoderTest { String source = "Format=EU; expires=Fri, 31-Dec-9999 23:59:59 GMT; path=/"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(source); + Cookie cookie = ClientCookieDecoder.decode(source); assertTrue(Math.abs(expectedMaxAge - cookie.maxAge()) < 2); } @@ -216,7 +216,7 @@ public class Rfc6265ClientCookieDecoderTest { String source = "UserCookie=timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=®ion=BE;" + " expires=Sat, 01-Dec-2012 10:53:31 GMT; path=/"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(source); + Cookie cookie = ClientCookieDecoder.decode(source); assertEquals( "timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=®ion=BE", @@ -226,7 +226,7 @@ public class Rfc6265ClientCookieDecoderTest { @Test public void testDecodingWeirdNames1() { String src = "path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(src); + Cookie cookie = ClientCookieDecoder.decode(src); assertEquals("path", cookie.name()); assertEquals("", cookie.value()); assertEquals("/", cookie.path()); @@ -235,7 +235,7 @@ public class Rfc6265ClientCookieDecoderTest { @Test public void testDecodingWeirdNames2() { String src = "HTTPOnly="; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(src); + Cookie cookie = ClientCookieDecoder.decode(src); assertEquals("HTTPOnly", cookie.name()); assertEquals("", cookie.value()); } @@ -243,7 +243,7 @@ public class Rfc6265ClientCookieDecoderTest { @Test public void testDecodingValuesWithCommasAndEquals() { String src = "A=v=1&lg=en-US,it-IT,it&intl=it&np=1;T=z=E"; - Cookie cookie = Rfc6265ClientCookieDecoder.decode(src); + Cookie cookie = ClientCookieDecoder.decode(src); assertEquals("A", cookie.name()); assertEquals("v=1&lg=en-US,it-IT,it&intl=it&np=1", cookie.value()); } @@ -295,8 +295,8 @@ public class Rfc6265ClientCookieDecoderTest { + "%=KqtH!$?mi!!!!'=KqtH!$?mx!!!!'=KqtH!$D7]!!!!#=J_#p!$D@T!!!!#=J_#p!$V cookies = CookieDecoder.decode(cookieString); - assertEquals(1, cookies.size()); - Cookie cookie = cookies.iterator().next(); - assertNotNull(cookie); - assertEquals("myValue", cookie.value()); - assertNull(cookie.comment()); - assertNull(cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertFalse(cookie.isDiscard()); - - boolean fail = true; - for (int i = 40; i <= 60; i ++) { - if (cookie.maxAge() == i) { - fail = false; - break; - } - } - if (fail) { - fail("expected: 50, actual: " + cookie.maxAge()); - } - - assertEquals("/apathsomewhere", cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertTrue(cookie.isSecure()); - assertEquals(0, cookie.version()); - } - - @Test - public void testDecodingSingleCookieV0ExtraParamsIgnored() { - String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + - "domain=.adomainsomewhere;secure;comment=this is a comment;version=0;" + - "commentURL=http://aurl.com;port=\"80,8080\";discard;"; - Set cookies = CookieDecoder.decode(cookieString); - assertEquals(1, cookies.size()); - Cookie cookie = cookies.iterator().next(); - assertNotNull(cookie); - assertEquals("myValue", cookie.value()); - assertNull(cookie.comment()); - assertNull(cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertFalse(cookie.isDiscard()); - assertEquals(50, cookie.maxAge()); - assertEquals("/apathsomewhere", cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertTrue(cookie.isSecure()); - assertEquals(0, cookie.version()); - } - @Test - public void testDecodingSingleCookieV1() { - String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + - "domain=.adomainsomewhere;secure;comment=this is a comment;version=1;"; - Set cookies = CookieDecoder.decode(cookieString); - assertEquals(1, cookies.size()); - Cookie cookie = cookies.iterator().next(); - assertEquals("myValue", cookie.value()); - assertNotNull(cookie); - assertEquals("this is a comment", cookie.comment()); - assertNull(cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertFalse(cookie.isDiscard()); - assertEquals(50, cookie.maxAge()); - assertEquals("/apathsomewhere", cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertTrue(cookie.isSecure()); - assertEquals(1, cookie.version()); - } - - @Test - public void testDecodingSingleCookieV1ExtraParamsIgnored() { - String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + - "domain=.adomainsomewhere;secure;comment=this is a comment;version=1;" + - "commentURL=http://aurl.com;port='80,8080';discard;"; - Set cookies = CookieDecoder.decode(cookieString); - assertEquals(1, cookies.size()); - Cookie cookie = cookies.iterator().next(); - assertNotNull(cookie); - assertEquals("myValue", cookie.value()); - assertEquals("this is a comment", cookie.comment()); - assertNull(cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertFalse(cookie.isDiscard()); - assertEquals(50, cookie.maxAge()); - assertEquals("/apathsomewhere", cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertTrue(cookie.isSecure()); - assertEquals(1, cookie.version()); - } - @Test - public void testDecodingSingleCookieV2() { - String cookieString = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + - "domain=.adomainsomewhere;secure;comment=this is a comment;version=2;" + - "commentURL=http://aurl.com;port=\"80,8080\";discard;"; - Set cookies = CookieDecoder.decode(cookieString); - assertEquals(1, cookies.size()); - Cookie cookie = cookies.iterator().next(); - assertNotNull(cookie); - assertEquals("myValue", cookie.value()); - assertEquals("this is a comment", cookie.comment()); - assertEquals("http://aurl.com", cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertTrue(cookie.isDiscard()); - assertEquals(50, cookie.maxAge()); - assertEquals("/apathsomewhere", cookie.path()); - assertEquals(2, cookie.ports().size()); - assertTrue(cookie.ports().contains(80)); - assertTrue(cookie.ports().contains(8080)); - assertTrue(cookie.isSecure()); - assertEquals(2, cookie.version()); - } - - @Test - public void testDecodingMultipleCookies() { - String c1 = "myCookie=myValue;max-age=50;path=/apathsomewhere;" + - "domain=.adomainsomewhere;secure;comment=this is a comment;version=2;" + - "commentURL=\"http://aurl.com\";port='80,8080';discard;"; - String c2 = "myCookie2=myValue2;max-age=0;path=/anotherpathsomewhere;" + - "domain=.anotherdomainsomewhere;comment=this is another comment;version=2;" + - "commentURL=http://anotherurl.com;"; - String c3 = "myCookie3=myValue3;max-age=0;version=2;"; - - Set cookies = CookieDecoder.decode(c1 + c2 + c3); - assertEquals(3, cookies.size()); - Iterator it = cookies.iterator(); - Cookie cookie = it.next(); - assertNotNull(cookie); - assertEquals("myValue", cookie.value()); - assertEquals("this is a comment", cookie.comment()); - assertEquals("http://aurl.com", cookie.commentUrl()); - assertEquals(".adomainsomewhere", cookie.domain()); - assertTrue(cookie.isDiscard()); - assertEquals(50, cookie.maxAge()); - assertEquals("/apathsomewhere", cookie.path()); - assertEquals(2, cookie.ports().size()); - assertTrue(cookie.ports().contains(80)); - assertTrue(cookie.ports().contains(8080)); - assertTrue(cookie.isSecure()); - assertEquals(2, cookie.version()); - cookie = it.next(); - assertNotNull(cookie); - assertEquals("myValue2", cookie.value()); - assertEquals("this is another comment", cookie.comment()); - assertEquals("http://anotherurl.com", cookie.commentUrl()); - assertEquals(".anotherdomainsomewhere", cookie.domain()); - assertFalse(cookie.isDiscard()); - assertEquals(0, cookie.maxAge()); - assertEquals("/anotherpathsomewhere", cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertFalse(cookie.isSecure()); - assertEquals(2, cookie.version()); - cookie = it.next(); - assertNotNull(cookie); - assertEquals("myValue3", cookie.value()); - assertNull(cookie.comment()); - assertNull(cookie.commentUrl()); - assertNull(cookie.domain()); - assertFalse(cookie.isDiscard()); - assertEquals(0, cookie.maxAge()); - assertNull(cookie.path()); - assertTrue(cookie.ports().isEmpty()); - assertFalse(cookie.isSecure()); - assertEquals(2, cookie.version()); - } - - @Test - public void testDecodingClientSideCookies() { - String source = "$Version=\"1\"; " + - "Part_Number=\"Riding_Rocket_0023\"; $Path=\"/acme/ammo\"; " + - "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\""; - - Set cookies = CookieDecoder.decode(source); - Iterator it = cookies.iterator(); - Cookie c; - - c = it.next(); - assertEquals(1, c.version()); - assertEquals("Part_Number", c.name()); - assertEquals("Rocket_Launcher_0001", c.value()); - assertEquals("/acme", c.path()); - assertNull(c.comment()); - assertNull(c.commentUrl()); - assertNull(c.domain()); - assertTrue(c.ports().isEmpty()); - assertEquals(Long.MIN_VALUE, c.maxAge()); - - c = it.next(); - assertEquals(1, c.version()); - assertEquals("Part_Number", c.name()); - assertEquals("Riding_Rocket_0023", c.value()); - assertEquals("/acme/ammo", c.path()); - assertNull(c.comment()); - assertNull(c.commentUrl()); - assertNull(c.domain()); - assertTrue(c.ports().isEmpty()); - assertEquals(Long.MIN_VALUE, c.maxAge()); - - assertFalse(it.hasNext()); - } - - @Test - public void testDecodingCommaSeparatedClientSideCookies() { - String source = - "$Version=\"1\"; session_id=\"1234\", " + - "$Version=\"1\"; session_id=\"1111\"; $Domain=\".cracker.edu\""; - - Set cookies = CookieDecoder.decode(source); - Iterator it = cookies.iterator(); - Cookie c; - - assertTrue(it.hasNext()); - c = it.next(); - assertEquals(1, c.version()); - assertEquals("session_id", c.name()); - assertEquals("1234", c.value()); - assertNull(c.path()); - assertNull(c.comment()); - assertNull(c.commentUrl()); - assertNull(c.domain()); - assertTrue(c.ports().isEmpty()); - assertEquals(Long.MIN_VALUE, c.maxAge()); - - assertTrue(it.hasNext()); - c = it.next(); - assertEquals(1, c.version()); - assertEquals("session_id", c.name()); - assertEquals("1111", c.value()); - assertEquals(".cracker.edu", c.domain()); - assertNull(c.path()); - assertNull(c.comment()); - assertNull(c.commentUrl()); - assertTrue(c.ports().isEmpty()); - assertEquals(Long.MIN_VALUE, c.maxAge()); - - assertFalse(it.hasNext()); - } - - @Test - public void testDecodingQuotedCookie() { - String source = - "a=\"\"," + - "b=\"1\"," + - "c=\"\\\"1\\\"2\\\"\"," + - "d=\"1\\\"2\\\"3\"," + - "e=\"\\\"\\\"\"," + - "f=\"1\\\"\\\"2\"," + - "g=\"\\\\\"," + - "h=\"';,\\x\""; - - Set cookies = CookieDecoder.decode(source); - Iterator it = cookies.iterator(); - Cookie c; - - c = it.next(); - assertEquals("a", c.name()); - assertEquals("", c.value()); - - c = it.next(); - assertEquals("b", c.name()); - assertEquals("1", c.value()); - - c = it.next(); - assertEquals("c", c.name()); - assertEquals("\"1\"2\"", c.value()); - - c = it.next(); - assertEquals("d", c.name()); - assertEquals("1\"2\"3", c.value()); - - c = it.next(); - assertEquals("e", c.name()); - assertEquals("\"\"", c.value()); - - c = it.next(); - assertEquals("f", c.name()); - assertEquals("1\"\"2", c.value()); - - c = it.next(); - assertEquals("g", c.name()); - assertEquals("\\", c.value()); - - c = it.next(); - assertEquals("h", c.name()); - assertEquals("';,\\x", c.value()); - - assertFalse(it.hasNext()); - } - - @Test - public void testDecodingGoogleAnalyticsCookie() { - String source = - "ARPT=LWUKQPSWRTUN04CKKJI; " + - "kw-2E343B92-B097-442c-BFA5-BE371E0325A2=unfinished furniture; " + - "__utma=48461872.1094088325.1258140131.1258140131.1258140131.1; " + - "__utmb=48461872.13.10.1258140131; __utmc=48461872; " + - "__utmz=48461872.1258140131.1.1.utmcsr=overstock.com|utmccn=(referral)|" + - "utmcmd=referral|utmcct=/Home-Garden/Furniture/Clearance,/clearance,/32/dept.html"; - Set cookies = CookieDecoder.decode(source); - Iterator it = cookies.iterator(); - Cookie c; - - c = it.next(); - assertEquals("__utma", c.name()); - assertEquals("48461872.1094088325.1258140131.1258140131.1258140131.1", c.value()); - - c = it.next(); - assertEquals("__utmb", c.name()); - assertEquals("48461872.13.10.1258140131", c.value()); - - c = it.next(); - assertEquals("__utmc", c.name()); - assertEquals("48461872", c.value()); - - c = it.next(); - assertEquals("__utmz", c.name()); - assertEquals("48461872.1258140131.1.1.utmcsr=overstock.com|" + - "utmccn=(referral)|utmcmd=referral|utmcct=/Home-Garden/Furniture/Clearance,/clearance,/32/dept.html", - c.value()); - - c = it.next(); - assertEquals("ARPT", c.name()); - assertEquals("LWUKQPSWRTUN04CKKJI", c.value()); - - c = it.next(); - assertEquals("kw-2E343B92-B097-442c-BFA5-BE371E0325A2", c.name()); - assertEquals("unfinished furniture", c.value()); - - assertFalse(it.hasNext()); - } - - @Test - public void testDecodingLongDates() { - Calendar cookieDate = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - cookieDate.set(9999, Calendar.DECEMBER, 31, 23, 59, 59); - long expectedMaxAge = (cookieDate.getTimeInMillis() - System.currentTimeMillis()) / 1000; - - String source = "Format=EU; expires=Fri, 31-Dec-9999 23:59:59 GMT; path=/"; - - Set cookies = CookieDecoder.decode(source); - - Cookie c = cookies.iterator().next(); - assertTrue(Math.abs(expectedMaxAge - c.maxAge()) < 2); - } - - @Test - public void testDecodingValueWithComma() { - String source = "UserCookie=timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=®ion=BE;" + - " expires=Sat, 01-Dec-2012 10:53:31 GMT; path=/"; - - Set cookies = CookieDecoder.decode(source); - - Cookie c = cookies.iterator().next(); - assertEquals("timeZoneName=(GMT+04:00) Moscow, St. Petersburg, Volgograd&promocode=®ion=BE", c.value()); - } - - @Test - public void testDecodingWeirdNames1() { - String src = "path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com"; - Set cookies = CookieDecoder.decode(src); - Cookie c = cookies.iterator().next(); - assertEquals("path", c.name()); - assertEquals("", c.value()); - assertEquals("/", c.path()); - } - - @Test - public void testDecodingWeirdNames2() { - String src = "HTTPOnly="; - Set cookies = CookieDecoder.decode(src); - Cookie c = cookies.iterator().next(); - assertEquals("HTTPOnly", c.name()); - assertEquals("", c.value()); - } - - @Test - public void testDecodingValuesWithCommasAndEquals() { - String src = "A=v=1&lg=en-US,it-IT,it&intl=it&np=1;T=z=E"; - Set cookies = CookieDecoder.decode(src); - Iterator i = cookies.iterator(); - Cookie c = i.next(); - assertEquals("A", c.name()); - assertEquals("v=1&lg=en-US,it-IT,it&intl=it&np=1", c.value()); - c = i.next(); - assertEquals("T", c.name()); - assertEquals("z=E", c.value()); - } - - @Test - public void testDecodingLongValue() { - String longValue = - "b!!!$Q!!$ha!!!!!!" + - "%=J^wI!!3iD!!!!$=HbQW!!3iF!!!!#=J^wI!!3iH!!!!%=J^wI!!3iM!!!!%=J^wI!!3iS!!!!" + - "#=J^wI!!3iU!!!!%=J^wI!!3iZ!!!!#=J^wI!!3i]!!!!%=J^wI!!3ig!!!!%=J^wI!!3ij!!!!" + - "%=J^wI!!3ik!!!!#=J^wI!!3il!!!!$=HbQW!!3in!!!!%=J^wI!!3ip!!!!$=HbQW!!3iq!!!!" + - "$=HbQW!!3it!!!!%=J^wI!!3ix!!!!#=J^wI!!3j!!!!!$=HbQW!!3j%!!!!$=HbQW!!3j'!!!!" + - "%=J^wI!!3j(!!!!%=J^wI!!9mJ!!!!'=KqtH!!=SE!!M!!!!" + - "'=KqtH!!s1X!!!!$=MMyc!!s1_!!!!#=MN#O!!ypn!!!!'=KqtH!!ypr!!!!'=KqtH!#%h!!!!!" + - "%=KqtH!#%o!!!!!'=KqtH!#)H6!!!!!!'=KqtH!#]9R!!!!$=H/Lt!#]I6!!!!#=KqtH!#]Z#!!!!%=KqtH!#^*N!!!!" + - "#=KqtH!#^:m!!!!#=KqtH!#_*_!!!!%=J^wI!#`-7!!!!#=KqtH!#`T>!!!!'=KqtH!#`T?!!!!" + - "'=KqtH!#`TA!!!!'=KqtH!#`TB!!!!'=KqtH!#`TG!!!!'=KqtH!#`TP!!!!#=KqtH!#`U,!!!!" + - "'=KqtH!#`U/!!!!'=KqtH!#`U0!!!!#=KqtH!#`U9!!!!'=KqtH!#aEQ!!!!%=KqtH!#b<)!!!!" + - "'=KqtH!#c9-!!!!%=KqtH!#dxC!!!!%=KqtH!#dxE!!!!%=KqtH!#ev$!!!!'=KqtH!#fBi!!!!" + - "#=KqtH!#fBj!!!!'=KqtH!#fG)!!!!'=KqtH!#fG+!!!!'=KqtH!#g*B!!!!'=KqtH!$>hD!!!!+=J^x0!$?lW!!!!'=KqtH!$?ll!!!!'=KqtH!$?lm!!!!" + - "%=KqtH!$?mi!!!!'=KqtH!$?mx!!!!'=KqtH!$D7]!!!!#=J_#p!$D@T!!!!#=J_#p!$V cookies = CookieDecoder.decode("bh=\"" + longValue + "\";"); - assertEquals(1, cookies.size()); - Cookie c = cookies.iterator().next(); - assertEquals("bh", c.name()); - assertEquals(longValue, c.value()); - } -} diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java deleted file mode 100644 index bd5149be57..0000000000 --- a/codec-http/src/test/java/io/netty/handler/codec/http/CookieEncoderTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you 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 io.netty.handler.codec.http; - -import static org.junit.Assert.*; - -import java.text.DateFormat; -import java.util.Date; -import java.util.List; - -import org.junit.Test; - -public class CookieEncoderTest { - @Test - public void testEncodingSingleCookieV0() { - String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure"; - DateFormat df = HttpHeaderDateFormat.get(); - Cookie cookie = new DefaultCookie("myCookie", "myValue"); - cookie.setComment("this is a Comment"); - cookie.setCommentUrl("http://aurl.com"); - cookie.setDomain(".adomainsomewhere"); - cookie.setDiscard(true); - cookie.setMaxAge(50); - cookie.setPath("/apathsomewhere"); - cookie.setPorts(80, 8080); - cookie.setSecure(true); - - String encodedCookie = ServerCookieEncoder.encode(cookie); - - long currentTime = System.currentTimeMillis(); - boolean fail = true; - // +/- 10-second tolerance - for (int delta = 0; delta <= 20000; delta += 250) { - if (encodedCookie.equals(result.replace( - "XXX", df.format(new Date(currentTime + 40000 + delta))))) { - fail = false; - break; - } - } - - if (fail) { - fail("Expected: " + result + ", Actual: " + encodedCookie); - } - } - - @Test - public void testEncodingSingleCookieV1() { - String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; " + - "Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1"; - Cookie cookie = new DefaultCookie("myCookie", "myValue"); - cookie.setVersion(1); - cookie.setComment("this is a Comment"); - cookie.setDomain(".adomainsomewhere"); - cookie.setMaxAge(50); - cookie.setPath("/apathsomewhere"); - cookie.setSecure(true); - String encodedCookie = ServerCookieEncoder.encode(cookie); - assertEquals(result, encodedCookie); - } - - @Test - public void testEncodingSingleCookieV2() { - String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; Domain=.adomainsomewhere; " + - "Secure; Comment=\"this is a Comment\"; Version=1; CommentURL=\"http://aurl.com\"; " + - "Port=\"80,8080\"; Discard"; - Cookie cookie = new DefaultCookie("myCookie", "myValue"); - cookie.setVersion(1); - cookie.setComment("this is a Comment"); - cookie.setCommentUrl("http://aurl.com"); - cookie.setDomain(".adomainsomewhere"); - cookie.setDiscard(true); - cookie.setMaxAge(50); - cookie.setPath("/apathsomewhere"); - cookie.setPorts(80, 8080); - cookie.setSecure(true); - String encodedCookie = ServerCookieEncoder.encode(cookie); - assertEquals(result, encodedCookie); - } - - @Test - public void testEncodingMultipleClientCookies() { - String c1 = "$Version=1; myCookie=myValue; $Path=\"/apathsomewhere\"; " + - "$Domain=.adomainsomewhere; $Port=\"80,8080\"; "; - String c2 = "$Version=1; myCookie2=myValue2; $Path=\"/anotherpathsomewhere\"; " + - "$Domain=.anotherdomainsomewhere; "; - String c3 = "$Version=1; myCookie3=myValue3"; - Cookie cookie = new DefaultCookie("myCookie", "myValue"); - cookie.setVersion(1); - cookie.setComment("this is a Comment"); - cookie.setCommentUrl("http://aurl.com"); - cookie.setDomain(".adomainsomewhere"); - cookie.setDiscard(true); - cookie.setMaxAge(50); - cookie.setPath("/apathsomewhere"); - cookie.setPorts(80, 8080); - cookie.setSecure(true); - Cookie cookie2 = new DefaultCookie("myCookie2", "myValue2"); - cookie2.setVersion(1); - cookie2.setComment("this is another Comment"); - cookie2.setCommentUrl("http://anotherurl.com"); - cookie2.setDomain(".anotherdomainsomewhere"); - cookie2.setDiscard(false); - cookie2.setPath("/anotherpathsomewhere"); - cookie2.setSecure(false); - Cookie cookie3 = new DefaultCookie("myCookie3", "myValue3"); - cookie3.setVersion(1); - String encodedCookie = ClientCookieEncoder.encode(cookie, cookie2, cookie3); - assertEquals(c1 + c2 + c3, encodedCookie); - } - - @Test - public void testEncodingWithNoCookies() { - String encodedCookie1 = ClientCookieEncoder.encode(); - List encodedCookie2 = ServerCookieEncoder.encode(); - assertNotNull(encodedCookie1); - assertNotNull(encodedCookie2); - } -} diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieDecoderTest.java similarity index 94% rename from codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoderTest.java rename to codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieDecoderTest.java index d2b9ef34e4..2ebd1b87c7 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieDecoderTest.java @@ -23,14 +23,14 @@ import java.util.Set; import static org.junit.Assert.*; -public class Rfc6265ServerCookieDecoderTest { +public class ServerCookieDecoderTest { @Test public void testDecodingSingleCookie() { String cookieString = "myCookie=myValue"; cookieString = cookieString.replace("XXX", HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000))); - Set cookies = Rfc6265ServerCookieDecoder.decode(cookieString); + Set cookies = ServerCookieDecoder.decode(cookieString); assertEquals(1, cookies.size()); Cookie cookie = cookies.iterator().next(); assertNotNull(cookie); @@ -43,7 +43,7 @@ public class Rfc6265ServerCookieDecoderTest { String c2 = "myCookie2=myValue2;"; String c3 = "myCookie3=myValue3;"; - Set cookies = Rfc6265ServerCookieDecoder.decode(c1 + c2 + c3); + Set cookies = ServerCookieDecoder.decode(c1 + c2 + c3); assertEquals(3, cookies.size()); Iterator it = cookies.iterator(); Cookie cookie = it.next(); @@ -69,7 +69,7 @@ public class Rfc6265ServerCookieDecoderTest { "g=\"\\\\\";" + "h=\"';,\\x\""; - Set cookies = Rfc6265ServerCookieDecoder.decode(source); + Set cookies = ServerCookieDecoder.decode(source); Iterator it = cookies.iterator(); Cookie c; @@ -117,7 +117,7 @@ public class Rfc6265ServerCookieDecoderTest { "__utmb=48461872.13.10.1258140131; __utmc=48461872; " + "__utmz=48461872.1258140131.1.1.utmcsr=overstock.com|utmccn=(referral)|" + "utmcmd=referral|utmcct=/Home-Garden/Furniture/Clearance,/clearance,/32/dept.html"; - Set cookies = Rfc6265ServerCookieDecoder.decode(source); + Set cookies = ServerCookieDecoder.decode(source); Iterator it = cookies.iterator(); Cookie c; @@ -198,7 +198,7 @@ public class Rfc6265ServerCookieDecoderTest { "%=KqtH!$?mi!!!!'=KqtH!$?mx!!!!'=KqtH!$D7]!!!!#=J_#p!$D@T!!!!#=J_#p!$V cookies = Rfc6265ServerCookieDecoder.decode("bh=\"" + longValue + "\";"); + Set cookies = ServerCookieDecoder.decode("bh=\"" + longValue + "\";"); assertEquals(1, cookies.size()); Cookie c = cookies.iterator().next(); assertEquals("bh", c.name()); @@ -211,7 +211,7 @@ public class Rfc6265ServerCookieDecoderTest { "Part_Number1=\"Riding_Rocket_0023\"; $Path=\"/acme/ammo\"; " + "Part_Number2=\"Rocket_Launcher_0001\"; $Path=\"/acme\""; - Set cookies = Rfc6265ServerCookieDecoder.decode(source); + Set cookies = ServerCookieDecoder.decode(source); Iterator it = cookies.iterator(); Cookie c; diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieEncoderTest.java similarity index 83% rename from codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoderTest.java rename to codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieEncoderTest.java index 659f4708a2..bb101555d3 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/Rfc6265ServerCookieEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/ServerCookieEncoderTest.java @@ -15,13 +15,13 @@ */ package io.netty.handler.codec.http; -import static org.junit.Assert.*; +import org.junit.Test; import java.util.List; -import org.junit.Test; +import static org.junit.Assert.*; -public class Rfc6265ServerCookieEncoderTest { +public class ServerCookieEncoderTest { @Test public void testEncodingSingleCookieV0() { String result = "myCookie=myValue; Max-Age=50; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure"; @@ -31,14 +31,14 @@ public class Rfc6265ServerCookieEncoderTest { cookie.setPath("/apathsomewhere"); cookie.setSecure(true); - String encodedCookie = Rfc6265ServerCookieEncoder.encode(cookie); + String encodedCookie = ServerCookieEncoder.encode(cookie); assertEquals(result, encodedCookie); } @Test public void testEncodingWithNoCookies() { - String encodedCookie1 = Rfc6265ClientCookieEncoder.encode(); - List encodedCookie2 = Rfc6265ServerCookieEncoder.encode(); + String encodedCookie1 = ClientCookieEncoder.encode(); + List encodedCookie2 = ServerCookieEncoder.encode(); assertNull(encodedCookie1); assertNotNull(encodedCookie2); assertTrue(encodedCookie2.isEmpty());