java.lang.Object
io.github.grumpystuff.grumpyrest.request.mock.MockRequest
All Implemented Interfaces:
Request

public final class MockRequest extends Object implements Request
A mock implementation of Request that can be used in tests.
  • Constructor Details

    • MockRequest

      public MockRequest(MockRequestServices services)
      Constructor.
      Parameters:
      services - the services used to parse path arguments, querystring arguments, and request bodies
  • Method Details

    • setMethod

      public void setMethod(String method)
      Sets the HTTP method. By default, no method is set, and trying to get the method will cause an IllegalStateException.

      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

      public void setHeaders(Map<String,String> headers)
      Sets HTTP headers. By default, no headers are set, and trying to get a header will cause an IllegalStateException.
      Parameters:
      headers - the HTTP headers
    • setPathArguments

      public void setPathArguments(List<MockPathArgument> pathArguments)
      Sets path arguments. By default, no path arguments are set, and trying to get a path argument will cause an IllegalStateException.

      See MockPathArgument for information on why it is used instead of PathArgument.

      Parameters:
      pathArguments - the path arguments
    • setQuerystring

      public void setQuerystring(Map<String,String> querystring)
      Sets the querystring. By default, no querystring is set, and trying to obtain the querystring will cause an IllegalStateException.
      Parameters:
      querystring - the querystring
    • setBody

      public void setBody(JsonElement body)
      Sets the request body. By default, no body is present, and trying to parse the body will cause an IllegalStateException.
      Parameters:
      body - the request body
    • getMethod

      public String getMethod()
      Description copied from interface: Request
      Getter method for the HTTP method.
      Specified by:
      getMethod in interface Request
      Returns:
      the HTTP method
    • getHeader

      public String getHeader(String name)
      Description copied from interface: Request
      Getter 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.

      Specified by:
      getHeader in interface Request
      Parameters:
      name - the name of the header to return
      Returns:
      the header value, as a string, or null if not present
    • getPathArguments

      public List<PathArgument> getPathArguments()
      Description copied from interface: Request
      Getter 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 PathArgument objects.

      Both SimpleHandler and ComplexHandler can 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:
      getPathArguments in interface Request
      Returns:
      the path arguments
    • parseQuerystring

      public Object parseQuerystring(Type type) throws QuerystringParsingException
      Description copied from interface: Request
      Parses 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 the QuerystringParserRegistry. Parsers for yet unknown record types will be auto-generated, and parsers for the individual fields will be taken from the FromStringParserRegistry.
      Specified by:
      parseQuerystring in interface Request
      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

      public Object parseBody(Type type)
      Description copied from interface: Request
      Parses the request body using the JSON parsing mechanism defined by JsonEngine and the JSON-able types defined in the engine's JsonRegistries. Refer to these classes for details.
      Specified by:
      parseBody in interface Request
      Parameters:
      type - the type to parse as
      Returns:
      the parsed object