1 38 package com.gargoylesoftware.htmlunit.jelly; 39 40 import java.net.MalformedURLException ; 41 import java.net.URL ; 42 import java.util.Collections ; 43 import java.util.List ; 44 45 import org.apache.commons.jelly.JellyTagException; 46 import org.apache.commons.jelly.XMLOutput; 47 48 import com.gargoylesoftware.htmlunit.MockWebConnection; 49 import com.gargoylesoftware.htmlunit.WebConnection; 50 51 56 public class MockResponseTag extends HtmlUnitTagSupport { 57 private String url_ = ""; 58 private final int statusCode_ = 200; 59 private final String statusMessage_ = "OK"; 60 private final String contentType_ = "text/html"; 61 private final List responseHeaders_ = Collections.EMPTY_LIST; 62 63 68 public void doTag(final XMLOutput xmlOutput) throws JellyTagException { 69 final String content = getBodyText(); 70 final URL newUrl; 71 try { 72 newUrl = new URL (url_); 73 } 74 catch( final MalformedURLException e ) { 75 throw new JellyTagException("Malformed URL: "+url_); 76 } 77 getMockWebConnection().setResponse( 78 newUrl, content, statusCode_, statusMessage_, contentType_, responseHeaders_); 79 } 80 81 private MockWebConnection getMockWebConnection() throws JellyTagException { 82 final WebConnection webConnection = getWebClient().getWebConnection(); 83 if( webConnection instanceof MockWebConnection ) { 84 return (MockWebConnection)webConnection; 85 } 86 throw new JellyTagException( 87 "WebClient is not using a MockWebConnection - have you used the mockWebConnection tag?"); 88 } 89 90 94 public void setUrl( final String url ) { 95 url_ = url; 96 } 97 } 98 99 | Popular Tags |