1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.ByteArrayInputStream ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.UnsupportedEncodingException ; 44 import java.net.URL ; 45 import java.util.Collections ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 import org.org.apache.commons.httpclient.HttpState; 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 63 public class MockWebConnection extends WebConnection { 64 private class ResponseEntry { 65 private void validateParameters( 66 final Object content, 67 final String statusMessage, 68 final String contentType, 69 final List responseHeaders) { 70 Assert.notNull("content", content); 71 Assert.notNull("statusMessage", statusMessage); 72 Assert.notNull("contentType", contentType); 73 Assert.notNull("responseHeaders", responseHeaders); 74 final Iterator iterator = responseHeaders.iterator(); 76 while( iterator.hasNext() ) { 77 final Object object = iterator.next(); 78 if( object instanceof KeyValuePair == false ) { 79 throw new IllegalArgumentException ( 80 "Only KeyValuePairs may be in the response header list but found: " 81 + object.getClass().getName()); 82 } 83 } 84 } 85 86 94 public ResponseEntry( 95 final byte[] content, 96 final int statusCode, 97 final String statusMessage, 98 final String contentType, 99 final List responseHeaders ) { 100 101 validateParameters(content, statusMessage, contentType, responseHeaders); 102 content_ = content; 103 statusCode_ = statusCode; 104 statusMessage_ = statusMessage; 105 contentType_ = contentType; 106 responseHeaders_ = Collections.unmodifiableList(responseHeaders); 107 } 108 109 private final int statusCode_; 110 private final String statusMessage_; 111 private final byte[] content_; 112 private final String contentType_; 113 private final List responseHeaders_; 114 } 115 116 private byte[] stringToByteArray(final String content) { 117 byte[] contentBytes; 118 try { 119 contentBytes = content.getBytes("ISO-8859-1"); 120 } 121 catch (final UnsupportedEncodingException e) { 122 contentBytes = new byte[0]; 123 } 124 return contentBytes; 125 } 126 127 private final Map responseMap_ = new HashMap (10); 128 private ResponseEntry defaultResponseEntry_; 129 130 private SubmitMethod lastMethod_; 131 private List lastParameters_; 132 private HttpState httpState_ = new HttpState(); 133 134 139 public MockWebConnection( final WebClient webClient ) { 140 super( webClient ); 141 } 142 143 147 protected final Log getLog() { 148 return LogFactory.getLog(getClass()); 149 } 150 151 158 public WebResponse getResponse(final WebRequestSettings webRequestSettings) throws IOException { 159 final URL url = webRequestSettings.getURL(); 160 final SubmitMethod method = webRequestSettings.getSubmitMethod(); 161 final List parameters = webRequestSettings.getRequestParameters(); 162 163 getLog().debug("Getting response for " + url.toExternalForm()); 164 165 lastMethod_ = method; 166 lastParameters_ = parameters; 167 168 ResponseEntry entry = (ResponseEntry)responseMap_.get(url.toExternalForm()); 169 if( entry == null ) { 170 entry = defaultResponseEntry_; 171 if( entry == null ) { 172 throw new IllegalStateException ( 173 "No response specified that can handle url ["+url.toExternalForm()+"]"); 174 } 175 } 176 177 final ResponseEntry responseEntry = entry; 178 return new WebResponse() { 179 public int getStatusCode() { return responseEntry.statusCode_; } 180 public String getStatusMessage() { return responseEntry.statusMessage_; } 181 public String getContentType() { 182 final String contentTypeHeaderLine = responseEntry.contentType_; 183 final int index = contentTypeHeaderLine.indexOf( ';' ); 184 final String contentType; 185 if( index == -1 ) { 186 contentType = contentTypeHeaderLine; 187 } 188 else { 189 contentType = contentTypeHeaderLine.substring( 0, index ); 190 } 191 return contentType; 192 } 193 public String getContentAsString() { 194 try { 195 return new String (responseEntry.content_, getContentCharSet()); 196 } 197 catch (final UnsupportedEncodingException e) { 198 return null; 199 } 200 } 201 public URL getUrl() { return url; } 202 public List getResponseHeaders() { return responseEntry.responseHeaders_; } 203 public long getLoadTimeInMilliSeconds() { return 0; } 204 205 public String getResponseHeaderValue( final String headerName ) { 206 final Iterator iterator = responseEntry.responseHeaders_.iterator(); 207 while( iterator.hasNext() ) { 208 final KeyValuePair pair = (KeyValuePair)iterator.next(); 209 if( pair.getKey().equals( headerName ) ) { 210 return pair.getValue(); 211 } 212 } 213 return null; 214 } 215 216 public InputStream getContentAsStream() { 217 return new ByteArrayInputStream (responseEntry.content_); 218 } 219 public byte[] getResponseBody() { 220 224 return responseEntry.content_; 225 } 226 public String getContentCharSet() { 227 final String contentType = responseEntry.contentType_; 228 final String prefix = "charset="; 229 final int index = contentType.indexOf(prefix); 230 final String charset; 231 if( index == -1 ) { 232 charset = "ISO-8859-1"; 233 } 234 else { 235 charset = contentType.substring(index+prefix.length()); 236 } 237 return charset; 238 } 239 }; 240 } 241 242 243 248 public SubmitMethod getLastMethod() { 249 return lastMethod_; 250 } 251 252 253 258 public List getLastParameters() { 259 return lastParameters_; 260 } 261 262 263 273 public void setResponse( 274 final URL url, 275 final String content, 276 final int statusCode, 277 final String statusMessage, 278 final String contentType, 279 final List responseHeaders ) { 280 281 setResponse(url, stringToByteArray(content), statusCode, statusMessage, 282 contentType, responseHeaders); 283 284 } 285 286 296 public void setResponse( 297 final URL url, 298 final byte[] content, 299 final int statusCode, 300 final String statusMessage, 301 final String contentType, 302 final List responseHeaders ) { 303 304 final ResponseEntry responseEntry = 305 new ResponseEntry(content, statusCode, statusMessage, contentType, responseHeaders); 306 responseMap_.put( url.toExternalForm(), responseEntry ); 307 } 308 309 317 public void setResponse( final URL url, final String content ) { 318 setResponse( url, content, 200, "OK", "text/html", Collections.EMPTY_LIST ); 319 } 320 321 330 public void setResponse(final URL url, final String content, final String contentType) { 331 setResponse( url, content, 200, "OK", contentType, Collections.EMPTY_LIST ); 332 } 333 334 342 public void setResponseAsGenericHtml( 343 final URL url, 344 final String title ) { 345 346 final String content = "<html><head><title>"+title+"</title></head><body></body></html>"; 347 final ResponseEntry responseEntry 348 = new ResponseEntry(stringToByteArray(content), 200, "OK", "text/html", Collections.EMPTY_LIST); 349 responseMap_.put( url.toExternalForm(), responseEntry ); 350 } 351 352 361 public void setDefaultResponse( 362 final String content, 363 final int statusCode, 364 final String statusMessage, 365 final String contentType ) { 366 367 setDefaultResponse(stringToByteArray(content), statusCode, statusMessage, contentType); 368 } 369 370 379 public void setDefaultResponse( 380 final byte[] content, 381 final int statusCode, 382 final String statusMessage, 383 final String contentType ) { 384 385 defaultResponseEntry_ = new ResponseEntry(content, statusCode, statusMessage, 386 contentType, Collections.EMPTY_LIST); 387 } 388 389 395 public void setDefaultResponse( final String content ) { 396 setDefaultResponse(content, 200, "OK", "text/html"); 397 } 398 399 400 405 public HttpState getStateForUrl( final URL url ) { 406 return httpState_; 407 } 408 } 409 410 | Popular Tags |