Class TransformedRequest

java.lang.Object
com.trafficparrot.sdk.http.TransformedRequest
All Implemented Interfaces:
com.github.tomakehurst.wiremock.http.Request

public class TransformedRequest extends Object implements com.github.tomakehurst.wiremock.http.Request
A copy of an HTTP request with a changed body or an added header, for an HttpResponseDefinitionTransformer to pass to a TransformedRequestDefinition:

 TransformedRequest changed = TransformedRequest.like(serveEvent.getRequest())
         .withTransformBody(body -> body.replace("2020", "2016"))
         .withHeader(HttpHeader.httpHeader("X-Changed-By", "my-extension"));
 
Start from like(com.github.tomakehurst.wiremock.http.Request), or from fromBytes(com.github.tomakehurst.wiremock.http.Request, byte[]), fromJson(com.github.tomakehurst.wiremock.http.Request, java.lang.String) or fromBase64(com.github.tomakehurst.wiremock.http.Request, java.lang.String) to replace the body. A TransformedRequest does not change: each with method returns a new one.

It is made to be sent on, not to be inspected. getScheme(), getHost(), getPort() and queryParameter(key) throw UnsupportedOperationException, containsHeader(key) answers false for every header (use header(key).isPresent()), and it has no form parameters and no original request.

  • Constructor Details

  • Method Details

    • like

      public static TransformedRequest like(com.github.tomakehurst.wiremock.http.Request request)
      A copy of a request: its URL, method, client IP, headers, cookies, parts and protocol, and its body as text. Because the body is copied as text, use fromBytes(com.github.tomakehurst.wiremock.http.Request, byte[]) for a body that is not text.
      Parameters:
      request - the request to copy
      Returns:
      the copy
    • fromBytes

      public static TransformedRequest fromBytes(com.github.tomakehurst.wiremock.http.Request template, byte[] bytes)
      A copy of a request with these bytes as its body.
      Parameters:
      template - the request to copy everything but the body from
      bytes - the new body
      Returns:
      the copy
    • fromJson

      public static TransformedRequest fromJson(com.github.tomakehurst.wiremock.http.Request template, String jsonString)
      A copy of a request with this text as its body. The text is not parsed, so it may be JSON or anything else.
      Parameters:
      template - the request to copy everything but the body from
      jsonString - the new body
      Returns:
      the copy
    • fromBase64

      public static TransformedRequest fromBase64(com.github.tomakehurst.wiremock.http.Request template, String base64Body)
      A copy of a request whose body is the bytes this Base64 text decodes to.
      Parameters:
      template - the request to copy everything but the body from
      base64Body - the new body, Base64 encoded
      Returns:
      the copy
    • contentEncoding

      public static Charset contentEncoding(com.github.tomakehurst.wiremock.http.HttpHeaders headers)
      The character set a message's Content-Type header names, for reading its body as text.
      Parameters:
      headers - the message's headers; may be null
      Returns:
      the header's charset, or UTF-8 when there are no headers, no Content-Type header or no charset in it
    • contentEncoding

      public static Charset contentEncoding(com.github.tomakehurst.wiremock.http.ContentTypeHeader contentTypeHeader)
      The character set a Content-Type header names, for reading a body as text.
      Parameters:
      contentTypeHeader - the header; may be null
      Returns:
      the header's charset, or UTF-8 when the header is null or absent or names none
    • getUrl

      public String getUrl()
      Specified by:
      getUrl in interface com.github.tomakehurst.wiremock.http.Request
    • getAbsoluteUrl

      public String getAbsoluteUrl()
      Specified by:
      getAbsoluteUrl in interface com.github.tomakehurst.wiremock.http.Request
    • getMethod

      public com.github.tomakehurst.wiremock.http.RequestMethod getMethod()
      Specified by:
      getMethod in interface com.github.tomakehurst.wiremock.http.Request
    • getScheme

      public String getScheme()
      Specified by:
      getScheme in interface com.github.tomakehurst.wiremock.http.Request
    • getHost

      public String getHost()
      Specified by:
      getHost in interface com.github.tomakehurst.wiremock.http.Request
    • getPort

      public int getPort()
      Specified by:
      getPort in interface com.github.tomakehurst.wiremock.http.Request
    • getClientIp

      public String getClientIp()
      Specified by:
      getClientIp in interface com.github.tomakehurst.wiremock.http.Request
    • getHeader

      public String getHeader(String key)
      Specified by:
      getHeader in interface com.github.tomakehurst.wiremock.http.Request
    • header

      public com.github.tomakehurst.wiremock.http.HttpHeader header(String key)
      Specified by:
      header in interface com.github.tomakehurst.wiremock.http.Request
    • contentTypeHeader

      public com.github.tomakehurst.wiremock.http.ContentTypeHeader contentTypeHeader()
      Specified by:
      contentTypeHeader in interface com.github.tomakehurst.wiremock.http.Request
    • getHeaders

      public com.github.tomakehurst.wiremock.http.HttpHeaders getHeaders()
      Specified by:
      getHeaders in interface com.github.tomakehurst.wiremock.http.Request
    • containsHeader

      public boolean containsHeader(String key)
      Specified by:
      containsHeader in interface com.github.tomakehurst.wiremock.http.Request
    • getAllHeaderKeys

      public Set<String> getAllHeaderKeys()
      Specified by:
      getAllHeaderKeys in interface com.github.tomakehurst.wiremock.http.Request
    • getHttpHeaders

      public com.github.tomakehurst.wiremock.http.HttpHeaders getHttpHeaders()
      The request's headers; the same as getHeaders().
      Returns:
      the headers
    • getCookies

      public com.google.common.collect.ImmutableMap<String,com.github.tomakehurst.wiremock.http.Cookie> getCookies()
      Specified by:
      getCookies in interface com.github.tomakehurst.wiremock.http.Request
    • queryParameter

      public com.github.tomakehurst.wiremock.http.QueryParameter queryParameter(String key)
      Specified by:
      queryParameter in interface com.github.tomakehurst.wiremock.http.Request
    • formParameter

      public com.github.tomakehurst.wiremock.http.FormParameter formParameter(String key)
      Specified by:
      formParameter in interface com.github.tomakehurst.wiremock.http.Request
    • formParameters

      public Map<String,com.github.tomakehurst.wiremock.http.FormParameter> formParameters()
      Specified by:
      formParameters in interface com.github.tomakehurst.wiremock.http.Request
    • isBrowserProxyRequest

      public boolean isBrowserProxyRequest()
      Specified by:
      isBrowserProxyRequest in interface com.github.tomakehurst.wiremock.http.Request
    • getOriginalRequest

      public Optional<com.github.tomakehurst.wiremock.http.Request> getOriginalRequest()
      Specified by:
      getOriginalRequest in interface com.github.tomakehurst.wiremock.http.Request
    • getProtocol

      public String getProtocol()
      Specified by:
      getProtocol in interface com.github.tomakehurst.wiremock.http.Request
    • getBody

      public byte[] getBody()
      Specified by:
      getBody in interface com.github.tomakehurst.wiremock.http.Request
    • getBodyAsString

      public String getBodyAsString()
      Specified by:
      getBodyAsString in interface com.github.tomakehurst.wiremock.http.Request
    • getBodyAsBase64

      public String getBodyAsBase64()
      Specified by:
      getBodyAsBase64 in interface com.github.tomakehurst.wiremock.http.Request
    • isMultipart

      public boolean isMultipart()
      Specified by:
      isMultipart in interface com.github.tomakehurst.wiremock.http.Request
    • getParts

      public Collection<com.github.tomakehurst.wiremock.http.Request.Part> getParts()
      Specified by:
      getParts in interface com.github.tomakehurst.wiremock.http.Request
    • getPart

      public com.github.tomakehurst.wiremock.http.Request.Part getPart(String name)
      Specified by:
      getPart in interface com.github.tomakehurst.wiremock.http.Request
    • withTransformBody

      public TransformedRequest withTransformBody(TransformedRequest.BodyTransformer bodyTransformer)
      A copy of this request with its body changed.
      Parameters:
      bodyTransformer - given this request's body as text, returns the new body
      Returns:
      the copy with the new body
    • withHeader

      public TransformedRequest withHeader(com.github.tomakehurst.wiremock.http.HttpHeader header)
      A copy of this request with a header added. The body is carried over as text, so for a body that is not text add the header first and set the body last: fromBytes(like(request).withHeader(header), bytes).
      Parameters:
      header - the header to add
      Returns:
      the copy with the header
    • toString

      public String toString()
      Overrides:
      toString in class Object