Package com.trafficparrot.sdk.http
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceChanges a request body, forwithTransformBody(com.trafficparrot.sdk.http.TransformedRequest.BodyTransformer).Nested classes/interfaces inherited from interface com.github.tomakehurst.wiremock.http.Request
com.github.tomakehurst.wiremock.http.Request.Part -
Constructor Summary
ConstructorsConstructorDescriptionTransformedRequest(String url, String absoluteUrl, com.github.tomakehurst.wiremock.http.RequestMethod method, String clientIp, com.github.tomakehurst.wiremock.http.HttpHeaders httpHeaders, com.google.common.collect.ImmutableMap<String, com.github.tomakehurst.wiremock.http.Cookie> cookies, boolean browserProxyRequest, String stringBody, Collection<com.github.tomakehurst.wiremock.http.Request.Part> parts) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsHeader(String key) static CharsetcontentEncoding(com.github.tomakehurst.wiremock.http.ContentTypeHeader contentTypeHeader) The character set aContent-Typeheader names, for reading a body as text.static CharsetcontentEncoding(com.github.tomakehurst.wiremock.http.HttpHeaders headers) The character set a message'sContent-Typeheader names, for reading its body as text.com.github.tomakehurst.wiremock.http.ContentTypeHeadercom.github.tomakehurst.wiremock.http.FormParameterformParameter(String key) static TransformedRequestfromBase64(com.github.tomakehurst.wiremock.http.Request template, String base64Body) A copy of a request whose body is the bytes this Base64 text decodes to.static TransformedRequestfromBytes(com.github.tomakehurst.wiremock.http.Request template, byte[] bytes) A copy of a request with these bytes as its body.static TransformedRequestA copy of a request with this text as its body.byte[]getBody()com.google.common.collect.ImmutableMap<String,com.github.tomakehurst.wiremock.http.Cookie> com.github.tomakehurst.wiremock.http.HttpHeadersgetHost()com.github.tomakehurst.wiremock.http.HttpHeadersThe request's headers; the same asgetHeaders().com.github.tomakehurst.wiremock.http.RequestMethodOptional<com.github.tomakehurst.wiremock.http.Request>com.github.tomakehurst.wiremock.http.Request.PartCollection<com.github.tomakehurst.wiremock.http.Request.Part>getParts()intgetPort()getUrl()com.github.tomakehurst.wiremock.http.HttpHeaderbooleanbooleanstatic TransformedRequestlike(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.com.github.tomakehurst.wiremock.http.QueryParameterqueryParameter(String key) toString()withHeader(com.github.tomakehurst.wiremock.http.HttpHeader header) A copy of this request with a header added.withTransformBody(TransformedRequest.BodyTransformer bodyTransformer) A copy of this request with its body changed.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tomakehurst.wiremock.http.Request
getId, getPathParameters
-
Constructor Details
-
TransformedRequest
@Deprecated public TransformedRequest(String url, String absoluteUrl, com.github.tomakehurst.wiremock.http.RequestMethod method, String clientIp, com.github.tomakehurst.wiremock.http.HttpHeaders httpHeaders, com.google.common.collect.ImmutableMap<String, com.github.tomakehurst.wiremock.http.Cookie> cookies, boolean browserProxyRequest, String stringBody, Collection<com.github.tomakehurst.wiremock.http.Request.Part> parts) Deprecated.Uselike(com.github.tomakehurst.wiremock.http.Request),fromBytes(com.github.tomakehurst.wiremock.http.Request, byte[]),fromJson(com.github.tomakehurst.wiremock.http.Request, java.lang.String)orfromBase64(com.github.tomakehurst.wiremock.http.Request, java.lang.String), which copy all of this from a request.Creates a request from its parts, with the protocolHTTP/1.1.- Parameters:
url- the path and query string, asgetUrl()returns itabsoluteUrl- the whole URL, asgetAbsoluteUrl()returns itmethod- the HTTP methodclientIp- the client's IP addresshttpHeaders- the headerscookies- the cookies by namebrowserProxyRequest- whether the request came through Traffic Parrot as a browser proxystringBody- the body as textparts- the parts of a multipart request; null or empty for any other
-
-
Method Details
-
like
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, usefromBytes(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 frombytes- 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 fromjsonString- 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 frombase64Body- the new body, Base64 encoded- Returns:
- the copy
-
contentEncoding
The character set a message'sContent-Typeheader 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, noContent-Typeheader or nocharsetin it
-
contentEncoding
public static Charset contentEncoding(com.github.tomakehurst.wiremock.http.ContentTypeHeader contentTypeHeader) The character set aContent-Typeheader 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
- Specified by:
getUrlin interfacecom.github.tomakehurst.wiremock.http.Request
-
getAbsoluteUrl
- Specified by:
getAbsoluteUrlin interfacecom.github.tomakehurst.wiremock.http.Request
-
getMethod
public com.github.tomakehurst.wiremock.http.RequestMethod getMethod()- Specified by:
getMethodin interfacecom.github.tomakehurst.wiremock.http.Request
-
getScheme
- Specified by:
getSchemein interfacecom.github.tomakehurst.wiremock.http.Request
-
getHost
- Specified by:
getHostin interfacecom.github.tomakehurst.wiremock.http.Request
-
getPort
public int getPort()- Specified by:
getPortin interfacecom.github.tomakehurst.wiremock.http.Request
-
getClientIp
- Specified by:
getClientIpin interfacecom.github.tomakehurst.wiremock.http.Request
-
getHeader
- Specified by:
getHeaderin interfacecom.github.tomakehurst.wiremock.http.Request
-
header
- Specified by:
headerin interfacecom.github.tomakehurst.wiremock.http.Request
-
contentTypeHeader
public com.github.tomakehurst.wiremock.http.ContentTypeHeader contentTypeHeader()- Specified by:
contentTypeHeaderin interfacecom.github.tomakehurst.wiremock.http.Request
-
getHeaders
public com.github.tomakehurst.wiremock.http.HttpHeaders getHeaders()- Specified by:
getHeadersin interfacecom.github.tomakehurst.wiremock.http.Request
-
containsHeader
- Specified by:
containsHeaderin interfacecom.github.tomakehurst.wiremock.http.Request
-
getAllHeaderKeys
- Specified by:
getAllHeaderKeysin interfacecom.github.tomakehurst.wiremock.http.Request
-
getHttpHeaders
public com.github.tomakehurst.wiremock.http.HttpHeaders getHttpHeaders()The request's headers; the same asgetHeaders().- Returns:
- the headers
-
getCookies
public com.google.common.collect.ImmutableMap<String,com.github.tomakehurst.wiremock.http.Cookie> getCookies()- Specified by:
getCookiesin interfacecom.github.tomakehurst.wiremock.http.Request
-
queryParameter
- Specified by:
queryParameterin interfacecom.github.tomakehurst.wiremock.http.Request
-
formParameter
- Specified by:
formParameterin interfacecom.github.tomakehurst.wiremock.http.Request
-
formParameters
- Specified by:
formParametersin interfacecom.github.tomakehurst.wiremock.http.Request
-
isBrowserProxyRequest
public boolean isBrowserProxyRequest()- Specified by:
isBrowserProxyRequestin interfacecom.github.tomakehurst.wiremock.http.Request
-
getOriginalRequest
- Specified by:
getOriginalRequestin interfacecom.github.tomakehurst.wiremock.http.Request
-
getProtocol
- Specified by:
getProtocolin interfacecom.github.tomakehurst.wiremock.http.Request
-
getBody
public byte[] getBody()- Specified by:
getBodyin interfacecom.github.tomakehurst.wiremock.http.Request
-
getBodyAsString
- Specified by:
getBodyAsStringin interfacecom.github.tomakehurst.wiremock.http.Request
-
getBodyAsBase64
- Specified by:
getBodyAsBase64in interfacecom.github.tomakehurst.wiremock.http.Request
-
isMultipart
public boolean isMultipart()- Specified by:
isMultipartin interfacecom.github.tomakehurst.wiremock.http.Request
-
getParts
- Specified by:
getPartsin interfacecom.github.tomakehurst.wiremock.http.Request
-
getPart
- Specified by:
getPartin interfacecom.github.tomakehurst.wiremock.http.Request
-
withTransformBody
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
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
-
like(com.github.tomakehurst.wiremock.http.Request),fromBytes(com.github.tomakehurst.wiremock.http.Request, byte[]),fromJson(com.github.tomakehurst.wiremock.http.Request, java.lang.String)orfromBase64(com.github.tomakehurst.wiremock.http.Request, java.lang.String), which copy all of this from a request.