com.gargoylesoftware.htmlunit
Class MockWebConnection

java.lang.Object
  extended by com.gargoylesoftware.htmlunit.MockWebConnection
All Implemented Interfaces:
WebConnection

public class MockWebConnection
extends Object
implements WebConnection

A fake WebConnection designed to mock out the actual HTTP connections.

Version:
$Revision: 5834 $
Author:
Mike Bowler, Noboru Sinohara, Marc Guillemot, Brad Clarke, Ahmed Ashour

Constructor Summary
MockWebConnection()
           
 
Method Summary
 Map<String,String> getLastAdditionalHeaders()
          Returns the additional headers that were used in the in the last call to getResponse(WebRequest).
 HttpMethod getLastMethod()
          Returns the method that was used in the last call to submitRequest().
 List<NameValuePair> getLastParameters()
          Returns the parameters that were used in the last call to submitRequest().
 WebRequest getLastWebRequest()
          Returns the WebRequest that was used in the in the last call to getResponse(WebRequest).
 WebRequest getLastWebRequestSettings()
          Deprecated. as of 2.8, please use getLastWebRequest() instead
 int getRequestCount()
          Returns the number of requests made to this mock web connection.
 List<String> getRequestedUrls(URL relativeTo)
          Gets the list of requested URLs relative to the provided URL.
 WebResponse getResponse(WebRequest request)
          Submits a request and retrieves a response.
 boolean hasResponse(URL url)
          Indicates if a response has already been configured for this URL.
 void setDefaultResponse(byte[] content, int statusCode, String statusMessage, String contentType)
          Sets the response that will be returned when a URL is requested that does not have a specific content set for it.
 void setDefaultResponse(String content)
          Sets the response that will be returned when a URL is requested that does not have a specific content set for it.
 void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType)
          Sets the response that will be returned when a URL is requested that does not have a specific content set for it.
 void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType, List<? extends NameValuePair> responseHeaders)
          Sets the response that will be returned when the specified URL is requested.
 void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType, String charset, List<? extends NameValuePair> responseHeaders)
          Sets the response that will be returned when the specified URL is requested.
 void setDefaultResponse(String content, String contentType)
          Sets the response that will be returned when a URL is requested that does not have a specific content set for it.
 void setDefaultResponse(String content, String contentType, String charset)
          Sets the response that will be returned when a URL is requested that does not have a specific content set for it.
 void setResponse(URL url, byte[] content, int statusCode, String statusMessage, String contentType, List<? extends NameValuePair> responseHeaders)
          Sets the response that will be returned when the specified URL is requested.
 void setResponse(URL url, String content)
          Convenient method that is the same as calling setResponse(URL,String,int,String,String,List) with a status of "200 OK", a content type of "text/html" and no additional headers.
 void setResponse(URL url, String content, int statusCode, String statusMessage, String contentType, List<? extends NameValuePair> responseHeaders)
          Sets the response that will be returned when the specified URL is requested.
 void setResponse(URL url, String content, int statusCode, String statusMessage, String contentType, String charset, List<? extends NameValuePair> responseHeaders)
          Sets the response that will be returned when the specified URL is requested.
 void setResponse(URL url, String content, String contentType)
          Convenient method that is the same as calling setResponse(URL,String,int,String,String,List) with a status of "200 OK" and no additional headers.
 void setResponse(URL url, String content, String contentType, String charset)
          Convenient method that is the same as calling setResponse(URL,String,int,String,String,String,List) with a status of "200 OK" and no additional headers.
 void setResponseAsGenericHtml(URL url, String title)
          Specify a generic HTML page that will be returned when the given URL is specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockWebConnection

public MockWebConnection()
Method Detail

getResponse

public WebResponse getResponse(WebRequest request)
                        throws IOException
Submits a request and retrieves a response.

Specified by:
getResponse in interface WebConnection
Parameters:
request - the request
Returns:
the response to the request defined by the specified request
Throws:
IOException - if an IO error occurs

getRequestedUrls

public List<String> getRequestedUrls(URL relativeTo)
Gets the list of requested URLs relative to the provided URL.

Parameters:
relativeTo - what should be removed from the requested URLs.
Returns:
the list of relative URLs

getLastMethod

public HttpMethod getLastMethod()
Returns the method that was used in the last call to submitRequest().

Returns:
the method that was used in the last call to submitRequest()

getLastParameters

public List<NameValuePair> getLastParameters()
Returns the parameters that were used in the last call to submitRequest().

Returns:
the parameters that were used in the last call to submitRequest()

setResponse

public void setResponse(URL url,
                        String content,
                        int statusCode,
                        String statusMessage,
                        String contentType,
                        List<? extends NameValuePair> responseHeaders)
Sets the response that will be returned when the specified URL is requested.

Parameters:
url - the URL that will return the given response
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return
responseHeaders - the response headers to return

setResponse

public void setResponse(URL url,
                        String content,
                        int statusCode,
                        String statusMessage,
                        String contentType,
                        String charset,
                        List<? extends NameValuePair> responseHeaders)
Sets the response that will be returned when the specified URL is requested.

Parameters:
url - the URL that will return the given response
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return
charset - the name of a supported charset
responseHeaders - the response headers to return

setResponse

public void setResponse(URL url,
                        byte[] content,
                        int statusCode,
                        String statusMessage,
                        String contentType,
                        List<? extends NameValuePair> responseHeaders)
Sets the response that will be returned when the specified URL is requested.

Parameters:
url - the URL that will return the given response
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return
responseHeaders - the response headers to return

setResponse

public void setResponse(URL url,
                        String content)
Convenient method that is the same as calling setResponse(URL,String,int,String,String,List) with a status of "200 OK", a content type of "text/html" and no additional headers.

Parameters:
url - the URL that will return the given response
content - the content to return

setResponse

public void setResponse(URL url,
                        String content,
                        String contentType)
Convenient method that is the same as calling setResponse(URL,String,int,String,String,List) with a status of "200 OK" and no additional headers.

Parameters:
url - the URL that will return the given response
content - the content to return
contentType - the content type to return

setResponse

public void setResponse(URL url,
                        String content,
                        String contentType,
                        String charset)
Convenient method that is the same as calling setResponse(URL,String,int,String,String,String,List) with a status of "200 OK" and no additional headers.

Parameters:
url - the URL that will return the given response
content - the content to return
contentType - the content type to return
charset - the name of a supported charset

setResponseAsGenericHtml

public void setResponseAsGenericHtml(URL url,
                                     String title)
Specify a generic HTML page that will be returned when the given URL is specified. The page will contain only minimal HTML to satisfy the HTML parser but will contain the specified title so that tests can check for titleText.

Parameters:
url - the URL that will return the given response
title - the title of the page

setDefaultResponse

public void setDefaultResponse(String content,
                               int statusCode,
                               String statusMessage,
                               String contentType)
Sets the response that will be returned when a URL is requested that does not have a specific content set for it.

Parameters:
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return

setDefaultResponse

public void setDefaultResponse(byte[] content,
                               int statusCode,
                               String statusMessage,
                               String contentType)
Sets the response that will be returned when a URL is requested that does not have a specific content set for it.

Parameters:
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return

setDefaultResponse

public void setDefaultResponse(String content)
Sets the response that will be returned when a URL is requested that does not have a specific content set for it.

Parameters:
content - the content to return

setDefaultResponse

public void setDefaultResponse(String content,
                               String contentType)
Sets the response that will be returned when a URL is requested that does not have a specific content set for it.

Parameters:
content - the content to return
contentType - the content type to return

setDefaultResponse

public void setDefaultResponse(String content,
                               String contentType,
                               String charset)
Sets the response that will be returned when a URL is requested that does not have a specific content set for it.

Parameters:
content - the content to return
contentType - the content type to return
charset - the name of a supported charset

setDefaultResponse

public void setDefaultResponse(String content,
                               int statusCode,
                               String statusMessage,
                               String contentType,
                               List<? extends NameValuePair> responseHeaders)
Sets the response that will be returned when the specified URL is requested.

Parameters:
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return
responseHeaders - the response headers to return

setDefaultResponse

public void setDefaultResponse(String content,
                               int statusCode,
                               String statusMessage,
                               String contentType,
                               String charset,
                               List<? extends NameValuePair> responseHeaders)
Sets the response that will be returned when the specified URL is requested.

Parameters:
content - the content to return
statusCode - the status code to return
statusMessage - the status message to return
contentType - the content type to return
charset - the name of a supported charset
responseHeaders - the response headers to return

getLastAdditionalHeaders

public Map<String,String> getLastAdditionalHeaders()
Returns the additional headers that were used in the in the last call to getResponse(WebRequest).

Returns:
the additional headers that were used in the in the last call to getResponse(WebRequest)

getLastWebRequestSettings

@Deprecated
public WebRequest getLastWebRequestSettings()
Deprecated. as of 2.8, please use getLastWebRequest() instead

Returns the WebRequest that was used in the in the last call to getResponse(WebRequest).

Returns:
the WebRequest that was used in the in the last call to getResponse(WebRequest)

getLastWebRequest

public WebRequest getLastWebRequest()
Returns the WebRequest that was used in the in the last call to getResponse(WebRequest).

Returns:
the WebRequest that was used in the in the last call to getResponse(WebRequest)

getRequestCount

public int getRequestCount()
Returns the number of requests made to this mock web connection.

Returns:
the number of requests made to this mock web connection

hasResponse

public boolean hasResponse(URL url)
Indicates if a response has already been configured for this URL.

Parameters:
url - the url
Returns:
false if no response has been configured


Copyright © 2002-2010 Gargoyle Software Inc.. All Rights Reserved.