1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.apache.commons.httpclient.cookie;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.apache.commons.httpclient.Cookie;
35 import org.apache.commons.httpclient.Header;
36 import org.apache.commons.httpclient.HttpException;
37 import org.apache.commons.httpclient.NameValuePair;
38
39
40 /***
41 * Test cases for Netscape cookie draft
42 *
43 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
44 *
45 * @version $Revision: 327789 $
46 */
47 public class TestCookieNetscapeDraft extends TestCookieBase {
48
49
50
51 public TestCookieNetscapeDraft(String name) {
52 super(name);
53 }
54
55
56
57
58
59 public static Test suite() {
60 return new TestSuite(TestCookieNetscapeDraft.class);
61 }
62
63 public void testParseAttributeInvalidAttrib() throws Exception {
64 CookieSpec cookiespec = new NetscapeDraftSpec();
65 try {
66 cookiespec.parseAttribute(null, null);
67 fail("IllegalArgumentException must have been thrown");
68 } catch (IllegalArgumentException expected) {
69 }
70 }
71
72 public void testParseAttributeInvalidCookie() throws Exception {
73 CookieSpec cookiespec = new NetscapeDraftSpec();
74 try {
75 cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
76 fail("IllegalArgumentException must have been thrown");
77 } catch (IllegalArgumentException expected) {
78 }
79 }
80
81 public void testParseAttributeInvalidCookieExpires() throws Exception {
82 CookieSpec cookiespec = new NetscapeDraftSpec();
83 Cookie cookie = new Cookie();
84 try {
85 cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
86 fail("MalformedCookieException must have been thrown");
87 } catch (MalformedCookieException expected) {
88 }
89 }
90
91 public void testParseWithNullHost() throws Exception {
92 Header header = new Header("Set-Cookie",
93 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
94
95 CookieSpec cookiespec = new NetscapeDraftSpec();
96 try {
97 Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
98 fail("IllegalArgumentException should have been thrown");
99 } catch (IllegalArgumentException e) {
100
101 }
102 }
103
104 public void testParseWithBlankHost() throws Exception {
105 Header header = new Header("Set-Cookie",
106 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
107
108 CookieSpec cookiespec = new NetscapeDraftSpec();
109 try {
110 Cookie[] parsed = cookieParse(cookiespec, " ", 80, "/", false, header);
111 fail("IllegalArgumentException should have been thrown");
112 } catch (IllegalArgumentException e) {
113
114 }
115 }
116
117 public void testParseWithNullPath() throws Exception {
118 Header header = new Header("Set-Cookie",
119 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
120
121 CookieSpec cookiespec = new NetscapeDraftSpec();
122 try {
123 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
124 fail("IllegalArgumentException should have been thrown");
125 } catch (IllegalArgumentException e) {
126
127 }
128 }
129
130 public void testParseWithBlankPath() throws Exception {
131 Header header = new Header("Set-Cookie",
132 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
133
134 CookieSpec cookiespec = new NetscapeDraftSpec();
135 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, " ", false, header);
136 assertNotNull(parsed);
137 assertEquals(1, parsed.length);
138 assertEquals("/", parsed[0].getPath());
139 }
140
141 public void testParseWithNegativePort() throws Exception {
142 Header header = new Header("Set-Cookie",
143 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
144
145 CookieSpec cookiespec = new NetscapeDraftSpec();
146 try {
147 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
148 fail("IllegalArgumentException should have been thrown");
149 } catch (IllegalArgumentException e) {
150
151 }
152 }
153
154 public void testParseWithInvalidHeader1() throws Exception {
155 CookieSpec cookiespec = new NetscapeDraftSpec();
156 try {
157 Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
158 fail("IllegalArgumentException should have been thrown.");
159 } catch (IllegalArgumentException e) {
160
161 }
162 }
163
164 public void testParseAbsPath() throws Exception {
165 Header header = new Header("Set-Cookie", "name1=value1;Path=/path/");
166
167 CookieSpec cookiespec = new NetscapeDraftSpec();
168 Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/path/", true, header);
169 assertEquals("Found 1 cookies.",1,parsed.length);
170 assertEquals("Name","name1",parsed[0].getName());
171 assertEquals("Value","value1",parsed[0].getValue());
172 assertEquals("Domain","host",parsed[0].getDomain());
173 assertEquals("Path","/path/",parsed[0].getPath());
174 }
175
176 public void testParseAbsPath2() throws Exception {
177 Header header = new Header("Set-Cookie", "name1=value1;Path=/");
178
179 CookieSpec cookiespec = new NetscapeDraftSpec();
180 Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/", true, header);
181 assertEquals("Found 1 cookies.",1,parsed.length);
182 assertEquals("Name","name1",parsed[0].getName());
183 assertEquals("Value","value1",parsed[0].getValue());
184 assertEquals("Domain","host",parsed[0].getDomain());
185 assertEquals("Path","/",parsed[0].getPath());
186 }
187
188 public void testParseRelativePath() throws Exception {
189 Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
190
191 CookieSpec cookiespec = new NetscapeDraftSpec();
192 Cookie[] parsed = cookieParse(cookiespec, "host", 80, "whatever", true, header);
193 assertEquals("Found 1 cookies.",1,parsed.length);
194 assertEquals("Name","name1",parsed[0].getName());
195 assertEquals("Value","value1",parsed[0].getValue());
196 assertEquals("Domain","host",parsed[0].getDomain());
197 assertEquals("Path","whatever",parsed[0].getPath());
198 }
199
200 public void testParseWithIllegalNetscapeDomain1() throws Exception {
201 Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com");
202
203 CookieSpec cookiespec = new NetscapeDraftSpec();
204 try {
205 Cookie[] parsed = cookieParse(cookiespec, "a.com", 80, "/", false, header);
206 fail("HttpException exception should have been thrown");
207 } catch (HttpException e) {
208
209 }
210 }
211
212 public void testParseWithWrongNetscapeDomain2() throws Exception {
213 Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
214
215 CookieSpec cookiespec = new NetscapeDraftSpec();
216 try {
217 Cookie[] parsed = cookieParse(cookiespec, "x.y.z", 80, "/", false, header);
218 fail("HttpException exception should have been thrown");
219 } catch (HttpException e) {
220
221 }
222 }
223
224 /***
225 * Tests Netscape specific cookie formatting.
226 */
227
228 public void testNetscapeCookieFormatting() throws Exception {
229 Header header = new Header(
230 "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
231 CookieSpec cookiespec = new NetscapeDraftSpec();
232 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
233 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
234 String s = cookiespec.formatCookie(cookies[0]);
235 assertEquals("name=value", s);
236 }
237
238 /***
239 * Tests Netscape specific expire attribute parsing.
240 */
241 public void testNetscapeCookieExpireAttribute() throws Exception {
242 CookieSpec cookiespec = new NetscapeDraftSpec();
243 Header header = new Header("Set-Cookie",
244 "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
245 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
246 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
247 header = new Header("Set-Cookie",
248 "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
249 try {
250 cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
251 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
252 fail("MalformedCookieException must have been thrown");
253 }
254 catch (MalformedCookieException expected) {
255 }
256 }
257
258 /***
259 * Tests Netscape specific expire attribute without a time zone.
260 */
261 public void testNetscapeCookieExpireAttributeNoTimeZone() throws Exception {
262 CookieSpec cookiespec = new NetscapeDraftSpec();
263 Header header = new Header("Set-Cookie",
264 "name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
265 try {
266 cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
267 fail("MalformedCookieException should have been thrown");
268 } catch (MalformedCookieException ex) {
269
270 }
271 }
272
273 /***
274 * Tests if cookie values with embedded comma are handled correctly.
275 */
276 public void testCookieWithComma() throws Exception {
277 Header header = new Header("Set-Cookie", "a=b,c");
278
279 CookieSpec cookiespec = new NetscapeDraftSpec();
280 Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
281 assertEquals("number of cookies", 1, cookies.length);
282 assertEquals("a", cookies[0].getName());
283 assertEquals("b,c", cookies[0].getValue());
284 }
285
286 }
287