Class MockRequest
- All Implemented Interfaces:
Request
Request that can be used in tests.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGetter method for HTTP request headers.Getter method for the HTTP method.Getter method for the path arguments bound to path parameters in the matched route.Parses the request body using the JSON parsing mechanism defined byJsonEngineand the JSON-able types defined in the engine'sJsonRegistries.parseQuerystring(Type type) Parses the whole querystring into an object.voidsetBody(JsonElement body) Sets the request body.voidsetHeaders(Map<String, String> headers) Sets HTTP headers.voidSets the HTTP method.voidsetPathArguments(List<MockPathArgument> pathArguments) Sets path arguments.voidsetQuerystring(Map<String, String> querystring) Sets the querystring.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.github.grumpystuff.grumpyrest.request.Request
parseBody, parseBody, parseQuerystring, parseQuerystring
-
Constructor Details
-
MockRequest
Constructor.- Parameters:
services- the services used to parse path arguments, querystring arguments, and request bodies
-
-
Method Details
-
setMethod
Sets the HTTP method. By default, no method is set, and trying to get the method will cause anIllegalStateException.Setting the request method is usually not needed in single-endpoint tests because the method is used to select an endpoint from the API, but not used by the endpoint itself.
- Parameters:
method- the HTTP method
-
setHeaders
Sets HTTP headers. By default, no headers are set, and trying to get a header will cause anIllegalStateException.- Parameters:
headers- the HTTP headers
-
setPathArguments
Sets path arguments. By default, no path arguments are set, and trying to get a path argument will cause anIllegalStateException.See
MockPathArgumentfor information on why it is used instead ofPathArgument.- Parameters:
pathArguments- the path arguments
-
setQuerystring
Sets the querystring. By default, no querystring is set, and trying to obtain the querystring will cause anIllegalStateException.- Parameters:
querystring- the querystring
-
setBody
Sets the request body. By default, no body is present, and trying to parse the body will cause anIllegalStateException.- Parameters:
body- the request body
-
getMethod
Description copied from interface:RequestGetter method for the HTTP method. -
getHeader
Description copied from interface:RequestGetter method for HTTP request headers. Returns null if a header is not present.Neither this method nor its return value provides any high-level parsing. There is simply not enough consistency between the various HTTP headers to make this useful.
-
getPathArguments
Description copied from interface:RequestGetter method for the path arguments bound to path parameters in the matched route.The returned list contains one element per path parameter. That is, literal path segments do not appear in the returned list.
The path arguments keep the values of the corresponding segments of the request path in their textual form. No parsing or mapping to application types has been performed at this point. Instead, parsing as an application type is done by calling methods on the returned
PathArgumentobjects.Both
SimpleHandlerandComplexHandlercan always call this method. During request processing, and before the handler gets called, however, there is a point where this method is not allowed to be called. This is before a route has been matched and the match result applied to the request.- Specified by:
getPathArgumentsin interfaceRequest- Returns:
- the path arguments
-
parseQuerystring
Description copied from interface:RequestParses the whole querystring into an object. This object is usually a Java record with a one-to-one mapping of querystring parameters to record fields, but custom parsing can be defined through theQuerystringParserRegistry. Parsers for yet unknown record types will be auto-generated, and parsers for the individual fields will be taken from theFromStringParserRegistry.- Specified by:
parseQuerystringin interfaceRequest- Parameters:
type- the type to parse as- Returns:
- the parsed object
- Throws:
QuerystringParsingException- on parsing errors, such as wrongly formatted fields, unknown fields, missing fields or duplicate fields
-
parseBody
Description copied from interface:RequestParses the request body using the JSON parsing mechanism defined byJsonEngineand the JSON-able types defined in the engine'sJsonRegistries. Refer to these classes for details.
-