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 HttpResponseclone()booleancontainsHeader(String name)Returns true if a header with the specified name has been addedbooleancontainsHeader(String name, String value)Returns true if a header with the specified name has been addedbooleancookieHeadeDoesNotAlreadyExists(String name, String value)booleancookieHeadeDoesNotAlreadyExists(Cookie cookieValue)BodyWithContentTypegetBody()byte[]getBodyAsRawBytes()StringgetBodyAsString()ConnectionOptionsgetConnectionOptions()List<Cookie>getCookieList()Map<NottableString,NottableString>getCookieMap()CookiesgetCookies()StringgetFirstHeader(String name)List<String>getHeader(String name)List<Header>getHeaderList()com.google.common.collect.Multimap<NottableString,NottableString>getHeaderMultimap()HeadersgetHeaders()StringgetReasonPhrase()IntegergetStatusCode()Action.TypegetType()static HttpResponsenotFoundResponse()Static builder to create a not found response.HttpResponseremoveHeader(String name)HttpResponseremoveHeader(NottableString name)HttpResponsereplaceHeader(String name, String... values)Update header to return as a Header object, if a header with the same name already exists it will be modifiedHttpResponsereplaceHeader(Header header)Update header to return as a Header object, if a header with the same name already exists it will be modifiedstatic HttpResponseresponse()Static builder to create a response.static HttpResponseresponse(String body)Static builder to create a response with a 200 status code and the string response body.HttpResponsewithBody(byte[] body)Set response body to return as binary such as a pdf or imageHttpResponsewithBody(String body)Set response body to return as a string response body.HttpResponsewithBody(String body, com.google.common.net.MediaType contentType)Set response body to return a string response body with the specified encoding.HttpResponsewithBody(String body, Charset charset)Set response body to return a string response body with the specified encoding.HttpResponsewithBody(BodyWithContentType body)Set the body to return for example:HttpResponsewithConnectionOptions(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 sentHttpResponsewithCookie(String name, String value)Add cookie to return as Set-Cookie headerHttpResponsewithCookie(Cookie cookie)Add cookie to return as Set-Cookie headerHttpResponsewithCookie(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)HttpResponsewithCookies(List<Cookie> cookies)The cookies to return as Set-Cookie headers as a list of Cookie objectsHttpResponsewithCookies(Cookie... cookies)The cookies to return as Set-Cookie headers as a varargs of Cookie objectsHttpResponsewithCookies(Cookies cookies)HttpResponsewithHeader(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 existHttpResponsewithHeader(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 existHttpResponsewithHeader(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 existHttpResponsewithHeaders(List<Header> headers)The headers to return as a list of Header objectsHttpResponsewithHeaders(Header... headers)The headers to return as a varargs of Header objectsHttpResponsewithHeaders(Headers headers)HttpResponsewithReasonPhrase(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.HttpResponsewithStatusCode(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:
withBodyin 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:
withBodyin 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:
withBodyin 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:
withBodyin interfaceHttpObject<HttpResponse,BodyWithContentType>- Parameters:
body- an instance of one of the Body subclasses including StringBody or BinaryBody
-
getBody
public BodyWithContentType getBody()
- Specified by:
getBodyin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getBodyAsRawBytes
public byte[] getBodyAsRawBytes()
- Specified by:
getBodyAsRawBytesin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getBodyAsString
public String getBodyAsString()
- Specified by:
getBodyAsStringin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getHeaders
public Headers getHeaders()
- Specified by:
getHeadersin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withHeaders
public HttpResponse withHeaders(Headers headers)
- Specified by:
withHeadersin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withHeaders
public HttpResponse withHeaders(List<Header> headers)
The headers to return as a list of Header objects- Specified by:
withHeadersin 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:
withHeadersin 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:
withHeaderin 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:
withHeaderin 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:
withHeaderin 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:
replaceHeaderin 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:
getHeaderListin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getHeaderMultimap
public com.google.common.collect.Multimap<NottableString,NottableString> getHeaderMultimap()
-
getHeader
public List<String> getHeader(String name)
- Specified by:
getHeaderin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
getFirstHeader
public String getFirstHeader(String name)
- Specified by:
getFirstHeaderin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
containsHeader
public boolean containsHeader(String name)
Returns true if a header with the specified name has been added- Specified by:
containsHeaderin 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:
removeHeaderin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
removeHeader
public HttpResponse removeHeader(NottableString name)
- Specified by:
removeHeaderin 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:
getCookiesin interfaceHttpObject<HttpResponse,BodyWithContentType>
-
withCookies
public HttpResponse withCookies(Cookies cookies)
- Specified by:
withCookiesin 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:
withCookiesin 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:
withCookiesin 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:
withCookiein 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:
withCookiein 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:
withCookiein interfaceHttpObject<HttpResponse,BodyWithContentType>- Parameters:
name- the cookies namevalue- the cookies value
-
getCookieList
public List<Cookie> getCookieList()
- Specified by:
getCookieListin 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:
getTypein classAction<HttpResponse>
-
clone
public HttpResponse clone()