1 7 package com.inversoft.junit.internal; 8 9 10 import java.io.IOException ; 11 import java.net.MalformedURLException ; 12 import java.util.Enumeration ; 13 import java.util.HashMap ; 14 import java.util.List ; 15 import java.util.Map ; 16 import javax.servlet.http.Cookie ; 17 18 import org.apache.commons.httpclient.Header; 19 import org.apache.commons.httpclient.HostConfiguration; 20 import org.apache.commons.httpclient.HttpClient; 21 import org.apache.commons.httpclient.HttpState; 22 import org.apache.commons.httpclient.cookie.CookiePolicy; 23 import org.apache.commons.httpclient.cookie.CookieSpec; 24 import org.apache.commons.httpclient.methods.PostMethod; 25 import org.apache.log4j.Logger; 26 import org.jdom.Document; 27 import org.jdom.JDOMException; 28 import org.jdom.input.SAXBuilder; 29 30 import com.inversoft.junit.Configuration; 31 import com.inversoft.junit.Request; 32 import com.inversoft.junit.Response; 33 import com.inversoft.junit.Result; 34 import com.inversoft.junit.URL; 35 import com.inversoft.junit.WebTestCase; 36 37 38 46 public class HttpCaller { 47 48 51 private static final Logger logger = Logger.getLogger(HttpCaller.class); 52 53 56 private static HttpClient client; 57 58 61 private static boolean persisting; 62 63 66 public static final String SET_COOKIE = "Set-Cookie"; 67 public static final String SET_COOKIE2 = "Set-Cookie2"; 68 69 70 static { 72 persisting = Configuration.isPersistingSession(); 73 74 if (persisting) { 75 76 String urlStr = Configuration.getTestLocation(WebTestCase.class); 78 java.net.URL url = createURL(urlStr); 79 80 HostConfiguration hc = new HostConfiguration(); 81 hc.setHost(url.getHost(), url.getPort()); 82 83 client = new HttpClient(); 84 client.setHostConfiguration(hc); 85 } 86 } 87 88 91 private static java.net.URL createURL(String urlStr) { 92 try { 93 return new java.net.URL (urlStr); 94 } catch (MalformedURLException mue) { 95 logger.error("The test location URL '" + urlStr + "' is invalid", mue); 96 97 throw new IllegalArgumentException ("The test location URL '" + 98 urlStr + "' is invalid"); 99 } 100 } 101 102 103 106 public HttpCaller() { 107 } 109 110 111 128 public Response runTest(WebTestCase testCase, Request request) throws Throwable { 129 String testLocation = request.getTestLocation(); 130 131 if (testLocation == null) { 133 testLocation = Configuration.getTestLocation(testCase.getClass()); 134 } 135 136 java.net.URL url = createURL(testLocation); 137 PostMethod method = new PostMethod(url.getPath()); 138 HttpState state = new HttpState(); 139 140 executeTestRequest(testCase, request, method, state, url); 141 142 Result result = executeResultRequest(request, url); 144 145 if (!result.isSuccessful()) { 146 throw result.getThrowable(); 147 } 148 149 Map cookies = fetchCookies(request, method, url); 150 return new Response(request, method.getStatusCode(), 151 method.getResponseBodyAsString(), null, 152 method.getResponseBodyAsStream(), cookies); 153 } 154 155 158 protected void executeTestRequest(WebTestCase testCase, Request request, 159 PostMethod method, HttpState state, java.net.URL url) { 160 HttpClient localClient; 161 162 if (persisting) { 163 localClient = client; 164 } else { 165 HostConfiguration hc = new HostConfiguration(); 166 hc.setHost(url.getHost(), url.getPort()); 167 168 localClient = new HttpClient(); 169 localClient.setHostConfiguration(hc); 170 } 171 172 String [] headers = request.getHeaderNames(); 174 for (int i = 0; i < headers.length; i++) { 175 method.addRequestHeader(headers[i], request.getHeader(headers[i])); 176 } 177 178 Cookie [] cookies = request.getCookies(); 180 org.apache.commons.httpclient.Cookie cookie; 181 for (int i = 0; i < cookies.length; i++) { 182 cookie = new org.apache.commons.httpclient.Cookie( 183 cookies[i].getDomain(), cookies[i].getName(), cookies[i].getValue(), 184 cookies[i].getPath(), cookies[i].getMaxAge(), cookies[i].getSecure()); 185 state.addCookie(cookie); 186 } 187 188 setupParameters(request, method); 190 191 method.addParameter(Constants.URL_REQUEST_TYPE_PARAM, Constants.REQUEST_TYPE_TEST); 193 method.addParameter(Constants.URL_CLASS_PARAM, testCase.getClass().getName()); 194 method.addParameter(Constants.URL_METHOD_PARAM, testCase.getName()); 195 196 try { 198 localClient.setState(state); 199 localClient.executeMethod(method); 200 } catch (Exception e) { 201 logger.error("Error executing the test HTTP request", e); 202 throw new RuntimeException ("Error executing the test HTTP request: " + e.toString()); 203 } 204 205 int responseCode = method.getStatusCode(); 206 if (responseCode != 200 && responseCode != 202) { 207 logger.error("Server side error: " + responseCode); 208 throw new RuntimeException ("Server side error: " + responseCode); 209 } 210 } 211 212 216 protected Result executeResultRequest(Request request, java.net.URL url) { 217 HttpClient localClient = new HttpClient(); 218 PostMethod method = new PostMethod(url.getPath()); 219 HttpState state = new HttpState(); 220 221 HostConfiguration hc = new HostConfiguration(); 222 hc.setHost(url.getHost(), url.getPort()); 223 224 localClient.setHostConfiguration(hc); 225 localClient.setState(state); 226 227 setupParameters(request, method); 229 230 method.addParameter(Constants.URL_REQUEST_TYPE_PARAM, Constants.REQUEST_TYPE_RESULT); 232 233 try { 234 localClient.executeMethod(method); 235 } catch (Exception e) { 236 logger.error("Error executing the result HTTP request", e); 238 throw new RuntimeException ("Error executing the result HTTP request", e); 239 } 240 241 try { 243 int responseCode = method.getStatusCode(); 244 if (responseCode != 200 && responseCode != 202) { 245 logger.error("Server side error: " + responseCode); 246 throw new RuntimeException ("Server side error: " + responseCode); 247 } 248 249 SAXBuilder builder = new SAXBuilder(); 250 Document document = builder.build(method.getResponseBodyAsStream()); 251 Result result = new Result(); 252 253 result.fromXML(document); 254 255 return result; 256 } catch (JDOMException je) { 257 logger.error("Error parsing result XML", je); 258 throw new RuntimeException ("Error parsing result XML", je); 259 } catch (IOException ioe) { 260 logger.error("Error parsing result XML", ioe); 261 throw new RuntimeException ("Error parsing result XML", ioe); 262 } 263 } 264 265 271 protected void setupParameters(Request request, PostMethod method) { 272 Enumeration keys = request.getParameterNames(); 273 while (keys.hasMoreElements()) { 274 String key = (String ) keys.nextElement(); 275 String [] values = request.getParameterValues(key); 276 for (int i = 0; i < values.length; i++) { 277 method.addParameter(key, values[i]); 278 } 279 } 280 } 281 282 286 protected Map fetchCookies(Request request, PostMethod method, java.net.URL jurl) { 287 Map cookies = new HashMap (); 289 Header[] headers = method.getResponseHeaders(); 290 URL url = request.getURL(); 291 String name; 292 String domain; 293 String path; 294 int port; 295 296 if (url == null) { 299 domain = jurl.getHost(); 300 path = jurl.getPath(); 301 port = jurl.getPort(); 302 } else { 303 domain = (url.getServerName() != null) ? url.getServerName() : 304 jurl.getHost(); 305 port = (url.getServerPort() != -1) ? url.getServerPort() : 306 jurl.getPort(); 307 308 String servletPath = url.getServletPath(); 309 String context = (url.getContextPath() != null) ? url.getContextPath() : 310 ""; 311 String pathInfo = (url.getPathInfo() != null) ? url.getPathInfo() : 312 ""; 313 314 if (servletPath == null) { 315 path = jurl.getPath(); 316 } else { 317 StringBuffer buf = new StringBuffer (); 318 buf.append(context).append(servletPath).append(pathInfo); 319 path = buf.toString(); 320 } 321 } 322 323 CookieSpec spec = CookiePolicy.getCompatibilitySpec(); 325 for (int i = 0; i < headers.length; i++) { 326 327 name = headers[i].getName(); 328 if (name != null && 329 (name.equalsIgnoreCase(SET_COOKIE) || 330 name.equalsIgnoreCase(SET_COOKIE2))) { 331 332 try { 333 334 org.apache.commons.httpclient.Cookie[] cArray = 335 spec.parse(domain, port, path, false, headers[i]); 336 337 for (int j = 0; j < cArray.length; j++) { 338 Cookie cookie = new Cookie (cArray[j].getName(), cArray[j].getValue()); 339 cookie.setComment(cArray[j].getComment()); 340 cookie.setDomain(cArray[j].getDomain()); 341 342 if (cArray[j].getExpiryDate() == null) { 344 cookie.setMaxAge(-1); 345 } else { 346 cookie.setMaxAge( 347 (int) cArray[j].getExpiryDate().getTime() - 348 (int) System.currentTimeMillis()); 349 } 350 351 cookie.setPath(cArray[j].getPath()); 352 cookie.setSecure(cArray[j].getSecure()); 353 cookie.setVersion(cArray[j].getVersion()); 354 cookies.put(cookie.getName(), cookie); 355 } 356 357 } catch (Exception e) { 359 throw new RuntimeException ("Error parsing cookies", e); 360 } 361 } 362 } 363 364 return cookies; 365 } 366 } | Popular Tags |