1 22 package org.jboss.test.web.test; 23 24 import java.net.HttpURLConnection ; 25 26 import junit.framework.TestCase; 27 import org.apache.commons.httpclient.Cookie; 28 import org.apache.commons.httpclient.Header; 29 import org.apache.commons.httpclient.HttpClient; 30 import org.apache.commons.httpclient.HttpState; 31 import org.apache.commons.httpclient.methods.GetMethod; 32 import org.apache.commons.httpclient.methods.PostMethod; 33 import org.jboss.logging.Logger; 34 35 41 public abstract class SSOBaseCase extends TestCase 42 { 43 47 protected static void executeFormAuthSingleSignOnTest(String serverA, 48 String serverB, 49 Logger log) 50 throws Exception 51 { 52 HttpClient httpConn = new HttpClient(); 54 GetMethod indexGet = new GetMethod(serverA+"/war1/index.html"); 55 int responseCode = httpConn.executeMethod(indexGet); 56 String body = indexGet.getResponseBodyAsString(); 57 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 58 assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0 ); 59 60 HttpState state = httpConn.getState(); 61 Cookie[] cookies = state.getCookies(); 62 String sessionID = null; 63 for(int c = 0; c < cookies.length; c ++) 64 { 65 Cookie k = cookies[c]; 66 if( k.getName().equalsIgnoreCase("JSESSIONID") ) 67 sessionID = k.getValue(); 68 } 69 log.debug("Saw JSESSIONID="+sessionID); 70 71 PostMethod formPost = new PostMethod(serverA+"/war1/j_security_check"); 73 formPost.addRequestHeader("Referer", serverA+"/war1/login.html"); 74 formPost.addParameter("j_username", "jduke"); 75 formPost.addParameter("j_password", "theduke"); 76 responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), 77 formPost, state); 78 String response = formPost.getStatusText(); 79 log.debug("responseCode="+responseCode+", response="+response); 80 assertTrue("Saw HTTP_MOVED_TEMP("+responseCode+")", 81 responseCode == HttpURLConnection.HTTP_MOVED_TEMP); 82 83 Header location = formPost.getResponseHeader("Location"); 85 String indexURI = location.getValue(); 86 GetMethod war1Index = new GetMethod(indexURI); 87 responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), 88 war1Index, state); 89 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 90 body = war1Index.getResponseBodyAsString(); 91 if( body.indexOf("j_security_check") > 0 ) 92 fail("get of "+indexURI+" redirected to login page"); 93 94 cookies = state.getCookies(); 95 String ssoID = null; 96 for(int c = 0; c < cookies.length; c ++) 97 { 98 Cookie k = cookies[c]; 99 if( k.getName().equalsIgnoreCase("JSESSIONIDSSO") ) 100 { 101 ssoID = k.getValue(); 102 if (serverA.equals(serverB) == false) 103 { 104 Cookie copy = copyCookie(k, serverB); 106 state.addCookie(copy); 107 log.debug("Added state cookie: "+copy); 108 } 109 } 110 } 111 assertTrue("Saw JSESSIONIDSSO", ssoID != null); 112 log.debug("Saw JSESSIONIDSSO="+ssoID); 113 114 if (!serverA.equals(serverB)) 120 Thread.sleep(500); 121 122 log.debug("Prepare /war2/index.html get"); 124 GetMethod war2Index = new GetMethod(serverB+"/war2/index.html"); 125 responseCode = httpConn.executeMethod(war2Index.getHostConfiguration(), 126 war2Index, state); 127 response = war2Index.getStatusText(); 128 log.debug("responseCode="+responseCode+", response="+response); 129 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 130 body = war2Index.getResponseBodyAsString(); 131 log.debug("body: "+body); 132 if( body.indexOf("j_security_check") > 0 ) 133 fail("get of /war2/index.html redirected to login page"); 134 135 138 GetMethod war2Servlet = new GetMethod(serverB+"/war2/EJBServlet"); 139 responseCode = httpConn.executeMethod(war2Servlet.getHostConfiguration(), 140 war2Servlet, state); 141 response = war2Servlet.getStatusText(); 142 log.debug("responseCode="+responseCode+", response="+response); 143 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 144 body = war2Servlet.getResponseBodyAsString(); 145 log.debug("body: "+body); 146 if( body.indexOf("j_security_check") > 0 ) 147 fail("get of /war2/EJBServlet redirected to login page"); 148 149 GetMethod war2Logout = new GetMethod(serverB+"/war2/Logout"); 151 war2Logout.setFollowRedirects(false); 152 responseCode = httpConn.executeMethod(war2Logout.getHostConfiguration(), 153 war2Logout, state); 154 response = war2Logout.getStatusText(); 155 log.debug("responseCode="+responseCode+", response="+response); 156 assertTrue("Logout: Saw HTTP_MOVED_TEMP("+responseCode+")", 157 responseCode == HttpURLConnection.HTTP_MOVED_TEMP); 158 location = war2Logout.getResponseHeader("Location"); 159 indexURI = location.getValue(); 160 if( indexURI.indexOf("index.html") < 0 ) 161 fail("get of /war2/Logout not redirected to login page"); 162 163 if (!serverA.equals(serverB)) 165 Thread.sleep(500); 166 167 war1Index = new GetMethod(serverA+"/war1/index.html"); 169 responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), 170 war1Index, state); 171 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 172 body = war1Index.getResponseBodyAsString(); 173 log.debug("body: " + body); 174 if( body.indexOf("j_security_check") < 0 ) 175 fail("get of /war1/index.html not redirected to login page"); 176 177 war2Index = new GetMethod(serverB+"/war2/index.html"); 179 responseCode = httpConn.executeMethod(war2Index.getHostConfiguration(), 180 war2Index, state); 181 assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); 182 body = war2Index.getResponseBodyAsString(); 183 log.debug("body: " + body); 184 if( body.indexOf("j_security_check") < 0 ) 185 fail("get of /war2/index.html not redirected to login page"); 186 187 } 188 189 public static Cookie copyCookie(Cookie toCopy, String targetServer) 190 { 191 int index = targetServer.indexOf("://"); 193 if (index > -1) 194 { 195 targetServer = targetServer.substring(index + 3); 196 } 197 index = targetServer.indexOf(":"); 198 if (index > -1) 199 { 200 targetServer = targetServer.substring(0, index); 201 } 202 index = targetServer.indexOf("/"); 203 if (index > -1) 204 { 205 targetServer = targetServer.substring(0, index); 206 } 207 208 Cookie copy = new Cookie(targetServer, 209 toCopy.getName(), 210 toCopy.getValue(), 211 "/", 212 null, 213 false); 214 return copy; 215 } 216 } 217 | Popular Tags |