Class HttpResponse
- All Implemented Interfaces:
HttpMessage<HttpResponse,,BodyWithContentType> Message
- 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> 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) update(HttpResponse responseOverride, HttpResponseModifier responseModifier, HttpRequest request) 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) withGenerateFromSchema(String generateFromSchema) Provide an inline JSON Schema (a plain JSON Schema object, not a full OpenAPI document) from which a schema-valid response body is generated at response time.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"withRecoverAfter(RecoverAfter recoverAfter) Attach a retry/backoff recovery primitive: serve a failure response for the firstfailTimesmatches, then serve this configured response.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 usedwithStatusCodeRange(String statusCodeRange) A status-code range or numeric-operator expression used only when verifying a recorded response (it is never written to the wire when serving).withStreamId(Integer streamId) withStreamingBody(StreamingBody streamingBody) Attach a streaming body to this response.withTiming(Timing timing) withTrailer(String name, String... values) Add a trailing header to send, if a trailer with the same name already exists this will NOT be modified but two trailers will existwithTrailer(Header trailer) Add a trailing header to send as a Header object, if a trailer with the same name already exists this will NOT be modified but two trailers will existwithTrailer(NottableString name, NottableString... values) Add a trailing header to send, if a trailer with the same name already exists this will NOT be modified but two trailers will existwithTrailers(List<Header> trailers) The trailing headers to send as a list of Header objectswithTrailers(Header... trailers) The trailing headers to send as a varargs of Header objectswithTrailers(Headers trailers) The trailing headers (HTTP trailers) to send after the response body.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
-
withStatusCodeRange
A status-code range or numeric-operator expression used only when verifying a recorded response (it is never written to the wire when serving). When set it drives status matching in place of an exactwithStatusCode(Integer)comparison. Supported forms:- class range — a single digit followed by
XX(case-insensitive), e.g."2XX"matches200..299,"5xx"matches500..599; - numeric operator — a leading comparison operator and a number, e.g.
">= 400","> 200","< 300","<= 204","== 201".
statusCodeRangeis null/blank the exactgetStatusCode()value is used, preserving the historical exact-match behaviour. When both are set the range takes precedence for matching and the exactgetStatusCode()is not consulted.- Parameters:
statusCodeRange- a status-code range ("2XX") or operator (">= 400") expression
- class range — a single digit followed by
-
getStatusCodeRange
-
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));
-