001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java,v 1.2 2004/04/24 23:28:04 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient.cookie;
031:
032: import junit.framework.Test;
033: import junit.framework.TestSuite;
034:
035: import org.apache.commons.httpclient.Cookie;
036: import org.apache.commons.httpclient.Header;
037: import org.apache.commons.httpclient.HttpException;
038: import org.apache.commons.httpclient.NameValuePair;
039:
040: /**
041: * Test cases for Netscape cookie draft
042: *
043: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
044: *
045: * @version $Revision: 480424 $
046: */
047: public class TestCookieNetscapeDraft extends TestCookieBase {
048:
049: // ------------------------------------------------------------ Constructor
050:
051: public TestCookieNetscapeDraft(String name) {
052: super (name);
053: }
054:
055: // ------------------------------------------------------- TestCase Methods
056:
057: public static Test suite() {
058: return new TestSuite(TestCookieNetscapeDraft.class);
059: }
060:
061: public void testParseAttributeInvalidAttrib() throws Exception {
062: CookieSpec cookiespec = new NetscapeDraftSpec();
063: try {
064: cookiespec.parseAttribute(null, null);
065: fail("IllegalArgumentException must have been thrown");
066: } catch (IllegalArgumentException expected) {
067: }
068: }
069:
070: public void testParseAttributeInvalidCookie() throws Exception {
071: CookieSpec cookiespec = new NetscapeDraftSpec();
072: try {
073: cookiespec.parseAttribute(
074: new NameValuePair("name", "value"), null);
075: fail("IllegalArgumentException must have been thrown");
076: } catch (IllegalArgumentException expected) {
077: }
078: }
079:
080: public void testParseAttributeInvalidCookieExpires()
081: throws Exception {
082: CookieSpec cookiespec = new NetscapeDraftSpec();
083: Cookie cookie = new Cookie();
084: try {
085: cookiespec.parseAttribute(
086: new NameValuePair("expires", null), cookie);
087: fail("MalformedCookieException must have been thrown");
088: } catch (MalformedCookieException expected) {
089: }
090: }
091:
092: public void testParseWithNullHost() throws Exception {
093: Header header = new Header("Set-Cookie",
094: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
095:
096: CookieSpec cookiespec = new NetscapeDraftSpec();
097: try {
098: Cookie[] parsed = cookieParse(cookiespec, null, 80, "/",
099: false, header);
100: fail("IllegalArgumentException should have been thrown");
101: } catch (IllegalArgumentException e) {
102: // expected
103: }
104: }
105:
106: public void testParseWithBlankHost() throws Exception {
107: Header header = new Header("Set-Cookie",
108: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
109:
110: CookieSpec cookiespec = new NetscapeDraftSpec();
111: try {
112: Cookie[] parsed = cookieParse(cookiespec, " ", 80, "/",
113: false, header);
114: fail("IllegalArgumentException should have been thrown");
115: } catch (IllegalArgumentException e) {
116: // expected
117: }
118: }
119:
120: public void testParseWithNullPath() throws Exception {
121: Header header = new Header("Set-Cookie",
122: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
123:
124: CookieSpec cookiespec = new NetscapeDraftSpec();
125: try {
126: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
127: null, false, header);
128: fail("IllegalArgumentException should have been thrown");
129: } catch (IllegalArgumentException e) {
130: // expected
131: }
132: }
133:
134: public void testParseWithBlankPath() throws Exception {
135: Header header = new Header("Set-Cookie",
136: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
137:
138: CookieSpec cookiespec = new NetscapeDraftSpec();
139: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
140: " ", false, header);
141: assertNotNull(parsed);
142: assertEquals(1, parsed.length);
143: assertEquals("/", parsed[0].getPath());
144: }
145:
146: public void testParseWithNegativePort() throws Exception {
147: Header header = new Header("Set-Cookie",
148: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
149:
150: CookieSpec cookiespec = new NetscapeDraftSpec();
151: try {
152: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80,
153: null, false, header);
154: fail("IllegalArgumentException should have been thrown");
155: } catch (IllegalArgumentException e) {
156: // expected
157: }
158: }
159:
160: public void testParseWithInvalidHeader1() throws Exception {
161: CookieSpec cookiespec = new NetscapeDraftSpec();
162: try {
163: Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo",
164: false, (String) null);
165: fail("IllegalArgumentException should have been thrown.");
166: } catch (IllegalArgumentException e) {
167: // expected
168: }
169: }
170:
171: public void testParseAbsPath() throws Exception {
172: Header header = new Header("Set-Cookie",
173: "name1=value1;Path=/path/");
174:
175: CookieSpec cookiespec = new NetscapeDraftSpec();
176: Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/path/",
177: true, header);
178: assertEquals("Found 1 cookies.", 1, parsed.length);
179: assertEquals("Name", "name1", parsed[0].getName());
180: assertEquals("Value", "value1", parsed[0].getValue());
181: assertEquals("Domain", "host", parsed[0].getDomain());
182: assertEquals("Path", "/path/", parsed[0].getPath());
183: }
184:
185: public void testParseAbsPath2() throws Exception {
186: Header header = new Header("Set-Cookie", "name1=value1;Path=/");
187:
188: CookieSpec cookiespec = new NetscapeDraftSpec();
189: Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/",
190: true, header);
191: assertEquals("Found 1 cookies.", 1, parsed.length);
192: assertEquals("Name", "name1", parsed[0].getName());
193: assertEquals("Value", "value1", parsed[0].getValue());
194: assertEquals("Domain", "host", parsed[0].getDomain());
195: assertEquals("Path", "/", parsed[0].getPath());
196: }
197:
198: public void testParseRelativePath() throws Exception {
199: Header header = new Header("Set-Cookie",
200: "name1=value1;Path=whatever");
201:
202: CookieSpec cookiespec = new NetscapeDraftSpec();
203: Cookie[] parsed = cookieParse(cookiespec, "host", 80,
204: "whatever", true, header);
205: assertEquals("Found 1 cookies.", 1, parsed.length);
206: assertEquals("Name", "name1", parsed[0].getName());
207: assertEquals("Value", "value1", parsed[0].getValue());
208: assertEquals("Domain", "host", parsed[0].getDomain());
209: assertEquals("Path", "whatever", parsed[0].getPath());
210: }
211:
212: public void testParseWithIllegalNetscapeDomain1() throws Exception {
213: Header header = new Header("Set-Cookie",
214: "cookie-name=cookie-value; domain=.com");
215:
216: CookieSpec cookiespec = new NetscapeDraftSpec();
217: try {
218: Cookie[] parsed = cookieParse(cookiespec, "a.com", 80, "/",
219: false, header);
220: fail("HttpException exception should have been thrown");
221: } catch (HttpException e) {
222: // expected
223: }
224: }
225:
226: public void testParseWithWrongNetscapeDomain2() throws Exception {
227: Header header = new Header("Set-Cookie",
228: "cookie-name=cookie-value; domain=.y.z");
229:
230: CookieSpec cookiespec = new NetscapeDraftSpec();
231: try {
232: Cookie[] parsed = cookieParse(cookiespec, "x.y.z", 80, "/",
233: false, header);
234: fail("HttpException exception should have been thrown");
235: } catch (HttpException e) {
236: // expected
237: }
238: }
239:
240: /**
241: * Tests Netscape specific cookie formatting.
242: */
243:
244: public void testNetscapeCookieFormatting() throws Exception {
245: Header header = new Header("Set-Cookie",
246: "name=value; path=/; domain=.mydomain.com");
247: CookieSpec cookiespec = new NetscapeDraftSpec();
248: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
249: "/", false, header);
250: cookiespec.validate("myhost.mydomain.com", 80, "/", false,
251: cookies[0]);
252: String s = cookiespec.formatCookie(cookies[0]);
253: assertEquals("name=value", s);
254: }
255:
256: /**
257: * Tests Netscape specific expire attribute parsing.
258: */
259: public void testNetscapeCookieExpireAttribute() throws Exception {
260: CookieSpec cookiespec = new NetscapeDraftSpec();
261: Header header = new Header(
262: "Set-Cookie",
263: "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
264: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
265: "/", false, header);
266: cookiespec.validate("myhost.mydomain.com", 80, "/", false,
267: cookies[0]);
268: header = new Header(
269: "Set-Cookie",
270: "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
271: try {
272: cookies = cookiespec.parse("myhost.mydomain.com", 80, "/",
273: false, header);
274: cookiespec.validate("myhost.mydomain.com", 80, "/", false,
275: cookies[0]);
276: fail("MalformedCookieException must have been thrown");
277: } catch (MalformedCookieException expected) {
278: }
279: }
280:
281: /**
282: * Tests Netscape specific expire attribute without a time zone.
283: */
284: public void testNetscapeCookieExpireAttributeNoTimeZone()
285: throws Exception {
286: CookieSpec cookiespec = new NetscapeDraftSpec();
287: Header header = new Header("Set-Cookie",
288: "name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
289: try {
290: cookiespec.parse("myhost.mydomain.com", 80, "/", false,
291: header);
292: fail("MalformedCookieException should have been thrown");
293: } catch (MalformedCookieException ex) {
294: // expected
295: }
296: }
297:
298: /**
299: * Tests if cookie values with embedded comma are handled correctly.
300: */
301: public void testCookieWithComma() throws Exception {
302: Header header = new Header("Set-Cookie", "a=b,c");
303:
304: CookieSpec cookiespec = new NetscapeDraftSpec();
305: Cookie[] cookies = cookiespec.parse("localhost", 80, "/",
306: false, header);
307: assertEquals("number of cookies", 1, cookies.length);
308: assertEquals("a", cookies[0].getName());
309: assertEquals("b,c", cookies[0].getValue());
310: }
311:
312: }
|