Package org.mockserver.model
Class HttpResponse
java.lang.Object
- All Implemented Interfaces:
HttpMessage<HttpResponse,,BodyWithContentType> Message
public class HttpResponse
extends Action<HttpResponse>
implements HttpMessage<HttpResponse,BodyWithContentType>
- Author:
- jamesdbloom
-
Nested Class Summary
Nested classes/interfaces inherited from class org.mockserver.model.Action
Action.Direction, Action.Type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpResponseStatic builder to create a bad gateway response.clone()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 addedbooleancookieHeaderDoesNotAlreadyExists(String name, String value) booleancookieHeaderDoesNotAlreadyExists(Cookie cookieValue) booleangetBody()byte[]getFirstHeader(String name) com.google.common.collect.Multimap<NottableString,NottableString> getType()inthashCode()static HttpResponseStatic builder to create a not found response.removeHeader(String name) removeHeader(NottableString name) 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 modifiedreplaceHeader(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 HttpResponseStatic builder to create a response with a 200 status code and the string response body.update(HttpResponse responseOverride, HttpResponseModifier responseModifier) withBody(byte[] body) Set response body to return as binary such as a pdf or imageSet response body to return as a string response body.Set response body to return a string response body with the specified encoding.Set response body to return a string response body with the specified encoding.withBody(BodyWithContentType body) Set the body to return for example:withBodyFromFile(String filePath) withBodyFromFile(String filePath, MediaType contentType) 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 sentwithContentType(MediaType mediaType) withCookie(String name, String value) Add cookie to return as Set-Cookie headerwithCookie(Cookie cookie) Add cookie to return as Set-Cookie headerwithCookie(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 ...)withCookies(List<Cookie> cookies) The cookies to return as Set-Cookie headers as a list of Cookie objectswithCookies(Cookie... cookies) The cookies to return as Set-Cookie headers as a varargs of Cookie objectswithCookies(Cookies cookies) 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 existwithHeader(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 existwithHeader(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 existwithHeaders(List<Header> headers) The headers to return as a list of Header objectswithHeaders(Header... headers) The headers to return as a varargs of Header objectswithHeaders(Headers headers) 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"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 usedwithStreamId(Integer streamId) withStreamingBody(StreamingBody streamingBody) Attach a streaming body to this response.withTiming(Timing timing) Methods inherited from class org.mockserver.model.Action
getDelay, getExpectationId, isPrimary, setExpectationId, withDelay, withDelay, withPrimaryMethods inherited from class org.mockserver.model.ObjectWithJsonToString
toStringMethods inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
fieldsExcludedFromEqualsAndHashCode
-
Constructor Details
-
HttpResponse
public HttpResponse()
-
-
Method Details
-
response
Static builder to create a response. -
response
Static builder to create a response with a 200 status code and the string response body.- Parameters:
body- a string
-
notFoundResponse
Static builder to create a not found response. -
badGatewayResponse
Static builder to create a bad gateway response. -
withStatusCode
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
-
withReasonPhrase
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
-
withBody
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 interfaceHttpMessage<HttpResponse,BodyWithContentType> - Parameters:
body- a string
-
withBody
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 interfaceHttpMessage<HttpResponse,BodyWithContentType> - Parameters:
body- a stringcharset- character set the string will be encoded in
-
withBody
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
Set response body to return as binary such as a pdf or image- Specified by:
withBodyin interfaceHttpMessage<HttpResponse,BodyWithContentType> - Parameters:
body- a byte array
-
withBody
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));
-