Package org.htmlunit
Class MockWebConnection
- java.lang.Object
-
- org.htmlunit.MockWebConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,WebConnection
public class MockWebConnection extends java.lang.Object implements WebConnection
A fakeWebConnection
designed to mock out the actual HTTP connections.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MockWebConnection.RawResponseData
Contains the raw data configured for a response.
-
Constructor Summary
Constructors Constructor Description MockWebConnection()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Resets this.void
close()
java.util.Map<java.lang.String,java.lang.String>
getLastAdditionalHeaders()
Returns the additional headers that were used in the in the last call togetResponse(WebRequest)
.HttpMethod
getLastMethod()
Returns the method that was used in the last call to submitRequest().java.util.List<NameValuePair>
getLastParameters()
Returns the parameters that were used in the last call to submitRequest().WebRequest
getLastWebRequest()
Returns theWebRequest
that was used in the in the last call togetResponse(WebRequest)
.MockWebConnection.RawResponseData
getRawResponse(WebRequest request)
Gets the raw response configured for the request.int
getRequestCount()
Returns the number of requests made to this mock web connection.java.util.List<java.net.URL>
getRequestedUrls()
Gets the list of requested URLs.java.util.List<java.lang.String>
getRequestedUrls(java.net.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(java.net.URL url)
Indicates if a response has already been configured for this URL.void
setDefaultResponse(byte[] content, int statusCode, java.lang.String statusMessage, java.lang.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(java.lang.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(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.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(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.nio.charset.Charset charset, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.void
setDefaultResponse(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.void
setDefaultResponse(java.lang.String content, java.lang.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(java.lang.String content, java.lang.String contentType, java.nio.charset.Charset 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(java.net.URL url, byte[] content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.void
setResponse(java.net.URL url, java.lang.String content)
Convenient method that is the same as callingsetResponse(URL,String,int,String,String,List)
with a status of "200 OK", a content type of "text/html" and no additional headers.void
setResponse(java.net.URL url, java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.nio.charset.Charset charset, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.void
setResponse(java.net.URL url, java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.void
setResponse(java.net.URL url, java.lang.String content, java.lang.String contentType)
Convenient method that is the same as callingsetResponse(URL,String,int,String,String,List)
with a status of "200 OK" and no additional headers.void
setResponse(java.net.URL url, java.lang.String content, java.lang.String contentType, java.nio.charset.Charset charset)
Convenient method that is the same as callingsetResponse(URL, String, int, String, String, Charset, List)
with a status of "200 OK" and no additional headers.void
setResponseAsGenericHtml(java.net.URL url, java.lang.String title)
Specify a generic HTML page that will be returned when the given URL is specified.void
setThrowable(java.net.URL url, java.io.IOException throwable)
Sets the exception that will be thrown when the specified URL is requested.
-
-
-
Method Detail
-
getResponse
public WebResponse getResponse(WebRequest request) throws java.io.IOException
Submits a request and retrieves a response.- Specified by:
getResponse
in interfaceWebConnection
- Parameters:
request
- the request- Returns:
- the response to the request defined by the specified request
- Throws:
java.io.IOException
- if an IO error occurs
-
getRawResponse
public MockWebConnection.RawResponseData getRawResponse(WebRequest request) throws java.io.IOException
Gets the raw response configured for the request.- Parameters:
request
- the request- Returns:
- the raw response
- Throws:
java.io.IOException
- if defined
-
getRequestedUrls
public java.util.List<java.net.URL> getRequestedUrls()
Gets the list of requested URLs.- Returns:
- the list of relative URLs
-
getRequestedUrls
public java.util.List<java.lang.String> getRequestedUrls(java.net.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 java.util.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(java.net.URL url, java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.- Parameters:
url
- the URL that will return the given responsecontent
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnheaders
- the response headers to return
-
setResponse
public void setResponse(java.net.URL url, java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.nio.charset.Charset charset, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.- Parameters:
url
- the URL that will return the given responsecontent
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returncharset
- the charsetheaders
- the response headers to return
-
setThrowable
public void setThrowable(java.net.URL url, java.io.IOException throwable)
Sets the exception that will be thrown when the specified URL is requested.- Parameters:
url
- the URL that will force the exceptionthrowable
- the Throwable
-
setResponse
public void setResponse(java.net.URL url, byte[] content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.- Parameters:
url
- the URL that will return the given responsecontent
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnheaders
- the response headers to return
-
setResponse
public void setResponse(java.net.URL url, java.lang.String content)
Convenient method that is the same as callingsetResponse(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 responsecontent
- the content to return
-
setResponse
public void setResponse(java.net.URL url, java.lang.String content, java.lang.String contentType)
Convenient method that is the same as callingsetResponse(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 responsecontent
- the content to returncontentType
- the content type to return
-
setResponse
public void setResponse(java.net.URL url, java.lang.String content, java.lang.String contentType, java.nio.charset.Charset charset)
Convenient method that is the same as callingsetResponse(URL, String, int, String, String, Charset, List)
with a status of "200 OK" and no additional headers.- Parameters:
url
- the URL that will return the given responsecontent
- the content to returncontentType
- the content type to returncharset
- the charset
-
setResponseAsGenericHtml
public void setResponseAsGenericHtml(java.net.URL url, java.lang.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 responsetitle
- the title of the page
-
setDefaultResponse
public void setDefaultResponse(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.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 returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to return
-
setDefaultResponse
public void setDefaultResponse(byte[] content, int statusCode, java.lang.String statusMessage, java.lang.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 returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to return
-
setDefaultResponse
public void setDefaultResponse(java.lang.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(java.lang.String content, java.lang.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 returncontentType
- the content type to return
-
setDefaultResponse
public void setDefaultResponse(java.lang.String content, java.lang.String contentType, java.nio.charset.Charset 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 returncontentType
- the content type to returncharset
- the charset
-
setDefaultResponse
public void setDefaultResponse(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.- Parameters:
content
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnheaders
- the response headers to return
-
setDefaultResponse
public void setDefaultResponse(java.lang.String content, int statusCode, java.lang.String statusMessage, java.lang.String contentType, java.nio.charset.Charset charset, java.util.List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.- Parameters:
content
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returncharset
- the charsetheaders
- the response headers to return
-
getLastAdditionalHeaders
public java.util.Map<java.lang.String,java.lang.String> getLastAdditionalHeaders()
Returns the additional headers that were used in the in the last call togetResponse(WebRequest)
.- Returns:
- the additional headers that were used in the in the last call
to
getResponse(WebRequest)
-
getLastWebRequest
public WebRequest getLastWebRequest()
Returns theWebRequest
that was used in the in the last call togetResponse(WebRequest)
.- Returns:
- the
WebRequest
that was used in the in the last call togetResponse(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(java.net.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
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceWebConnection
-
clear
public void clear()
Resets this.
-
-