Class HttpResponse

All Implemented Interfaces:
HttpMessage<HttpResponse,BodyWithContentType>, Message

public class HttpResponse extends Action<HttpResponse> implements HttpMessage<HttpResponse,BodyWithContentType>
Author:
jamesdbloom
  • Constructor Details

    • HttpResponse

      public HttpResponse()
  • Method Details

    • 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.
    • badGatewayResponse

      public static HttpResponse badGatewayResponse()
      Static builder to create a bad gateway 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()
    • withStatusCodeRange

      public HttpResponse withStatusCodeRange(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). When set it drives status matching in place of an exact withStatusCode(Integer) comparison. Supported forms:
      • class range — a single digit followed by XX (case-insensitive), e.g. "2XX" matches 200..299, "5xx" matches 500..599;
      • numeric operator — a leading comparison operator and a number, e.g. ">= 400", "> 200", "< 300", "<= 204", "== 201".
      When statusCodeRange is null/blank the exact getStatusCode() value is used, preserving the historical exact-match behaviour. When both are set the range takes precedence for matching and the exact getStatusCode() is not consulted.
      Parameters:
      statusCodeRange - a status-code range ("2XX") or operator (">= 400") expression
    • getStatusCodeRange

      public String getStatusCodeRange()
    • 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, use withBody(String, Charset).
      Specified by:
      withBody in interface HttpMessage<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 interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      body - a string
      charset - character set the string will be encoded in
    • withBody

      public HttpResponse withBody(String body, 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 string
      contentType - 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 interface HttpMessage<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 interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      body - an instance of one of the Body subclasses including StringBody or BinaryBody
    • withBodyFromFile

      public HttpResponse withBodyFromFile(String filePath)
    • withBodyFromFile

      public HttpResponse withBodyFromFile(String filePath, MediaType contentType)
    • getBody

      public BodyWithContentType getBody()
      Specified by:
      getBody in interface HttpMessage<HttpResponse,BodyWithContentType>
    • getBodyAsRawBytes

      public byte[] getBodyAsRawBytes()
      Specified by:
      getBodyAsRawBytes in interface HttpMessage<HttpResponse,BodyWithContentType>
    • getBodyAsString

      public String getBodyAsString()
      Specified by:
      getBodyAsString in interface HttpMessage<HttpResponse,BodyWithContentType>
    • withGenerateFromSchema

      public HttpResponse 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. The same example-generation engine used for OpenAPI responses is reused, so the generated body honours type, required, enum, default, arrays and nested objects.

      This is additive: it only takes effect when the response has no explicit body. An explicit body (set via any withBody(...) overload) always wins.

      Parameters:
      generateFromSchema - an inline JSON Schema, e.g. "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}}}"
    • getGenerateFromSchema

      public String getGenerateFromSchema()
    • getHeaders

      public Headers getHeaders()
      Specified by:
      getHeaders in interface HttpMessage<HttpResponse,BodyWithContentType>
    • withHeaders

      public HttpResponse withHeaders(Headers headers)
      Specified by:
      withHeaders in interface HttpMessage<HttpResponse,BodyWithContentType>
    • withHeaders

      public HttpResponse withHeaders(List<Header> headers)
      The headers to return as a list of Header objects
      Specified by:
      withHeaders in interface HttpMessage<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 interface HttpMessage<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 interface HttpMessage<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 interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      name - the header name
      values - 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 interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      name - the header name as a NottableString
      values - the header values which can be a varags of NottableStrings
    • withContentType

      public HttpResponse withContentType(MediaType mediaType)
      Specified by:
      withContentType in interface HttpMessage<HttpResponse,BodyWithContentType>
    • 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 interface HttpMessage<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 name
      values - the header values
    • getHeaderList

      public List<Header> getHeaderList()
      Specified by:
      getHeaderList in interface HttpMessage<HttpResponse,BodyWithContentType>
    • getHeaderMultimap

      public com.google.common.collect.Multimap<NottableString,NottableString> getHeaderMultimap()
    • getHeader

      public List<String> getHeader(String name)
      Specified by:
      getHeader in interface HttpMessage<HttpResponse,BodyWithContentType>
    • getFirstHeader

      public String getFirstHeader(String name)
      Specified by:
      getFirstHeader in interface HttpMessage<HttpResponse,BodyWithContentType>
    • containsHeader

      public boolean containsHeader(String name)
      Returns true if a header with the specified name has been added
      Specified by:
      containsHeader in interface HttpMessage<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 interface HttpMessage<HttpResponse,BodyWithContentType>
    • removeHeader

      public HttpResponse removeHeader(NottableString name)
      Specified by:
      removeHeader in interface HttpMessage<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 name
      value - the header value
      Returns:
      true if a header has been added with that name otherwise false
    • getTrailers

      public Headers getTrailers()
    • withTrailers

      public HttpResponse withTrailers(Headers trailers)
      The trailing headers (HTTP trailers) to send after the response body. Trailers are emitted as protocol-appropriate trailing headers: a chunked trailer section for HTTP/1.1 (with an automatic Trailer header and chunked transfer-encoding), a trailing HEADERS frame for HTTP/2, and a trailing HEADERS frame for HTTP/3.

      On HTTP/1.1 trailers force Transfer-Encoding: chunked (RFC 7230 section 3.3.1 -- trailers and a fixed Content-Length are mutually exclusive); any explicit Content-Length you set is dropped from a trailer-carrying HTTP/1.1 response.

      Null or empty trailers result in no trailers being sent (the default behaviour).

      Parameters:
      trailers - a Headers object holding the trailing header fields
    • withTrailers

      public HttpResponse withTrailers(List<Header> trailers)
      The trailing headers to send as a list of Header objects
      Parameters:
      trailers - a list of Header objects
    • withTrailers

      public HttpResponse withTrailers(Header... trailers)
      The trailing headers to send as a varargs of Header objects
      Parameters:
      trailers - varargs of Header objects
    • withTrailer

      public HttpResponse withTrailer(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 exist
      Parameters:
      trailer - a Header object
    • withTrailer

      public HttpResponse 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 exist
      Parameters:
      name - the trailer name
      values - the trailer values
    • withTrailer

      public HttpResponse withTrailer(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 exist
      Parameters:
      name - the trailer name as a NottableString
      values - the trailer values which can be a varags of NottableStrings
    • getTrailerList

      public List<Header> getTrailerList()
    • getTrailerMultimap

      public com.google.common.collect.Multimap<NottableString,NottableString> getTrailerMultimap()
    • getCookies

      public Cookies getCookies()
      Specified by:
      getCookies in interface HttpMessage<HttpResponse,BodyWithContentType>
    • withCookies

      public HttpResponse withCookies(Cookies cookies)
      Specified by:
      withCookies in interface HttpMessage<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 interface HttpMessage<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 interface HttpMessage<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 interface HttpMessage<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 interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      name - the cookies name
      value - 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 ...)

      Specified by:
      withCookie in interface HttpMessage<HttpResponse,BodyWithContentType>
      Parameters:
      name - the cookies name
      value - the cookies value
    • getCookieList

      public List<Cookie> getCookieList()
      Specified by:
      getCookieList in interface HttpMessage<HttpResponse,BodyWithContentType>
    • getCookieMap

      public Map<NottableString,NottableString> getCookieMap()
    • cookieHeaderDoesNotAlreadyExists

      public boolean cookieHeaderDoesNotAlreadyExists(Cookie cookieValue)
    • cookieHeaderDoesNotAlreadyExists

      public boolean cookieHeaderDoesNotAlreadyExists(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()
    • withRecoverAfter

      public HttpResponse withRecoverAfter(RecoverAfter recoverAfter)
      Attach a retry/backoff recovery primitive: serve a failure response for the first failTimes matches, then serve this configured response. Lets a client's retry/backoff logic be tested deterministically against a transiently-failing dependency. A null recoverAfter (the default) is inert and leaves this response unchanged.
      Parameters:
      recoverAfter - the recovery primitive, or null for no recovery behaviour
      See Also:
    • getRecoverAfter

      public RecoverAfter getRecoverAfter()
    • withStreamId

      public HttpResponse withStreamId(Integer streamId)
    • getStreamId

      public Integer getStreamId()
    • withTiming

      public HttpResponse withTiming(Timing timing)
    • getTiming

      public Timing getTiming()
    • withStreamingBody

      public HttpResponse withStreamingBody(StreamingBody streamingBody)
      Attach a streaming body to this response. When set, the response head has been delivered but the body is still arriving incrementally via this sink.
      Parameters:
      streamingBody - the streaming body sink
    • getStreamingBody

      public StreamingBody getStreamingBody()
      Returns:
      the streaming body sink, or null if this is a regular fully-buffered response
    • getType

      public Action.Type getType()
      Specified by:
      getType in class Action<HttpResponse>
    • shallowClone

      public HttpResponse shallowClone()
    • clone

      public HttpResponse clone()
      Overrides:
      clone in class Object
    • update

      public HttpResponse update(HttpResponse responseOverride, HttpResponseModifier responseModifier)
    • update

      public HttpResponse update(HttpResponse responseOverride, HttpResponseModifier responseModifier, HttpRequest request)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Action<HttpResponse>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Action<HttpResponse>