Author: markt Date: Wed Oct 1 12:59:00 2014 New Revision: 1628694 URL: http://svn.apache.org/r1628694 Log: Refactor version 0 cookie generation tests to test both CookieProcessor implementations.
Added: tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java (with props) Modified: tomcat/trunk/test/org/apache/tomcat/util/http/TestSetCookieSupport.java Added: tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java?rev=1628694&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java (added) +++ tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java Wed Oct 1 12:59:00 2014 @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 org.apache.tomcat.util.http; + +import javax.servlet.http.Cookie; + +import org.junit.Assert; +import org.junit.Test; + +public class TestCookieProcessorGeneration { + + @Test + public void v0SimpleCookie() { + doTest(new Cookie("foo", "bar"), "foo=bar"); + } + + @Test + public void v0NullValue() { + doTest(new Cookie("foo", null), "foo=\"\"", "foo="); + } + + @Test + public void v0QuotedValue() { + doTest(new Cookie("foo", "\"bar\""), "foo=\"bar\""); + } + + @Test + public void v0ValueContainsSemicolon() { + doTest(new Cookie("foo", "a;b"), "foo=\"a;b\"; Version=1", null); + } + + @Test + public void v0ValueContainsComma() { + doTest(new Cookie("foo", "a,b"), "foo=\"a,b\"; Version=1", null); + } + + @Test + public void v0ValueContainsSpace() { + doTest(new Cookie("foo", "a b"), "foo=\"a b\"; Version=1", null); + } + + @Test + public void v0ValueContainsEquals() { + doTest(new Cookie("foo", "a=b"),"foo=\"a=b\"; Version=1", "foo=a=b"); + } + + @Test + public void v0ValueContainsQuote() { + doTest(new Cookie("foo", "a\"b"),"foo=\"a\\\"b\"; Version=1", null); + } + + @Test + public void v0ValueContainsNonV0Separator() { + doTest(new Cookie("foo", "a()<>@:\\\"/[]?={}b"), + "foo=\"a()<>@:\\\\\\\"/[]?={}b\"; Version=1", null); + } + + @Test + public void v0ValueContainsBackslash() { + doTest(new Cookie("foo", "a\\b"), "foo=\"a\\\\b\"; Version=1", null); + } + + @Test + public void v0ValueContainsBackslashAtEnd() { + doTest(new Cookie("foo", "a\\"), "foo=\"a\\\\\"; Version=1", null); + } + + @Test + public void v0ValueContainsBackslashAndQuote() { + doTest(new Cookie("foo", "a\"b\\c"), "foo=\"a\\\"b\\\\c\"; Version=1", null); + } + + + + private void doTest(Cookie cookie, String expected) { + doTest(cookie, expected, expected); + } + + + private void doTest(Cookie cookie, + String expectedLegacy, String expectedRfc6265) { + doTestDefaults(cookie, expectedLegacy, expectedRfc6265); + doTestAllowSeparators(cookie, expectedLegacy, expectedRfc6265); + } + + + private void doTestDefaults(Cookie cookie, + String expectedLegacy, String expectedRfc6265) { + CookieProcessor legacy = new LegacyCookieProcessor(); + CookieProcessor rfc6265 = new Rfc6265CookieProcessor(); + doTest(cookie, legacy, expectedLegacy, rfc6265, expectedRfc6265); + } + + + private void doTestAllowSeparators(Cookie cookie, + String expectedLegacy, String expectedRfc6265) { + LegacyCookieProcessor legacy = new LegacyCookieProcessor(); + legacy.setAllowHttpSepsInV0(true); + legacy.setForwardSlashIsSeparator(true); + CookieProcessor rfc6265 = new Rfc6265CookieProcessor(); + doTest(cookie, legacy, expectedLegacy, rfc6265, expectedRfc6265); + } + + + private void doTest(Cookie cookie, + CookieProcessor legacy, String expectedLegacy, + CookieProcessor rfc6265, String expectedRfc6265) { + Assert.assertEquals(expectedLegacy, legacy.generateHeader(cookie)); + if (expectedRfc6265 == null) { + IllegalArgumentException e = null; + try { + rfc6265.generateHeader(cookie); + } catch (IllegalArgumentException iae) { + e = iae; + } + Assert.assertNotNull("Failed to throw IAE", e); + } else { + Assert.assertEquals(expectedRfc6265, rfc6265.generateHeader(cookie)); + } + } +} Propchange: tomcat/trunk/test/org/apache/tomcat/util/http/TestCookieProcessorGeneration.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/test/org/apache/tomcat/util/http/TestSetCookieSupport.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/http/TestSetCookieSupport.java?rev=1628694&r1=1628693&r2=1628694&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/http/TestSetCookieSupport.java (original) +++ tomcat/trunk/test/org/apache/tomcat/util/http/TestSetCookieSupport.java Wed Oct 1 12:59:00 2014 @@ -24,89 +24,6 @@ import org.junit.Test; public class TestSetCookieSupport { @Test - public void v0simpleCookie() { - Cookie cookie = new Cookie("foo", "bar"); - Assert.assertEquals("foo=bar", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0NullValue() { - Cookie cookie = new Cookie("foo", null); - // should this throw an IAE? -// Assert.assertEquals("foo=", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"\"", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0QuotedValue() { - Cookie cookie = new Cookie("foo", "\"bar\""); - Assert.assertEquals("foo=\"bar\"", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsSemicolon() { - Cookie cookie = new Cookie("foo", "a;b"); - // should probably throw IAE? - Assert.assertEquals("foo=\"a;b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsComma() { - Cookie cookie = new Cookie("foo", "a,b"); - // should probably throw IAE? - Assert.assertEquals("foo=\"a,b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsSpace() { - Cookie cookie = new Cookie("foo", "a b"); - // should probably throw IAE? - Assert.assertEquals("foo=\"a b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsEquals() { - Cookie cookie = new Cookie("foo", "a=b"); - Assert.assertEquals("foo=\"a=b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsQuote() { - Cookie cookie = new Cookie("foo", "a\"b"); -// Assert.assertEquals("foo=a\"b", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"a\\\"b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsNonV0Separator() { - Cookie cookie = new Cookie("foo", "a()<>@:\\\"/[]?={}b"); - // Assert.assertEquals("foo=a()<>@:\\\"/[]?{}=b", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"a()<>@:\\\\\\\"/[]?={}b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsBackslash() { - Cookie cookie = new Cookie("foo", "a\\b"); -// Assert.assertEquals("foo=a\\b", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"a\\\\b\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - - @Test - public void v0ValueContainsBackslashAtEnd() { - Cookie cookie = new Cookie("foo", "a\\"); -// Assert.assertEquals("foo=a\\", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"a\\\\\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test - public void v0ValueContainsBackslashAndQuote() { - Cookie cookie = new Cookie("foo", "a\"b\\c"); -// Assert.assertEquals("foo=a\"b\\c", SetCookieSupport.generateHeader(cookie)); - Assert.assertEquals("foo=\"a\\\"b\\\\c\"; Version=1", SetCookieSupport.generateHeader(cookie)); - } - - @Test public void v1simpleCookie() { Cookie cookie = new Cookie("foo", "bar"); cookie.setVersion(1); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org