Class HttpRequest
- java.lang.Object
-
- org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
-
- org.mockserver.model.ObjectWithJsonToString
-
- org.mockserver.model.Not
-
- org.mockserver.model.HttpRequest
-
- All Implemented Interfaces:
HttpObject<HttpRequest,Body>
public class HttpRequest extends Not implements HttpObject<HttpRequest,Body>
- Author:
- jamesdbloom
-
-
Constructor Summary
Constructors Constructor Description HttpRequest()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpRequest
clone()
boolean
containsHeader(String name)
Returns true if a header with the specified name has been addedBody
getBody()
byte[]
getBodyAsRawBytes()
String
getBodyAsString()
List<Cookie>
getCookieList()
Cookies
getCookies()
String
getFirstHeader(String name)
String
getFirstQueryStringParameter(String name)
List<String>
getHeader(String name)
List<Header>
getHeaderList()
Headers
getHeaders()
NottableString
getMethod()
String
getMethod(String defaultValue)
NottableString
getPath()
List<Parameter>
getQueryStringParameterList()
Parameters
getQueryStringParameters()
boolean
hasQueryStringParameter(String name, String value)
boolean
hasQueryStringParameter(NottableString name, NottableString value)
Boolean
isKeepAlive()
Boolean
isSecure()
boolean
matches(String method, String... paths)
HttpRequest
removeHeader(String name)
HttpRequest
removeHeader(NottableString name)
HttpRequest
replaceHeader(Header header)
Adds one header to match on as a Header object where the header values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)static HttpRequest
request()
static HttpRequest
request(String path)
InetSocketAddress
socketAddressFromHostHeader()
HttpRequest
update(HttpRequest replaceRequest)
HttpRequest
withBody(byte[] body)
The body to match on as binary data such as a pdf or imageHttpRequest
withBody(String body)
The exact string body to match on such as "this is an exact string body"HttpRequest
withBody(String body, Charset charset)
The exact string body to match on such as "this is an exact string body"HttpRequest
withBody(Body body)
The body match rules on such as using one of the Body subclasses as follows:HttpRequest
withCookie(String name, String value)
Adds one cookie to match on, which can specified using either plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withCookie(Cookie cookie)
Adds one cookie to match on as a Cookie object where the cookie values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
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 http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withCookies(List<Cookie> cookies)
The cookies to match on as a list of Cookie objects where the values or keys of each cookie can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withCookies(Cookie... cookies)
The cookies to match on as a varags Cookie objects where the values or keys of each cookie can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withCookies(Cookies cookies)
HttpRequest
withHeader(String name, String... values)
Adds one header to match which can specified using plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withHeader(Header header)
Adds one header to match on as a Header object where the header values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withHeader(NottableString name, NottableString... values)
Adds one header 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 also be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withHeaders(List<Header> headers)
The headers to match on as a list of Header objects where the values or keys of each header can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withHeaders(Header... headers)
The headers to match on as a varags of Header objects where the values or keys of each header can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withHeaders(Headers headers)
HttpRequest
withKeepAlive(Boolean isKeepAlive)
Match on whether the request was made using an HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuseHttpRequest
withMethod(String method)
The HTTP method to match on such as "GET" or "POST"HttpRequest
withMethod(NottableString method)
The HTTP method all method except a specific value using the "not" operator, for example this allows operations such as not("GET")HttpRequest
withPath(String path)
The path to match on such as "/some_mocked_path" any servlet context path is ignored for matching and should not be specified here regex values are also supported such as ".*_path", see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for full details of the supported regex syntaxHttpRequest
withPath(NottableString path)
The path to not match on for example not("/some_mocked_path") with match any path not equal to "/some_mocked_path", the servlet context path is ignored for matching and should not be specified here regex values are also supported such as not(".*_path"), see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for full details of the supported regex syntaxHttpRequest
withQueryStringParameter(String name, String... values)
Adds one query string parameter to match which can specified using plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withQueryStringParameter(NottableString name, NottableString... values)
Adds one query string parameter 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 also be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withQueryStringParameter(Parameter parameter)
Adds one query string parameter to match on as a Parameter object where the parameter values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withQueryStringParameters(List<Parameter> parameters)
The query string parameters to match on as a list of Parameter objects where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withQueryStringParameters(Map<String,List<String>> parameters)
The query string parameters to match on as a Map> where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html) HttpRequest
withQueryStringParameters(Parameter... parameters)
The query string parameters to match on as a varags Parameter objects where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)HttpRequest
withQueryStringParameters(Parameters parameters)
HttpRequest
withSecure(Boolean isSsl)
Match on whether the request was made over SSL (i.e.-
Methods inherited from class org.mockserver.model.ObjectWithJsonToString
toString
-
Methods inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
equals, fieldsExcludedFromEqualsAndHashCode, hashCode, key
-
-
-
-
Method Detail
-
request
public static HttpRequest request()
-
request
public static HttpRequest request(String path)
-
isKeepAlive
public Boolean isKeepAlive()
-
withKeepAlive
public HttpRequest withKeepAlive(Boolean isKeepAlive)
Match on whether the request was made using an HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse- Parameters:
isKeepAlive
- true if the request was made with an HTTP persistent connection
-
isSecure
public Boolean isSecure()
-
withSecure
public HttpRequest withSecure(Boolean isSsl)
Match on whether the request was made over SSL (i.e. HTTPS)- Parameters:
isSsl
- true if the request was made with SSL
-
withMethod
public HttpRequest withMethod(String method)
The HTTP method to match on such as "GET" or "POST"- Parameters:
method
- the HTTP method such as "GET" or "POST"
-
withMethod
public HttpRequest withMethod(NottableString method)
The HTTP method all method except a specific value using the "not" operator, for example this allows operations such as not("GET")- Parameters:
method
- the HTTP method to not match on not("GET") or not("POST")
-
getMethod
public NottableString getMethod()
-
withPath
public HttpRequest withPath(String path)
The path to match on such as "/some_mocked_path" any servlet context path is ignored for matching and should not be specified here regex values are also supported such as ".*_path", see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for full details of the supported regex syntax- Parameters:
path
- the path such as "/some_mocked_path" or a regex
-
withPath
public HttpRequest withPath(NottableString path)
The path to not match on for example not("/some_mocked_path") with match any path not equal to "/some_mocked_path", the servlet context path is ignored for matching and should not be specified here regex values are also supported such as not(".*_path"), see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for full details of the supported regex syntax- Parameters:
path
- the path to not match on such as not("/some_mocked_path") or not(".*_path")
-
getPath
public NottableString getPath()
-
getQueryStringParameters
public Parameters getQueryStringParameters()
-
withQueryStringParameters
public HttpRequest withQueryStringParameters(Parameters parameters)
-
withQueryStringParameters
public HttpRequest withQueryStringParameters(List<Parameter> parameters)
The query string parameters to match on as a list of Parameter objects where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Parameters:
parameters
- the list of Parameter objects where the values or keys of each parameter can be either a string or a regex
-
withQueryStringParameters
public HttpRequest withQueryStringParameters(Parameter... parameters)
The query string parameters to match on as a varags Parameter objects where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Parameters:
parameters
- the varags Parameter objects where the values or keys of each parameter can be either a string or a regex
-
withQueryStringParameters
public HttpRequest withQueryStringParameters(Map<String,List<String>> parameters)
The query string parameters to match on as a Map> where the values or keys of each parameter can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html) - Parameters:
parameters
- the Map> object where the values or keys of each parameter can be either a string or a regex
-
withQueryStringParameter
public HttpRequest withQueryStringParameter(Parameter parameter)
Adds one query string parameter to match on as a Parameter object where the parameter values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Parameters:
parameter
- the Parameter object which can have a values list of strings or regular expressions
-
withQueryStringParameter
public HttpRequest withQueryStringParameter(String name, String... values)
Adds one query string parameter to match which can specified using plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Parameters:
name
- the parameter namevalues
- the parameter values which can be a varags of strings or regular expressions
-
withQueryStringParameter
public HttpRequest withQueryStringParameter(NottableString name, NottableString... values)
Adds one query string parameter 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 also be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Parameters:
name
- the parameter name as a NottableStringvalues
- the parameter values which can be a varags of NottableStrings
-
hasQueryStringParameter
public boolean hasQueryStringParameter(NottableString name, NottableString value)
-
withBody
public HttpRequest withBody(String body)
The exact string body to match on such as "this is an exact string body"- Specified by:
withBody
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
body
- the body on such as "this is an exact string body"
-
withBody
public HttpRequest withBody(String body, Charset charset)
The exact string body to match on such as "this is an exact string body"- Specified by:
withBody
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
body
- the body on such as "this is an exact string body"charset
- character set the string will be encoded in
-
withBody
public HttpRequest withBody(byte[] body)
The body to match on as binary data such as a pdf or image- Specified by:
withBody
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
body
- a byte array
-
withBody
public HttpRequest withBody(Body body)
The body match rules on such as using one of the Body subclasses as follows:exact string match: - exact("this is an exact string body");
or
- new StringBody("this is an exact string body")
regular expression match: - regex("username[a-z]{4}");
or
- new RegexBody("username[a-z]{4}");
json match: - json("{username: 'foo', password: 'bar'}");
or
- json("{username: 'foo', password: 'bar'}", MatchType.STRICT);
or
- new JsonBody("{username: 'foo', password: 'bar'}");
json schema match: - jsonSchema("{type: 'object', properties: { 'username': { 'type': 'string' }, 'password': { 'type': 'string' } }, 'required': ['username', 'password']}");
or
- jsonSchemaFromResource("org/mockserver/model/loginSchema.json");
or
- new JsonSchemaBody("{type: 'object', properties: { 'username': { 'type': 'string' }, 'password': { 'type': 'string' } }, 'required': ['username', 'password']}");
xpath match: - xpath("/element[key = 'some_key' and value = 'some_value']");
or
- new XPathBody("/element[key = 'some_key' and value = 'some_value']");
body parameter match: - params( param("name_one", "value_one_one", "value_one_two") param("name_two", "value_two") );
or
- new ParameterBody( new Parameter("name_one", "value_one_one", "value_one_two") new Parameter("name_two", "value_two") );
binary match: - binary(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));
or
- new BinaryBody(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));
for more details of the supported regular expression syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for more details of the supported json syntax see http://jsonassert.skyscreamer.org for more details of the supported json schema syntax see http://json-schema.org/ for more detail of XPath syntax see http://saxon.sourceforge.net/saxon6.5.3/expressions.html
- Specified by:
withBody
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
body
- an instance of one of the Body subclasses including StringBody, ParameterBody or BinaryBody
-
getBody
public Body getBody()
- Specified by:
getBody
in interfaceHttpObject<HttpRequest,Body>
-
getBodyAsRawBytes
public byte[] getBodyAsRawBytes()
- Specified by:
getBodyAsRawBytes
in interfaceHttpObject<HttpRequest,Body>
-
getBodyAsString
public String getBodyAsString()
- Specified by:
getBodyAsString
in interfaceHttpObject<HttpRequest,Body>
-
getHeaders
public Headers getHeaders()
- Specified by:
getHeaders
in interfaceHttpObject<HttpRequest,Body>
-
withHeaders
public HttpRequest withHeaders(Headers headers)
- Specified by:
withHeaders
in interfaceHttpObject<HttpRequest,Body>
-
withHeaders
public HttpRequest withHeaders(List<Header> headers)
The headers to match on as a list of Header objects where the values or keys of each header can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withHeaders
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
headers
- the list of Header objects where the values or keys of each header can be either a string or a regex
-
withHeaders
public HttpRequest withHeaders(Header... headers)
The headers to match on as a varags of Header objects where the values or keys of each header can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withHeaders
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
headers
- the varags of Header objects where the values or keys of each header can be either a string or a regex
-
withHeader
public HttpRequest withHeader(Header header)
Adds one header to match on as a Header object where the header values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withHeader
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
header
- the Header object which can have a values list of strings or regular expressions
-
withHeader
public HttpRequest withHeader(String name, String... values)
Adds one header to match which can specified using plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withHeader
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
name
- the header namevalues
- the header values which can be a varags of strings or regular expressions
-
withHeader
public HttpRequest withHeader(NottableString name, NottableString... values)
Adds one header 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 also be a plain string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withHeader
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
name
- the header name as a NottableStringvalues
- the header values which can be a varags of NottableStrings
-
replaceHeader
public HttpRequest replaceHeader(Header header)
Adds one header to match on as a Header object where the header values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
replaceHeader
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
header
- the Header object which can have a values list of strings or regular expressions
-
getHeaderList
public List<Header> getHeaderList()
- Specified by:
getHeaderList
in interfaceHttpObject<HttpRequest,Body>
-
getHeader
public List<String> getHeader(String name)
- Specified by:
getHeader
in interfaceHttpObject<HttpRequest,Body>
-
getFirstHeader
public String getFirstHeader(String name)
- Specified by:
getFirstHeader
in interfaceHttpObject<HttpRequest,Body>
-
containsHeader
public boolean containsHeader(String name)
Returns true if a header with the specified name has been added- Specified by:
containsHeader
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
name
- the header name- Returns:
- true if a header has been added with that name otherwise false
-
removeHeader
public HttpRequest removeHeader(String name)
- Specified by:
removeHeader
in interfaceHttpObject<HttpRequest,Body>
-
removeHeader
public HttpRequest removeHeader(NottableString name)
- Specified by:
removeHeader
in interfaceHttpObject<HttpRequest,Body>
-
getCookies
public Cookies getCookies()
- Specified by:
getCookies
in interfaceHttpObject<HttpRequest,Body>
-
withCookies
public HttpRequest withCookies(Cookies cookies)
- Specified by:
withCookies
in interfaceHttpObject<HttpRequest,Body>
-
withCookies
public HttpRequest withCookies(List<Cookie> cookies)
The cookies to match on as a list of Cookie objects where the values or keys of each cookie can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookies
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
cookies
- a list of Cookie objects
-
withCookies
public HttpRequest withCookies(Cookie... cookies)
The cookies to match on as a varags Cookie objects where the values or keys of each cookie can be either a string or a regex (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookies
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
cookies
- a varargs of Cookie objects
-
withCookie
public HttpRequest withCookie(Cookie cookie)
Adds one cookie to match on as a Cookie object where the cookie values list can be a list of strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookie
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
cookie
- a Cookie object
-
withCookie
public HttpRequest withCookie(String name, String value)
Adds one cookie to match on, which can specified using either plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookie
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
name
- the cookies namevalue
- the cookies value
-
withCookie
public HttpRequest 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 http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)- Specified by:
withCookie
in interfaceHttpObject<HttpRequest,Body>
- Parameters:
name
- the cookies namevalue
- the cookies value
-
getCookieList
public List<Cookie> getCookieList()
- Specified by:
getCookieList
in interfaceHttpObject<HttpRequest,Body>
-
socketAddressFromHostHeader
public InetSocketAddress socketAddressFromHostHeader()
-
clone
public HttpRequest clone()
-
update
public HttpRequest update(HttpRequest replaceRequest)
-
-