001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.control;
020:
021: import java.net.URL;
022:
023: import org.apache.commons.httpclient.cookie.CookiePolicy;
024:
025: import org.apache.jmeter.junit.JMeterTestCase;
026: import org.apache.jmeter.protocol.http.sampler.HTTPNullSampler;
027: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
028: import org.apache.jmeter.threads.JMeterContext;
029: import org.apache.jmeter.threads.JMeterContextService;
030:
031: public class TestCookieManager extends JMeterTestCase {
032: private CookieManager man = null;
033:
034: public TestCookieManager(String name) {
035: super (name);
036: }
037:
038: private JMeterContext jmctx = null;
039:
040: public void setUp() throws Exception {
041: super .setUp();
042: jmctx = JMeterContextService.getContext();
043: man = new CookieManager();
044: man.setThreadContext(jmctx);
045: }
046:
047: public void testRemoveCookie() throws Exception {
048: man.setThreadContext(jmctx);
049: Cookie c = new Cookie("id", "me", "127.0.0.1", "/", false, 0);
050: man.add(c);
051: assertEquals(1, man.getCookieCount());
052: // This should be ignored, as there is no value
053: Cookie d = new Cookie("id", "", "127.0.0.1", "/", false, 0);
054: man.add(d);
055: assertEquals(0, man.getCookieCount());
056: man.add(c);
057: man.add(c);
058: assertEquals(1, man.getCookieCount());
059: Cookie e = new Cookie("id", "me2", "127.0.0.1", "/", false, 0);
060: man.add(e);
061: assertEquals(1, man.getCookieCount());
062: }
063:
064: public void testSendCookie() throws Exception {
065: man.add(new Cookie("id", "value", "jakarta.apache.org", "/",
066: false, 9999999999L));
067: HTTPSamplerBase sampler = new HTTPNullSampler();
068: sampler.setDomain("jakarta.apache.org");
069: sampler.setPath("/index.html");
070: sampler.setMethod(HTTPSamplerBase.GET);
071: assertNotNull(man.getCookieHeaderForURL(sampler.getUrl()));
072: }
073:
074: public void testSendCookie2() throws Exception {
075: man.add(new Cookie("id", "value", ".apache.org", "/", false,
076: 9999999999L));
077: HTTPSamplerBase sampler = new HTTPNullSampler();
078: sampler.setDomain("jakarta.apache.org");
079: sampler.setPath("/index.html");
080: sampler.setMethod(HTTPSamplerBase.GET);
081: assertNotNull(man.getCookieHeaderForURL(sampler.getUrl()));
082: }
083:
084: /**
085: * Test that the cookie domain field is actually handled as browsers do
086: * (i.e.: host X matches domain .X):
087: */
088: public void testDomainHandling() throws Exception {
089: URL url = new URL("http://jakarta.apache.org/");
090: man.addCookieFromHeader("test=1;domain=.jakarta.apache.org",
091: url);
092: assertNotNull(man.getCookieHeaderForURL(url));
093: }
094:
095: /**
096: * Test that we won't be tricked by similar host names (this was a past
097: * bug, although it never got reported in the bug database):
098: */
099: public void testSimilarHostNames() throws Exception {
100: URL url = new URL("http://ache.org/");
101: man.addCookieFromHeader("test=1", url);
102: url = new URL("http://jakarta.apache.org/");
103: assertNull(man.getCookieHeaderForURL(url));
104: }
105:
106: // Test session cookie is returned
107: public void testSessionCookie() throws Exception {
108: URL url = new URL("http://a.b.c/");
109: man.addCookieFromHeader("test=1", url);
110: String s = man.getCookieHeaderForURL(url);
111: assertNotNull(s);
112: assertEquals("test=1", s);
113: }
114:
115: // Bug 2063
116: public void testCookieWithEquals() throws Exception {
117: URL url = new URL("http://a.b.c/");
118: man.addCookieFromHeader("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", url);
119: String s = man.getCookieHeaderForURL(url);
120: assertNotNull(s);
121: assertEquals("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", s);
122: Cookie c = man.get(0);
123: assertEquals("NSCP_USER_LOGIN1_NEW", c.getName());
124: assertEquals("SHA=xxxxx", c.getValue());
125: }
126:
127: // Test Old cookie is not returned
128: public void testOldCookie() throws Exception {
129: URL url = new URL("http://a.b.c/");
130: man.addCookieFromHeader(
131: "test=1; expires=Mon, 01-Jan-1990 00:00:00 GMT", url);
132: String s = man.getCookieHeaderForURL(url);
133: assertNull(s);
134: }
135:
136: // Test New cookie is returned
137: public void testNewCookie() throws Exception {
138: URL url = new URL("http://a.b.c/");
139: man.addCookieFromHeader(
140: "test=1; expires=Mon, 01-Jan-2990 00:00:00 GMT", url);
141: assertEquals(1, man.getCookieCount());
142: String s = man.getCookieHeaderForURL(url);
143: assertNotNull(s);
144: assertEquals("test=1", s);
145: }
146:
147: // Test multi-cookie header handling
148: public void testCookies1() throws Exception {
149: URL url = new URL("http://a.b.c.d/testCookies1");
150: man
151: .addCookieFromHeader(
152: "test1=1; comment=\"how,now\", test2=2; version=1",
153: url);
154: assertEquals(2, man.getCookieCount());
155: String s = man.getCookieHeaderForURL(url);
156: assertNotNull(s);
157: assertEquals("test1=1; test2=2", s);
158: }
159:
160: public void testCookies2() throws Exception {
161: URL url = new URL("https://a.b.c.d/testCookies2");
162: man.addCookieFromHeader("test1=1;secure, test2=2;secure", url);
163: assertEquals(2, man.getCookieCount());
164: String s = man.getCookieHeaderForURL(url);
165: assertNotNull(s);
166: assertEquals("test1=1; test2=2", s);
167: }
168:
169: // Test duplicate cookie handling
170: public void testDuplicateCookie() throws Exception {
171: URL url = new URL("http://a.b.c/");
172: man.addCookieFromHeader("test=1", url);
173: String s = man.getCookieHeaderForURL(url);
174: assertNotNull(s);
175: assertEquals("test=1", s);
176: man.addCookieFromHeader("test=2", url);
177: s = man.getCookieHeaderForURL(url);
178: assertNotNull(s);
179: assertEquals("test=2", s);
180: }
181:
182: public void testDuplicateCookie2() throws Exception {
183: URL url = new URL("http://a.b.c/");
184: man.addCookieFromHeader("test=1", url);
185: man.addCookieFromHeader("test2=a", url);
186: String s = man.getCookieHeaderForURL(url);
187: assertNotNull(s);
188: assertEquals("test=1; test2=a", s); // Assumes some kind of list is used
189: man.addCookieFromHeader("test=2", url);
190: man.addCookieFromHeader("test3=b", url);
191: s = man.getCookieHeaderForURL(url);
192: assertNotNull(s);
193: assertEquals("test2=a; test=2; test3=b", s);// Assumes some kind of list is use
194: // If not using a list that retains the order, then the asserts would need to change
195: }
196:
197: /** Tests missing cookie path for a trivial URL fetch from the domain
198: * Note that this fails prior to a fix for BUG 38256
199: */
200: public void testMissingPath0() throws Exception {
201: URL url = new URL("http://d.e.f/goo.html");
202: man.addCookieFromHeader("test=moo", url);
203: String s = man.getCookieHeaderForURL(new URL("http://d.e.f/"));
204: assertNotNull(s);
205: assertEquals("test=moo", s);
206: }
207:
208: /** Tests missing cookie path for a non-trivial URL fetch from the
209: * domain. Note that this fails prior to a fix for BUG 38256
210: */
211: public void testMissingPath1() throws Exception {
212: URL url = new URL("http://d.e.f/moo.html");
213: man.addCookieFromHeader("test=moo", url);
214: String s = man.getCookieHeaderForURL(new URL(
215: "http://d.e.f/goo.html"));
216: assertNotNull(s);
217: assertEquals("test=moo", s);
218: }
219:
220: /** Tests explicit root path with a trivial URL fetch from the domain */
221: public void testRootPath0() throws Exception {
222: URL url = new URL("http://d.e.f/goo.html");
223: man.addCookieFromHeader("test=moo;path=/", url);
224: String s = man.getCookieHeaderForURL(new URL("http://d.e.f/"));
225: assertNotNull(s);
226: assertEquals("test=moo", s);
227: }
228:
229: /** Tests explicit root path with a non-trivial URL fetch from the domain */
230: public void testRootPath1() throws Exception {
231: URL url = new URL("http://d.e.f/moo.html");
232: man.addCookieFromHeader("test=moo;path=/", url);
233: String s = man.getCookieHeaderForURL(new URL(
234: "http://d.e.f/goo.html"));
235: assertNotNull(s);
236: assertEquals("test=moo", s);
237: }
238:
239: // Test cookie matching
240: public void testCookieMatching() throws Exception {
241: URL url = new URL("http://a.b.c:8080/TopDir/fred.jsp");
242: man.addCookieFromHeader("ID=abcd; Path=/TopDir", url);
243: String s = man.getCookieHeaderForURL(url);
244: assertNotNull(s);
245: assertEquals("ID=abcd", s);
246:
247: url = new URL("http://a.b.c:8080/other.jsp");
248: s = man.getCookieHeaderForURL(url);
249: assertNull(s);
250:
251: url = new URL("http://a.b.c:8080/TopDir/suub/another.jsp");
252: s = man.getCookieHeaderForURL(url);
253: assertNotNull(s);
254:
255: url = new URL("http://a.b.c:8080/TopDir");
256: s = man.getCookieHeaderForURL(url);
257: assertNotNull(s);
258:
259: url = new URL("http://a.b.d/");
260: s = man.getCookieHeaderForURL(url);
261: assertNull(s);
262: }
263:
264: public void testCookieOrdering1() throws Exception {
265: URL url = new URL("http://order.now/sub1/moo.html");
266: man.addCookieFromHeader("test1=moo1;path=/", url);
267: man.addCookieFromHeader("test2=moo2;path=/sub1", url);
268: man.addCookieFromHeader("test2=moo3;path=/", url);
269: assertEquals(3, man.getCookieCount());
270: String s = man.getCookieHeaderForURL(url);
271: assertNotNull(s);
272: assertEquals("test2=moo2; test1=moo1; test2=moo3", s);
273: }
274:
275: public void testCookieOrdering2() throws Exception {
276: URL url = new URL("http://order.now/sub1/moo.html");
277: man.addCookieFromHeader("test1=moo1;", url);
278: man.addCookieFromHeader("test2=moo2;path=/sub1", url);
279: man.addCookieFromHeader("test2=moo3;path=/", url);
280: assertEquals(3, man.getCookieCount());
281: assertEquals("/sub1", man.get(0).getPath()); // Defaults to caller URL
282: assertEquals("/sub1", man.get(1).getPath());
283: assertEquals("/", man.get(2).getPath());
284: String s = man.getCookieHeaderForURL(url);
285: assertNotNull(s);
286: org.apache.commons.httpclient.Cookie[] c = man
287: .getCookiesForUrl(url);
288: assertEquals("/sub1", c[0].getPath());
289: assertFalse(c[0].isPathAttributeSpecified());
290: assertEquals("/sub1", c[1].getPath());
291: assertTrue(c[1].isPathAttributeSpecified());
292: assertEquals("/", c[2].getPath());
293: assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
294: }
295:
296: public void testCookiePolicy2109() throws Exception {
297: man.setCookiePolicy(CookiePolicy.RFC_2109);
298: URL url = new URL("http://order.now/sub1/moo.html");
299: man.addCookieFromHeader("test1=moo1;", url);
300: man.addCookieFromHeader("test2=moo2;path=/sub1", url);
301: man.addCookieFromHeader("test2=moo3;path=/", url);
302: assertEquals(3, man.getCookieCount());
303: //assertEquals("/",man.get(0).getPath());
304: assertEquals("/sub1", man.get(1).getPath());
305: assertEquals("/", man.get(2).getPath());
306: String s = man.getCookieHeaderForURL(url);
307: assertNotNull(s);
308: org.apache.commons.httpclient.Cookie[] c = man
309: .getCookiesForUrl(url);
310: assertEquals("/sub1", c[0].getPath());
311: assertFalse(c[0].isPathAttributeSpecified());
312: assertEquals("/sub1", c[1].getPath());
313: assertTrue(c[1].isPathAttributeSpecified());
314: assertEquals("/", c[2].getPath());
315: assertTrue(c[2].isPathAttributeSpecified());
316: assertEquals(
317: "$Version=0; test1=moo1; test2=moo2; $Path=/sub1; test2=moo3; $Path=/",
318: s);
319: }
320:
321: public void testCookiePolicyNetscape() throws Exception {
322: man.setCookiePolicy(CookiePolicy.NETSCAPE);
323: URL url = new URL("http://order.now/sub1/moo.html");
324: man.addCookieFromHeader("test1=moo1;", url);
325: man.addCookieFromHeader("test2=moo2;path=/sub1", url);
326: man.addCookieFromHeader("test2=moo3;path=/", url);
327: assertEquals(3, man.getCookieCount());
328: assertEquals("/sub1", man.get(0).getPath());
329: assertEquals("/sub1", man.get(1).getPath());
330: assertEquals("/", man.get(2).getPath());
331: String s = man.getCookieHeaderForURL(url);
332: assertNotNull(s);
333: org.apache.commons.httpclient.Cookie[] c = man
334: .getCookiesForUrl(url);
335: assertEquals("/sub1", c[0].getPath());
336: assertFalse(c[0].isPathAttributeSpecified());
337: assertEquals("/sub1", c[1].getPath());
338: assertTrue(c[1].isPathAttributeSpecified());
339: assertEquals("/", c[2].getPath());
340: assertTrue(c[2].isPathAttributeSpecified());
341: assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
342: }
343:
344: public void testCookiePolicyIgnore() throws Exception {
345: man.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
346: URL url = new URL("http://order.now/sub1/moo.html");
347: man.addCookieFromHeader("test1=moo1;", url);
348: man.addCookieFromHeader("test2=moo2;path=/sub1", url);
349: man.addCookieFromHeader("test2=moo3;path=/", url);
350: assertEquals(0, man.getCookieCount());// Cookies are ignored
351: Cookie cc;
352: cc = new Cookie("test1", "moo1", null, "/sub1", false, 0,
353: false, false);
354: man.add(cc);
355: cc = new Cookie("test2", "moo2", null, "/sub1", false, 0, true,
356: false);
357: man.add(cc);
358: cc = new Cookie("test3", "moo3", null, "/", false, 0, false,
359: false);
360: man.add(cc);
361: assertEquals(3, man.getCookieCount());
362: assertEquals("/sub1", man.get(0).getPath());
363: assertEquals("/sub1", man.get(1).getPath());
364: assertEquals("/", man.get(2).getPath());
365: String s = man.getCookieHeaderForURL(url);
366: assertNull(s);
367: org.apache.commons.httpclient.Cookie[] c = man
368: .getCookiesForUrl(url);
369: assertEquals(0, c.length); // Cookies again ignored
370: }
371:
372: public void testLoad() throws Exception {
373: assertEquals(0, man.getCookieCount());
374: man.addFile("testfiles/cookies.txt");
375: assertEquals(3, man.getCookieCount());
376:
377: int num = 0;
378: assertEquals("name", man.get(num).getName());
379: assertEquals("value", man.get(num).getValue());
380: assertEquals("path", man.get(num).getPath());
381: assertEquals("domain", man.get(num).getDomain());
382: assertTrue(man.get(num).getSecure());
383: assertEquals(num, man.get(num).getExpires());
384:
385: num++;
386: assertEquals("name2", man.get(num).getName());
387: assertEquals("value2", man.get(num).getValue());
388: assertEquals("/", man.get(num).getPath());
389: assertEquals("", man.get(num).getDomain());
390: assertFalse(man.get(num).getSecure());
391: assertEquals(0, man.get(num).getExpires());
392:
393: num++;
394: assertEquals("a", man.get(num).getName());
395: assertEquals("b", man.get(num).getValue());
396: assertEquals("d", man.get(num).getPath());
397: assertEquals("c", man.get(num).getDomain());
398: assertTrue(man.get(num).getSecure());
399: assertEquals(0, man.get(num).getExpires()); // Show that maxlong now saved as 0
400: }
401: }
|