Source Code Cross Referenced for TestCookieRFC2109Spec.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » cookie » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache common HttpClient » org.apache.commons.httpclient.cookie 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java,v 1.3 2004/06/05 16:49:20 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.NameValuePair;
038:
039:        /**
040:         * Test cases for RFC2109 cookie spec
041:         *
042:         * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
043:         * 
044:         * @version $Revision: 480424 $
045:         */
046:        public class TestCookieRFC2109Spec extends TestCookieBase {
047:
048:            // ------------------------------------------------------------ Constructor
049:
050:            public TestCookieRFC2109Spec(String name) {
051:                super (name);
052:            }
053:
054:            // ------------------------------------------------------- TestCase Methods
055:
056:            public static Test suite() {
057:                return new TestSuite(TestCookieRFC2109Spec.class);
058:            }
059:
060:            public void testParseAttributeInvalidAttrib() throws Exception {
061:                CookieSpec cookiespec = new RFC2109Spec();
062:                try {
063:                    cookiespec.parseAttribute(null, null);
064:                    fail("IllegalArgumentException must have been thrown");
065:                } catch (IllegalArgumentException expected) {
066:                }
067:            }
068:
069:            public void testParseAttributeInvalidCookie() throws Exception {
070:                CookieSpec cookiespec = new RFC2109Spec();
071:                try {
072:                    cookiespec.parseAttribute(
073:                            new NameValuePair("name", "value"), null);
074:                    fail("IllegalArgumentException must have been thrown");
075:                } catch (IllegalArgumentException expected) {
076:                }
077:            }
078:
079:            public void testParseAttributeNullPath() throws Exception {
080:                CookieSpec cookiespec = new RFC2109Spec();
081:                try {
082:                    Cookie cookie = new Cookie();
083:                    cookiespec.parseAttribute(new NameValuePair("path", null),
084:                            cookie);
085:                    fail("MalformedCookieException must have been thrown");
086:                } catch (MalformedCookieException expected) {
087:                }
088:            }
089:
090:            public void testParseAttributeBlankPath() throws Exception {
091:                CookieSpec cookiespec = new RFC2109Spec();
092:                try {
093:                    Cookie cookie = new Cookie();
094:                    cookiespec.parseAttribute(new NameValuePair("path", "   "),
095:                            cookie);
096:                    fail("MalformedCookieException must have been thrown");
097:                } catch (MalformedCookieException expected) {
098:                }
099:            }
100:
101:            public void testParseAttributeNullVersion() throws Exception {
102:                CookieSpec cookiespec = new RFC2109Spec();
103:                try {
104:                    Cookie cookie = new Cookie();
105:                    cookiespec.parseAttribute(
106:                            new NameValuePair("version", null), cookie);
107:                    fail("MalformedCookieException must have been thrown");
108:                } catch (MalformedCookieException expected) {
109:                }
110:            }
111:
112:            public void testParseAttributeInvalidVersion() throws Exception {
113:                CookieSpec cookiespec = new RFC2109Spec();
114:                try {
115:                    Cookie cookie = new Cookie();
116:                    cookiespec.parseAttribute(new NameValuePair("version",
117:                            "nonsense"), cookie);
118:                    fail("MalformedCookieException must have been thrown");
119:                } catch (MalformedCookieException expected) {
120:                }
121:            }
122:
123:            public void testParseVersion() throws Exception {
124:                Header header = new Header("Set-Cookie",
125:                        "cookie-name=cookie-value; version=1");
126:
127:                CookieSpec cookiespec = new RFC2109Spec();
128:                Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
129:                        false, header);
130:                assertEquals("Found 1 cookie.", 1, parsed.length);
131:                assertEquals("Name", "cookie-name", parsed[0].getName());
132:                assertEquals("Value", "cookie-value", parsed[0].getValue());
133:                assertEquals("Version", 1, parsed[0].getVersion());
134:            }
135:
136:            /**
137:             * Test domain equals host 
138:             */
139:            public void testParseDomainEqualsHost() throws Exception {
140:                Header header = new Header("Set-Cookie",
141:                        "cookie-name=cookie-value; domain=www.b.com; version=1");
142:
143:                CookieSpec cookiespec = new RFC2109Spec();
144:                Cookie[] parsed = cookieParse(cookiespec, "www.b.com", 80, "/",
145:                        false, header);
146:                assertNotNull(parsed);
147:                assertEquals(1, parsed.length);
148:                assertEquals("www.b.com", parsed[0].getDomain());
149:            }
150:
151:            /**
152:             * Domain does not start with a dot
153:             */
154:            public void testParseWithIllegalDomain1() throws Exception {
155:                Header header = new Header("Set-Cookie",
156:                        "cookie-name=cookie-value; domain=a.b.com; version=1");
157:
158:                CookieSpec cookiespec = new RFC2109Spec();
159:                try {
160:                    Cookie[] parsed = cookieParse(cookiespec, "www.a.b.com",
161:                            80, "/", false, header);
162:                    fail("MalformedCookieException should have been thrown");
163:                } catch (MalformedCookieException e) {
164:                    // expected
165:                }
166:            }
167:
168:            /**
169:             * Domain must have alt least one embedded dot
170:             */
171:            public void testParseWithIllegalDomain2() throws Exception {
172:                Header header = new Header("Set-Cookie",
173:                        "cookie-name=cookie-value; domain=.com; version=1");
174:
175:                CookieSpec cookiespec = new RFC2109Spec();
176:                try {
177:                    Cookie[] parsed = cookieParse(cookiespec, "b.com", 80, "/",
178:                            false, header);
179:                    fail("MalformedCookieException should have been thrown");
180:                } catch (MalformedCookieException e) {
181:                    // expected
182:                }
183:            }
184:
185:            /**
186:             * Domain must have alt least one embedded dot
187:             */
188:            public void testParseWithIllegalDomain3() throws Exception {
189:                Header header = new Header("Set-Cookie",
190:                        "cookie-name=cookie-value; domain=.com.; version=1");
191:
192:                CookieSpec cookiespec = new RFC2109Spec();
193:                try {
194:                    Cookie[] parsed = cookieParse(cookiespec, "b.com", 80, "/",
195:                            false, header);
196:                    fail("HttpException exception should have been thrown");
197:                } catch (MalformedCookieException e) {
198:                    // expected
199:                }
200:            }
201:
202:            /**
203:             * Host minus domain may not contain any dots
204:             */
205:            public void testParseWithIllegalDomain4() throws Exception {
206:                Header header = new Header("Set-Cookie",
207:                        "cookie-name=cookie-value; domain=.c.com; version=1");
208:
209:                CookieSpec cookiespec = new RFC2109Spec();
210:                try {
211:                    Cookie[] parsed = cookieParse(cookiespec, "a.b.c.com", 80,
212:                            "/", false, header);
213:                    fail("MalformedCookieException should have been thrown");
214:                } catch (MalformedCookieException e) {
215:                    // expected
216:                }
217:            }
218:
219:            /**
220:             * Tests if that invalid second domain level cookie gets 
221:             * rejected in the strict mode, but gets accepted in the
222:             * browser compatibility mode.
223:             */
224:            public void testSecondDomainLevelCookie() throws Exception {
225:                Cookie cookie = new Cookie(".sourceforge.net", "name", null,
226:                        "/", null, false);
227:                cookie.setDomainAttributeSpecified(true);
228:                cookie.setPathAttributeSpecified(true);
229:
230:                CookieSpec cookiespec = new RFC2109Spec();
231:                try {
232:                    cookiespec.validate("sourceforge.net", 80, "/", false,
233:                            cookie);
234:                    fail("MalformedCookieException should have been thrown");
235:                } catch (MalformedCookieException e) {
236:                    // Expected
237:                }
238:            }
239:
240:            public void testSecondDomainLevelCookieMatch() throws Exception {
241:                Cookie cookie = new Cookie(".sourceforge.net", "name", null,
242:                        "/", null, false);
243:                cookie.setDomainAttributeSpecified(true);
244:                cookie.setPathAttributeSpecified(true);
245:
246:                CookieSpec cookiespec = new RFC2109Spec();
247:                assertFalse(cookiespec.match("sourceforge.net", 80, "/", false,
248:                        cookie));
249:            }
250:
251:            public void testParseWithWrongPath() throws Exception {
252:                Header header = new Header("Set-Cookie",
253:                        "cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root");
254:
255:                CookieSpec cookiespec = new RFC2109Spec();
256:                try {
257:                    Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
258:                            "/", false, header);
259:                    fail("HttpException exception should have been thrown");
260:                } catch (MalformedCookieException e) {
261:                    // expected
262:                }
263:            }
264:
265:            /**
266:             * Tests if cookie constructor rejects cookie name containing blanks.
267:             */
268:            public void testCookieNameWithBlanks() throws Exception {
269:                Header setcookie = new Header("Set-Cookie", "invalid name=");
270:                CookieSpec cookiespec = new RFC2109Spec();
271:                try {
272:                    Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
273:                            "/", false, setcookie);
274:                    fail("MalformedCookieException exception should have been thrown");
275:                } catch (MalformedCookieException e) {
276:                    // expected
277:                }
278:            }
279:
280:            /**
281:             * Tests if cookie constructor rejects cookie name starting with $.
282:             */
283:            public void testCookieNameStartingWithDollarSign() throws Exception {
284:                Header setcookie = new Header("Set-Cookie", "$invalid_name=");
285:                CookieSpec cookiespec = new RFC2109Spec();
286:                try {
287:                    Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
288:                            "/", false, setcookie);
289:                    fail("MalformedCookieException exception should have been thrown");
290:                } catch (MalformedCookieException e) {
291:                    // expected
292:                }
293:            }
294:
295:            /**
296:             * Tests if default cookie validator rejects cookies originating from a host without domain
297:             * where domain attribute does not match the host of origin 
298:             */
299:            public void testInvalidDomainWithSimpleHostName() throws Exception {
300:                CookieSpec cookiespec = new RFC2109Spec();
301:                Header header = new Header("Set-Cookie",
302:                        "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
303:                Cookie[] cookies = cookiespec.parse("host", 80, "/", false,
304:                        header);
305:                try {
306:                    cookiespec.validate("host", 80, "/", false, cookies[0]);
307:                    fail("MalformedCookieException must have thrown");
308:                } catch (MalformedCookieException expected) {
309:                }
310:                header = new Header("Set-Cookie",
311:                        "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\"");
312:                cookies = cookiespec.parse("host2", 80, "/", false, header);
313:                try {
314:                    cookiespec.validate("host2", 80, "/", false, cookies[0]);
315:                    fail("MalformedCookieException must have thrown");
316:                } catch (MalformedCookieException expected) {
317:                }
318:            }
319:
320:            /**
321:             * Tests if cookie values with embedded comma are handled correctly.
322:             */
323:            public void testCookieWithComma() throws Exception {
324:                Header header = new Header("Set-Cookie", "a=b,c");
325:
326:                CookieSpec cookiespec = new RFC2109Spec();
327:                Cookie[] cookies = cookiespec.parse("localhost", 80, "/",
328:                        false, header);
329:                assertEquals("number of cookies", 2, cookies.length);
330:                assertEquals("a", cookies[0].getName());
331:                assertEquals("b", cookies[0].getValue());
332:                assertEquals("c", cookies[1].getName());
333:                assertEquals(null, cookies[1].getValue());
334:            }
335:
336:            public void testFormatInvalidCookies() throws Exception {
337:                CookieSpec cookiespec = new RFC2109Spec();
338:                try {
339:                    String s = cookiespec.formatCookie(null);
340:                    fail("IllegalArgumentException nust have been thrown");
341:                } catch (IllegalArgumentException expected) {
342:                }
343:            }
344:
345:            /**
346:             * Tests RFC 2109 compiant cookie formatting.
347:             */
348:            public void testRFC2109CookieFormatting() throws Exception {
349:                CookieSpec cookiespec = new RFC2109Spec();
350:                Header header = new Header("Set-Cookie",
351:                        "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
352:                Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
353:                        "/", false, header);
354:                cookiespec.validate("myhost.mydomain.com", 80, "/", false,
355:                        cookies[0]);
356:                String s1 = cookiespec.formatCookie(cookies[0]);
357:                assertEquals(s1,
358:                        "$Version=\"1\"; name=\"value\"; $Path=\"/\"; $Domain=\".mydomain.com\"");
359:
360:                header = new Header("Set-Cookie",
361:                        "name=value; path=/; domain=.mydomain.com");
362:                cookies = cookiespec.parse("myhost.mydomain.com", 80, "/",
363:                        false, header);
364:                cookiespec.validate("myhost.mydomain.com", 80, "/", false,
365:                        cookies[0]);
366:                String s2 = cookiespec.formatCookie(cookies[0]);
367:                assertEquals(s2,
368:                        "$Version=0; name=value; $Path=/; $Domain=.mydomain.com");
369:            }
370:
371:            public void testRFC2109CookiesFormatting() throws Exception {
372:                CookieSpec cookiespec = new RFC2109Spec();
373:                Header header = new Header(
374:                        "Set-Cookie",
375:                        "name1=value1; path=/; domain=.mydomain.com, "
376:                                + "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
377:                Cookie[] cookies = cookieParse(cookiespec,
378:                        "myhost.mydomain.com", 80, "/", false, header);
379:                assertNotNull(cookies);
380:                assertEquals(2, cookies.length);
381:                String s1 = cookiespec.formatCookies(cookies);
382:                assertEquals(
383:                        s1,
384:                        "$Version=0; name1=value1; $Path=/; $Domain=.mydomain.com; "
385:                                + "name2=value2; $Path=/; $Domain=.mydomain.com");
386:
387:                header = new Header(
388:                        "Set-Cookie",
389:                        "name1=value1; version=1; path=/; domain=.mydomain.com, "
390:                                + "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
391:                cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80,
392:                        "/", false, header);
393:                assertNotNull(cookies);
394:                assertEquals(2, cookies.length);
395:                String s2 = cookiespec.formatCookies(cookies);
396:                assertEquals(
397:                        s2,
398:                        "$Version=\"1\"; name1=\"value1\"; $Path=\"/\"; $Domain=\".mydomain.com\"; "
399:                                + "name2=\"value2\"; $Path=\"/\"; $Domain=\".mydomain.com\"");
400:            }
401:
402:            /**
403:             * Tests if null cookie values are handled correctly.
404:             */
405:            public void testNullCookieValueFormatting() {
406:                Cookie cookie = new Cookie(".whatever.com", "name", null, "/",
407:                        null, false);
408:                cookie.setDomainAttributeSpecified(true);
409:                cookie.setPathAttributeSpecified(true);
410:
411:                CookieSpec cookiespec = new RFC2109Spec();
412:                String s = cookiespec.formatCookie(cookie);
413:                assertEquals(
414:                        "$Version=0; name=; $Path=/; $Domain=.whatever.com", s);
415:
416:                cookie.setVersion(1);
417:                s = cookiespec.formatCookie(cookie);
418:                assertEquals(
419:                        "$Version=\"1\"; name=\"\"; $Path=\"/\"; $Domain=\".whatever.com\"",
420:                        s);
421:            }
422:
423:            public void testCookieNullDomainNullPathFormatting() {
424:                Cookie cookie = new Cookie(null, "name", null, "/", null, false);
425:                cookie.setDomainAttributeSpecified(true);
426:                cookie.setPathAttributeSpecified(true);
427:
428:                CookieSpec cookiespec = new RFC2109Spec();
429:                String s = cookiespec.formatCookie(cookie);
430:                assertEquals("$Version=0; name=; $Path=/", s);
431:
432:                cookie.setDomainAttributeSpecified(false);
433:                cookie.setPathAttributeSpecified(false);
434:                s = cookiespec.formatCookie(cookie);
435:                assertEquals("$Version=0; name=", s);
436:            }
437:
438:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.