Package org.mockserver.model
Class HttpResponse
- java.lang.Object
-
- org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
-
- org.mockserver.model.ObjectWithJsonToString
-
- org.mockserver.model.Action<HttpResponse>
-
- org.mockserver.model.HttpResponse
-
- All Implemented Interfaces:
HttpObject<HttpResponse,BodyWithContentType>
public class HttpResponse extends Action<HttpResponse> implements HttpObject<HttpResponse,BodyWithContentType>
- Author:
- jamesdbloom
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.mockserver.model.Action
Action.Direction, Action.Type
-
-
Constructor Summary
Constructors Constructor Description HttpResponse()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpResponse
clone()
boolean
containsHeader(String name)
Returns true if a header with the specified name has been addedboolean
containsHeader(String name, String value)
Returns true if a header with the specified name has been addedboolean
cookieHeadeDoesNotAlreadyExists(String name, String value)
boolean
cookieHeadeDoesNotAlreadyExists(Cookie cookieValue)
BodyWithContentType
getBody()
byte[]
getBodyAsRawBytes()
String
getBodyAsString()
ConnectionOptions
getConnectionOptions()
List<Cookie>
getCookieList()
Map<NottableString,NottableString>
getCookieMap()
Cookies
getCookies()
String
getFirstHeader(String name)
List<String>
getHeader(String name)
List<Header>
getHeaderList()
com.google.common.collect.Multimap<NottableString,NottableString>
getHeaderMultimap()
Headers
getHeaders()
String
getReasonPhrase()
Integer
getStatusCode()
Action.Type
getType()
static HttpResponse
notFoundResponse()
Static builder to create a not found response.HttpResponse
removeHeader(String name)
HttpResponse
removeHeader(NottableString name)
HttpResponse
replaceHeader(String name, String... values)
Update header to return as a Header object, if a header with the same name already exists it will be modifiedHttpResponse
replaceHeader(Header header)
Update header to return as a Header object, if a header with the same name already exists it will be modifiedstatic HttpResponse
response()
Static builder to create a response.static HttpResponse
response(String body)
Static builder to create a response with a 200 status code and the string response body.HttpResponse
withBody(byte[] body)
Set response body to return as binary such as a pdf or imageHttpResponse
withBody(String body)
Set response body to return as a string response body.HttpResponse
withBody(String body, com.google.common.net.MediaType contentType)
Set response body to return a string response body with the specified encoding.HttpResponse
withBody(String body, Charset charset)
Set response body to return a string response body with the specified encoding.HttpResponse
withBody(BodyWithContentType body)
Set the body to return for example:HttpResponse
withConnectionOptions(ConnectionOptions connectionOptions)
The connection options for override the default connection behaviour, this allows full control of headers such as "Connection" or "Content-Length" or controlling whether the socket is closed after the response has been sentHttpResponse
withCookie(String name, String value)
Add cookie to return as Set-Cookie headerHttpResponse
withCookie(Cookie cookie)
Add cookie to return as Set-Cookie headerHttpResponse
withCookie(NottableString name, NottableString value)
Adds one cookie to match on or to not match on using the NottableString, each NottableString can either be a positive matching value, such as string("match"), or a value to not match on, such as not("do not match"), the string values passed to the NottableString can be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpResponse
withCookies(List<Cookie> cookies)
The cookies to return as Set-Cookie headers as a list of Cookie objectsHttpResponse
withCookies(Cookie... cookies)
The cookies to return as Set-Cookie headers as a varargs of Cookie objectsHttpResponse
withCookies(Cookies cookies)
HttpResponse
withHeader(String name, String... values)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will existHttpResponse
withHeader(Header header)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will existHttpResponse
withHeader(NottableString name, NottableString... values)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will existHttpResponse
withHeaders(List<Header> headers)
The headers to return as a list of Header objectsHttpResponse
withHeaders(Header... headers)
The headers to return as a varargs of Header objectsHttpResponse
withHeaders(Headers headers)
HttpResponse
withReasonPhrase(String reasonPhrase)
The reason phrase to return, if no reason code is returned this will be defaulted to the standard reason phrase for the statusCode, i.e.HttpResponse
withStatusCode(Integer statusCode)
The status code to return, such as 200, 404, the status code specified here will result in the default status message for this status code for example for 200 the status message "OK" is used-
Methods inherited from class org.mockserver.model.ObjectWithJsonToString
toString
-
Methods inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
equals, fieldsExcludedFromEqualsAndHashCode, hashCode, key
-
-
-
-
Method Detail
-
response
public static HttpResponse response()
Static builder to create a response.
-
response
public static HttpResponse response(String body)
Static builder to create a response with a 200 status code and the string response body.- Parameters:
body
- a string
-
notFoundResponse
public static HttpResponse notFoundResponse()
Static builder to create a not found response.
-
withStatusCode
public HttpResponse withStatusCode(Integer statusCode)
The status code to return, such as 200, 404, the status code specified here will result in the default status message for this status code for example for 200 the status message "OK" is used- Parameters:
statusCode
- an integer such as 200 or 404
-
getStatusCode
public Integer getStatusCode()
-
withReasonPhrase
public HttpResponse withReasonPhrase(String reasonPhrase)
The reason phrase to return, if no reason code is returned this will be defaulted to the standard reason phrase for the statusCode, i.e. for a statusCode of 200 the standard reason phrase is "OK"- Parameters:
reasonPhrase
- an string such as "Not Found" or "OK"
-
getReasonPhrase
public String getReasonPhrase()
-
withBody
public HttpResponse withBody(String body)
Set response body to return as a string response body. The character set will be determined by the Content-Type header on the response. To force the character set, usewithBody(String, Charset)
.- Specified by:
withBody
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
body
- a string
-
withBody
public HttpResponse withBody(String body, Charset charset)
Set response body to return a string response body with the specified encoding. Note: The character set of the response will be forced to the specified charset, even if the Content-Type header specifies otherwise.- Specified by:
withBody
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
body
- a stringcharset
- character set the string will be encoded in
-
withBody
public HttpResponse withBody(String body, com.google.common.net.MediaType contentType)
Set response body to return a string response body with the specified encoding. Note: The character set of the response will be forced to the specified charset, even if the Content-Type header specifies otherwise.- Parameters:
body
- a stringcontentType
- media type, if charset is included this will be used for encoding string
-
withBody
public HttpResponse withBody(byte[] body)
Set response body to return as binary such as a pdf or image- Specified by:
withBody
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
body
- a byte array
-
withBody
public HttpResponse withBody(BodyWithContentType body)
Set the body to return for example: string body: - exact("a simple string body"); or - new StringBody("
a simple string body") binary body: - binary(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024)); or - new BinaryBody(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024)); -
-
- Specified by:
withBody
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
body
- an instance of one of the Body subclasses including StringBody or BinaryBody
-
getBody
public BodyWithContentType getBody()
- Specified by:
getBody
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getBodyAsRawBytes
public byte[] getBodyAsRawBytes()
- Specified by:
getBodyAsRawBytes
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getBodyAsString
public String getBodyAsString()
- Specified by:
getBodyAsString
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getHeaders
public Headers getHeaders()
- Specified by:
getHeaders
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withHeaders
public HttpResponse withHeaders(Headers headers)
- Specified by:
withHeaders
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withHeaders
public HttpResponse withHeaders(List<Header> headers)
The headers to return as a list of Header objects- Specified by:
withHeaders
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
headers
- a list of Header objects
-
withHeaders
public HttpResponse withHeaders(Header... headers)
The headers to return as a varargs of Header objects- Specified by:
withHeaders
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
headers
- varargs of Header objects
-
withHeader
public HttpResponse withHeader(Header header)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist- Specified by:
withHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
header
- a Header object
-
withHeader
public HttpResponse withHeader(String name, String... values)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist- Specified by:
withHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
name
- the header namevalues
- the header values
-
withHeader
public HttpResponse withHeader(NottableString name, NottableString... values)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist- Specified by:
withHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
name
- the header name as a NottableStringvalues
- the header values which can be a varags of NottableStrings
-
replaceHeader
public HttpResponse replaceHeader(Header header)
Update header to return as a Header object, if a header with the same name already exists it will be modified- Specified by:
replaceHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
header
- a Header object
-
replaceHeader
public HttpResponse replaceHeader(String name, String... values)
Update header to return as a Header object, if a header with the same name already exists it will be modified- Parameters:
name
- the header namevalues
- the header values
-
getHeaderList
public List<Header> getHeaderList()
- Specified by:
getHeaderList
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getHeaderMultimap
public com.google.common.collect.Multimap<NottableString,NottableString> getHeaderMultimap()
-
getHeader
public List<String> getHeader(String name)
- Specified by:
getHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getFirstHeader
public String getFirstHeader(String name)
- Specified by:
getFirstHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
containsHeader
public boolean containsHeader(String name)
Returns true if a header with the specified name has been added- Specified by:
containsHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
name
- the header name- Returns:
- true if a header has been added with that name otherwise false
-
removeHeader
public HttpResponse removeHeader(String name)
- Specified by:
removeHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
removeHeader
public HttpResponse removeHeader(NottableString name)
- Specified by:
removeHeader
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
containsHeader
public boolean containsHeader(String name, String value)
Returns true if a header with the specified name has been added- Parameters:
name
- the header namevalue
- the header value- Returns:
- true if a header has been added with that name otherwise false
-
getCookies
public Cookies getCookies()
- Specified by:
getCookies
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withCookies
public HttpResponse withCookies(Cookies cookies)
- Specified by:
withCookies
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withCookies
public HttpResponse withCookies(List<Cookie> cookies)
The cookies to return as Set-Cookie headers as a list of Cookie objects- Specified by:
withCookies
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
cookies
- a list of Cookie objects
-
withCookies
public HttpResponse withCookies(Cookie... cookies)
The cookies to return as Set-Cookie headers as a varargs of Cookie objects- Specified by:
withCookies
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
cookies
- a varargs of Cookie objects
-
withCookie
public HttpResponse withCookie(Cookie cookie)
Add cookie to return as Set-Cookie header- Specified by:
withCookie
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
cookie
- a Cookie object
-
withCookie
public HttpResponse withCookie(String name, String value)
Add cookie to return as Set-Cookie header- Specified by:
withCookie
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
name
- the cookies namevalue
- the cookies value
-
withCookie
public HttpResponse withCookie(NottableString name, NottableString value)
Adds one cookie to match on or to not match on using the NottableString, each NottableString can either be a positive matching value, such as string("match"), or a value to not match on, such as not("do not match"), the string values passed to the NottableString can be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookie
in interfaceHttpObject<HttpResponse,BodyWithContentType>
- Parameters:
name
- the cookies namevalue
- the cookies value
-
getCookieList
public List<Cookie> getCookieList()
- Specified by:
getCookieList
in interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getCookieMap
public Map<NottableString,NottableString> getCookieMap()
-
cookieHeadeDoesNotAlreadyExists
public boolean cookieHeadeDoesNotAlreadyExists(Cookie cookieValue)
-
cookieHeadeDoesNotAlreadyExists
public boolean cookieHeadeDoesNotAlreadyExists(String name, String value)
-
withConnectionOptions
public HttpResponse withConnectionOptions(ConnectionOptions connectionOptions)
The connection options for override the default connection behaviour, this allows full control of headers such as "Connection" or "Content-Length" or controlling whether the socket is closed after the response has been sent- Parameters:
connectionOptions
- the connection options for override the default connection behaviour
-
getConnectionOptions
public ConnectionOptions getConnectionOptions()
-
getType
public Action.Type getType()
- Specified by:
getType
in classAction<HttpResponse>
-
clone
public HttpResponse clone()