1 7 package org.jboss.test.security.test.mapping; 8 9 import java.net.HttpURLConnection ; 10 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 14 import org.apache.commons.httpclient.Cookie; 15 import org.apache.commons.httpclient.Header; 16 import org.apache.commons.httpclient.HttpClient; 17 import org.apache.commons.httpclient.HttpState; 18 import org.apache.commons.httpclient.methods.GetMethod; 19 import org.apache.commons.httpclient.methods.PostMethod; 20 import org.jboss.test.JBossTestCase; 21 import org.jboss.test.JBossTestSetup; 22 23 29 public class RoleMappingWebTestCase extends JBossTestCase 30 { 31 private String baseURLNoAuth = "http://" + getServerHost() 32 + ":" + Integer.getInteger("web.port", 8080) + "/"; 33 private HttpClient httpConn = new HttpClient(); 34 35 public RoleMappingWebTestCase(String name) 36 { 37 super(name); 38 } 39 40 44 public void testWebAccess() throws Exception 45 { 46 GetMethod indexGet = new GetMethod(baseURLNoAuth+"web-role-map/Secured.jsp"); 47 int responseCode = httpConn.executeMethod(indexGet); 48 String body = indexGet.getResponseBodyAsString(); 49 assertTrue("Get OK("+responseCode+")", responseCode == HttpURLConnection.HTTP_OK); 50 assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0 ); 51 52 HttpState state = httpConn.getState(); 53 Cookie[] cookies = state.getCookies(); 54 String sessionID = null; 55 for(int c = 0; c < cookies.length; c ++) 56 { 57 Cookie k = cookies[c]; 58 if( k.getName().equalsIgnoreCase("JSESSIONID") ) 59 sessionID = k.getValue(); 60 } 61 getLog().debug("Saw JSESSIONID="+sessionID); 62 63 PostMethod formPost = new PostMethod(baseURLNoAuth+"web-role-map/j_security_check"); 65 formPost.addRequestHeader("Referer", baseURLNoAuth+"web-role-map/login.html"); 66 formPost.addParameter("j_username", "user"); 67 formPost.addParameter("j_password", "pass"); 68 responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), 69 formPost, state); 70 String response = formPost.getStatusText(); 71 log.debug("responseCode="+responseCode+", response="+response); 72 assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); 73 74 Header location = formPost.getResponseHeader("Location"); 76 String indexURI = location.getValue(); 77 GetMethod war1Index = new GetMethod(indexURI); 78 responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), 79 war1Index, state); 80 response = war1Index.getStatusText(); 81 log.debug("responseCode="+responseCode+", response="+response); 82 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 83 body = war1Index.getResponseBodyAsString(); 84 if( body.indexOf("j_security_check") > 0 ) 85 fail("get of "+indexURI+" redirected to login page"); 86 } 87 88 public static Test suite() throws Exception 89 { 90 TestSuite suite = new TestSuite(); 91 suite.addTest(new TestSuite(RoleMappingWebTestCase.class)); 92 93 Test wrapper = new JBossTestSetup(suite) 95 { 96 protected void setUp() throws Exception 97 { 98 super.setUp(); 99 deploy(getResourceURL("security-spi/rolemapping/rolemapping-test-service.xml")); 100 deploy("web-role-map.war"); 101 } 102 protected void tearDown() throws Exception 103 { 104 undeploy(getResourceURL("security-spi/rolemapping/rolemapping-test-service.xml")); 105 undeploy("web-role-map.war"); 106 super.tearDown(); 107 } 108 }; 109 return wrapper; 110 } 111 } 112 | Popular Tags |