1 package org.jboss.test.web.test; 2 3 import java.net.HttpURLConnection ; 4 import java.net.URL ; 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 21 22 public class CookieUnitTestCase extends JBossTestCase 23 24 { 25 protected static String [] cookieNames= {"simpleCookie","withSpace","commented","expired"}; 26 private String baseURL = HttpUtils.getBaseURL(); 27 28 public CookieUnitTestCase(String name) 29 { 30 super(name); 31 } 32 33 public void testCookieSetCorrectly() throws Exception 34 { 35 log.info("testCookieSetCorrectly"); 36 URL url = new URL (baseURL+"jbosstest-cookie/CookieReadServlet"); 37 HttpClient httpClient = new HttpClient(); 38 HttpMethodBase request = HttpUtils.createMethod(url,HttpUtils.GET); 39 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 49 { 50 URL url = new URL (baseURL+"jbosstest-cookie/CookieServlet"); 51 HttpClient httpClient = new HttpClient(); 52 HttpMethodBase request = HttpUtils.createMethod(url,HttpUtils.GET); 53 int responseCode =httpClient.executeMethod(request); 54 assertEquals(responseCode,HttpURLConnection.HTTP_OK); 56 request.getResponseHeader("Set-Cookie"); 57 58 Cookie[] cookies = httpClient.getState().getCookies(); 59 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 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 104 public static Test suite() throws Exception 105 { 106 TestSuite suite = new TestSuite(CookieUnitTestCase.class); 107 108 Test wrapper = new JBossTestSetup(suite) 110 { 111 protected void setUp() throws Exception 112 { 113 super.setUp(); 114 redeploy("jbosstest-cookie.war"); 115 } 116 protected void tearDown() throws Exception 117 { 118 undeploy("jbosstest-cookie.war"); 119 super.tearDown(); 120 } 121 }; 122 return wrapper; 123 } 124 } 125 | Popular Tags |