- All Implemented Interfaces:
AutoCloseable,WebConnection
WebConnection designed to mock out the actual HTTP connections.- Author:
- Mike Bowler, Noboru Sinohara, Marc Guillemot, Brad Clarke, Ahmed Ashour, Ronald Brill
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classContains the raw data configured for a response. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Resets all state: clears all configured responses and throwables, the default response, the last request, the request count, and the list of requested URLs.voidclose()Delegates toclear(), resetting all configured responses, recorded requests, and request counts.Returns the additional headers that were used in the last call togetResponse(WebRequest).Returns the HTTP method that was used in the last call togetResponse(WebRequest).Returns the request parameters that were used in the last call togetResponse(WebRequest).Returns theWebRequestthat was used in the last call togetResponse(WebRequest).getRawResponse(WebRequest request) Returns the raw response configured for the given request.intReturns the number of requests made to this mock web connection.Returns an unmodifiable list of all URLs requested so far, in request order.getRequestedUrls(URL relativeTo) Returns an unmodifiable list of requested URLs relativized against the given base URL.getResponse(WebRequest request) Submits a request and retrieves a response.booleanhasResponse(URL url) Returns whether a response has been configured for the given URL.voidsetDefaultResponse(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 response configured for it.voidsetDefaultResponse(String content) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.voidsetDefaultResponse(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 response configured for it.voidsetDefaultResponse(String content, int statusCode, String statusMessage, String contentType, Charset charset, List<NameValuePair> headers) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.voidsetDefaultResponse(String content, int statusCode, String statusMessage, String contentType, List<NameValuePair> headers) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.voidsetDefaultResponse(String content, String contentType) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.voidsetDefaultResponse(String content, String contentType, Charset charset) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.voidsetResponse(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.voidsetResponse(URL url, 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.voidsetResponse(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.voidsetResponse(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.voidsetResponse(URL url, String content, 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.voidsetResponse(URL url, String content, String contentType, 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.voidsetResponseAsGenericHtml(URL url, String title) Specify a generic HTML page that will be returned when the given URL is specified.voidsetThrowable(URL url, IOException throwable) Sets the exception that will be thrown when the specified URL is requested.
-
Constructor Details
-
MockWebConnection
public MockWebConnection()
-
-
Method Details
-
getResponse
Submits a request and retrieves a response.- Specified by:
getResponsein interfaceWebConnection- Parameters:
request- the request- Returns:
- the response to the request defined by the specified request
- Throws:
IOException- if an IO error occurs
-
getRawResponse
Returns the raw response configured for the given request.The request is always recorded (incrementing
getRequestCount()and appending togetRequestedUrls()) before any configuredIOExceptionis thrown, mirroring real HTTP behaviour where the request was dispatched even if the connection subsequently failed.URL lookup first tries an exact match (including query string), then retries without the query string, then falls back to the default response. If no default has been set an
IllegalStateExceptionis thrown.- Parameters:
request- the request- Returns:
- the raw response
- Throws:
IOException- if anIOExceptionhas been registered for the URL viasetThrowable(URL, IOException)IllegalStateException- if no response or default response is configured for the URL
-
getRequestedUrls
Returns an unmodifiable list of all URLs requested so far, in request order.- Returns:
- the list of requested URLs
-
getRequestedUrls
Returns an unmodifiable list of requested URLs relativized against the given base URL. If a requested URL starts withrelativeTo, the base is stripped; otherwise the full URL string is returned as-is.Note: the base URL string is compared as a plain prefix. Ensure
relativeTohas a trailing slash if needed to avoid unintended partial matches (e.g.http://localhost/rather thanhttp://localhost).- Parameters:
relativeTo- the base URL whose prefix should be stripped from each requested URL- Returns:
- the list of relative (or absolute, if not matching) URL strings
-
getLastMethod
Returns the HTTP method that was used in the last call togetResponse(WebRequest).- Returns:
- the HTTP method of the last request
- Throws:
IllegalStateException- if no request has been made yet
-
getLastParameters
Returns the request parameters that were used in the last call togetResponse(WebRequest).- Returns:
- the parameters of the last request
- Throws:
IllegalStateException- if no request has been made yet
-
setResponse
public 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.- 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(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.- 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
Sets the exception that will be thrown when the specified URL is requested.The stored exception is wrapped at throw time so that the stack trace points to the actual call site rather than to where the exception was constructed.
- Parameters:
url- the URL that will force the exceptionthrowable- theIOExceptionto throw
-
setResponse
public 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.- 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
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
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
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
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(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 response configured 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, String statusMessage, String contentType) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- Parameters:
content- the content to returnstatusCode- the status code to returnstatusMessage- the status message to returncontentType- the content type to return
-
setDefaultResponse
Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- Parameters:
content- the content to return
-
setDefaultResponse
Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- Parameters:
content- the content to returncontentType- the content type to return
-
setDefaultResponse
Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- Parameters:
content- the content to returncontentType- the content type to returncharset- the charset
-
setDefaultResponse
public void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType, List<NameValuePair> headers) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- 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(String content, int statusCode, String statusMessage, String contentType, Charset charset, List<NameValuePair> headers) Sets the response that will be returned when a URL is requested that does not have a specific response configured for it.- 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
Returns the additional headers that were used in the last call togetResponse(WebRequest).- Returns:
- the additional headers of the last request
- Throws:
IllegalStateException- if no request has been made yet
-
getLastWebRequest
Returns theWebRequestthat was used in the last call togetResponse(WebRequest).- Returns:
- the last
WebRequest - Throws:
IllegalStateException- if no request has been made yet
-
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
Returns whether a response has been configured for the given URL.- Parameters:
url- the URL to check- Returns:
trueif a response has been configured for the URL;falseotherwise
-
close
public void close()Delegates toclear(), resetting all configured responses, recorded requests, and request counts.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceWebConnection
-
clear
public void clear()Resets all state: clears all configured responses and throwables, the default response, the last request, the request count, and the list of requested URLs.Note:
in a try-with-resources block will reset all configured state on exit.close()delegates to this method, so using this connection
-