public class MockWebConnection extends Object implements WebConnection
WebConnection
designed to mock out the actual HTTP connections.Modifier and Type | Class and Description |
---|---|
static class |
MockWebConnection.RawResponseData
Contains the raw data configured for a response.
|
Constructor and Description |
---|
MockWebConnection() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Resets this.
|
void |
close() |
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) . |
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.
|
List<URL> |
getRequestedUrls()
Gets the list of requested URLs.
|
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,
Charset charset,
List<NameValuePair> headers)
Sets the response that will be returned when the specified URL is requested.
|
void |
setDefaultResponse(String content,
int statusCode,
String statusMessage,
String contentType,
List<NameValuePair> headers)
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,
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(URL url,
byte[] content,
int statusCode,
String statusMessage,
String contentType,
List<NameValuePair> headers)
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,
Charset charset,
List<NameValuePair> headers)
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,
List<NameValuePair> headers)
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,
Charset charset)
Convenient method that is the same as calling
setResponse(URL, String, int, String, String, Charset, 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.
|
void |
setThrowable(URL url,
IOException throwable)
Sets the exception that will be thrown when the specified URL is requested.
|
public WebResponse getResponse(WebRequest request) throws IOException
getResponse
in interface WebConnection
request
- the requestIOException
- if an IO error occurspublic MockWebConnection.RawResponseData getRawResponse(WebRequest request) throws IOException
request
- the requestIOException
- if definedpublic List<URL> getRequestedUrls()
public List<String> getRequestedUrls(URL relativeTo)
relativeTo
- what should be removed from the requested URLs.public HttpMethod getLastMethod()
public List<NameValuePair> getLastParameters()
public void setResponse(URL url, String content, int statusCode, String statusMessage, String contentType, List<NameValuePair> headers)
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 returnpublic void setResponse(URL url, String content, int statusCode, String statusMessage, String contentType, Charset charset, List<NameValuePair> headers)
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 returnpublic void setThrowable(URL url, IOException throwable)
url
- the URL that will force the exceptionthrowable
- the Throwablepublic void setResponse(URL url, byte[] content, int statusCode, String statusMessage, String contentType, List<NameValuePair> headers)
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 returnpublic void setResponse(URL url, String content)
setResponse(URL,String,int,String,String,List)
with a status
of "200 OK", a content type of "text/html" and no additional headers.url
- the URL that will return the given responsecontent
- the content to returnpublic void setResponse(URL url, String content, String contentType)
setResponse(URL,String,int,String,String,List)
with a status
of "200 OK" and no additional headers.url
- the URL that will return the given responsecontent
- the content to returncontentType
- the content type to returnpublic void setResponse(URL url, String content, String contentType, Charset charset)
setResponse(URL, String, int, String, String, Charset, List)
with a status
of "200 OK" and no additional headers.url
- the URL that will return the given responsecontent
- the content to returncontentType
- the content type to returncharset
- the charsetpublic void setResponseAsGenericHtml(URL url, String title)
url
- the URL that will return the given responsetitle
- the title of the pagepublic void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType)
content
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnpublic void setDefaultResponse(byte[] content, int statusCode, String statusMessage, String contentType)
content
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnpublic void setDefaultResponse(String content)
content
- the content to returnpublic void setDefaultResponse(String content, String contentType)
content
- the content to returncontentType
- the content type to returnpublic void setDefaultResponse(String content, String contentType, Charset charset)
content
- the content to returncontentType
- the content type to returncharset
- the charsetpublic void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType, List<NameValuePair> headers)
content
- the content to returnstatusCode
- the status code to returnstatusMessage
- the status message to returncontentType
- the content type to returnheaders
- the response headers to returnpublic void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType, Charset charset, List<NameValuePair> headers)
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 returnpublic Map<String,String> getLastAdditionalHeaders()
getResponse(WebRequest)
.getResponse(WebRequest)
public WebRequest getLastWebRequest()
WebRequest
that was used in the in the last call
to getResponse(WebRequest)
.WebRequest
that was used in the in the last call
to getResponse(WebRequest)
public int getRequestCount()
public boolean hasResponse(URL url)
url
- the urlfalse
if no response has been configuredpublic void close()
close
in interface AutoCloseable
close
in interface WebConnection
public void clear()
Copyright © 2002–2024 Gargoyle Software Inc.. All rights reserved.