KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > test > CookieUnitTestCase


1 package org.jboss.test.web.test;
2
3 import java.net.HttpURLConnection JavaDoc;
4 import java.net.URL JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.apache.commons.httpclient.Cookie;
10 import org.apache.commons.httpclient.HttpClient;
11 import org.apache.commons.httpclient.HttpMethodBase;
12 import org.jboss.test.JBossTestCase;
13 import org.jboss.test.JBossTestSetup;
14 import org.jboss.test.util.web.HttpUtils;
15
16 /**
17  *Test case for cookie
18  *@author prabhat.jha@jboss.com
19  *@version $Revision$
20  */

21
22 public class CookieUnitTestCase extends JBossTestCase
23
24 {
25     protected static String JavaDoc[] cookieNames= {"simpleCookie","withSpace","commented","expired"};
26     private String JavaDoc baseURL = HttpUtils.getBaseURL();
27     
28     public CookieUnitTestCase(String JavaDoc name)
29     {
30         super(name);
31     }
32     
33     public void testCookieSetCorrectly() throws Exception JavaDoc
34     {
35         log.info("testCookieSetCorrectly");
36         URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest-cookie/CookieReadServlet");
37         HttpClient httpClient = new HttpClient();
38         HttpMethodBase request = HttpUtils.createMethod(url,HttpUtils.GET);
39         //sending a blank request
40
httpClient.executeMethod(request);
41         
42         log.info("sending request with cookie");
43         request = HttpUtils.createMethod(url,HttpUtils.POST);
44         int responseCode = httpClient.executeMethod(request);
45         assertEquals(responseCode,HttpURLConnection.HTTP_OK);
46     }
47     
48     public void testCookieRetrievedCorrectly() throws Exception JavaDoc
49     {
50         URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest-cookie/CookieServlet");
51         HttpClient httpClient = new HttpClient();
52         HttpMethodBase request = HttpUtils.createMethod(url,HttpUtils.GET);
53         int responseCode =httpClient.executeMethod(request);
54         //assert that we are able to hit servlet successfully
55
assertEquals(responseCode,HttpURLConnection.HTTP_OK);
56         request.getResponseHeader("Set-Cookie");
57         
58         Cookie[] cookies = httpClient.getState().getCookies();
59         //verify that expired cookie is not set by server
60
assertTrue("sever did not set expired cookie on client", checkNoExpiredCookie(cookies));
61         
62         for(int i = 0; i < cookies.length; i++) {
63             log.info("Cookie " + i + " : " + cookies[i].toExternalForm());
64             if(cookies[i].getName().equals("simpleCookie")) {
65                 assertTrue("cookie value should be jboss", cookies[i].getValue().equals("jboss"));
66                 assertEquals("cookie path", "/jbosstest-cookie", cookies[i].getPath());
67                 assertEquals("cookie persistence", false, cookies[i].isPersistent());
68             }
69             else if(cookies[i].getName().equals("withSpace"))
70                 assertEquals("should be no quote in cookie with space", cookies[i].getValue().indexOf("\""),-1);
71             else if(cookies[i].getName().equals("commented")) {
72                 log.info("comment in cookie: " + cookies[i].getComment());
73                 //RFC2109:Note that there is no Comment attribute in the Cookie request header
74
//corresponding to the one in the Set-Cookie response header. The user
75
//agent does not return the comment information to the origin server.
76

77                 assertTrue(cookies[i].getComment() == null);
78             } else if(cookies[i].getName().equals("withComma")) {
79                 assertTrue("should contain a comma", cookies[i].getValue().indexOf(",") != -1);
80             }
81             else if(cookies[i].getName().equals("expireIn10Sec")) {
82                 log.info("will sleep for 5 seconds to see if cookie expires");
83                 Thread.sleep(5000);
84                 assertTrue("cookies should not be expired by now", !cookies[i].isExpired());
85                 log.info("will sleep for 5 more secs and it should expire");
86                 Thread.sleep(5000);
87                 assertTrue("cookies should be expired by now", cookies[i].isExpired());
88             }
89             
90         }
91     }
92     
93     protected boolean checkNoExpiredCookie(Cookie[] cookies)
94     {
95         for(int i = 0; i < cookies.length; i++)
96             if(cookies[i].getName().equals("expired"))
97                 return false;
98         return true;
99     }
100     
101     /**
102      * Setup the test suite.
103      */

104     public static Test suite() throws Exception JavaDoc
105     {
106         TestSuite suite = new TestSuite(CookieUnitTestCase.class);
107         
108         // Create an initializer for the test suite
109
Test wrapper = new JBossTestSetup(suite)
110         {
111             protected void setUp() throws Exception JavaDoc
112             {
113                 super.setUp();
114                 redeploy("jbosstest-cookie.war");
115             }
116             protected void tearDown() throws Exception JavaDoc
117             {
118                 undeploy("jbosstest-cookie.war");
119                 super.tearDown();
120             }
121         };
122         return wrapper;
123     }
124 }
125
Popular Tags