Interface GroovyRequestFixture

    • Method Detail

      • handle

        static HandlingResult handle​(Handler handler,
                                     @DelegatesTo(GroovyRequestFixture.class)
                                     Closure<?> closure)
                              throws Exception
        Unit test a Handler.

        Example:

         import ratpack.groovy.handling.GroovyHandler
         import ratpack.groovy.handling.GroovyContext
         import ratpack.groovy.test.handling.GroovyRequestFixture
        
         class MyHandler extends GroovyHandler {
           void handle(GroovyContext context) {
             context.with {
               def outputHeaderValue = request.headers.get("input-value") + ":bar"
               response.headers.set("output-value", outputHeaderValue)
               render "received: " + request.path
             }
           }
         }
        
         def result = GroovyRequestFixture.handle(new MyHandler()) {
           header "input-value", "foo"
           uri "some/path"
         }
        
         assert result.rendered(String) == "received: some/path"
         assert result.headers.get("output-value") == "foo:bar"
         
        Parameters:
        handler - the handler to test
        closure - the configuration of the request fixture
        Returns:
        The result of the invocation
        Throws:
        HandlerTimeoutException - if the handler takes more than RequestFixture.timeout(int) seconds to send a response or call next() on the context
        Exception - any thrown by closure
      • handle

        static HandlingResult handle​(Action<? super Chain> handlers,
                                     @DelegatesTo(GroovyRequestFixture.class)
                                     Closure<?> closure)
                              throws Exception
        Unit test a chain of handlers.

        Example:

        
         import ratpack.groovy.test.handling.GroovyRequestFixture
         import ratpack.groovy.Groovy
        
         def handlers = Groovy.chain {
           all {
             def outputHeaderValue = request.headers.get("input-value") + ":bar"
             response.headers.set("output-value", outputHeaderValue)
             next()
           }
           all {
             render "received: " + request.path
           }
         }
        
         def result = GroovyRequestFixture.handle(handlers) {
           header "input-value", "foo"
           uri "some/path"
         }
        
         assert result.rendered(String) == "received: some/path"
         assert result.headers.get("output-value") == "foo:bar"
         
        Parameters:
        handlers - the handlers to test
        closure - the configuration of the request fixture
        Returns:
        The result of the invocation
        Throws:
        HandlerTimeoutException - if the handler takes more than RequestFixture.timeout(int) seconds to send a response or call next() on the context
        Exception - any thrown by closure
      • requestFixture

        static GroovyRequestFixture requestFixture​(RequestFixture requestFixture)
        Create a Groovy request fixture, for unit testing a Handler, by wrapping the given RequestFixture.
        Parameters:
        requestFixture - The request fixture to wrap
        Returns:
        a Groovy request fixture
      • body

        GroovyRequestFixture body​(byte[] bytes,
                                  String contentType)
        Sets the request body to be the given bytes, and adds a Content-Type request header of the given value.

        By default the body is empty.

        Specified by:
        body in interface RequestFixture
        Parameters:
        bytes - the request body in bytes
        contentType - the content type of the request body
        Returns:
        this
      • body

        GroovyRequestFixture body​(String text,
                                  String contentType)
        Sets the request body to be the given string in utf8 bytes, and adds a Content-Type request header of the given value.

        By default the body is empty.

        Specified by:
        body in interface RequestFixture
        Parameters:
        text - the request body as a string
        contentType - the content type of the request body
        Returns:
        this
      • file

        MultipartFileSpec file()
        A specification of a file to upload (see RFC2388).

        Can be used to construct a multipart form with files

        Specified by:
        file in interface RequestFixture
        Returns:
        a specification of a multipart file
      • file

        RequestFixture file​(String field,
                            String filename,
                            String data)
        Uploads a file via a multipart form (see RFC2388).
        Specified by:
        file in interface RequestFixture
        Parameters:
        field - form field name
        filename - filename of uploaded file
        data - content of file
        Returns:
        this
      • form

        MultipartFormSpec form()
        A specification of a multipart form (see RFC2388).

        Can be used to construct a multipart form with name value pairs and files

        Note that more than one value and more than one file can be associated with a single field

        Specified by:
        form in interface RequestFixture
        Returns:
        a specification of a multipart form
      • responseHeader

        GroovyRequestFixture responseHeader​(CharSequence name,
                                            String value)
        Set a response header value.

        Can be used to simulate the setting of a response header by an upstream handler.

        By default there are no request headers.

        Specified by:
        responseHeader in interface RequestFixture
        Parameters:
        name - the header name
        value - the header value
        Returns:
        this
      • method

        GroovyRequestFixture method​(String method)
        Set the request method (case insensitive).

        The default method is "GET".

        Specified by:
        method in interface RequestFixture
        Parameters:
        method - the request method
        Returns:
        this
      • uri

        GroovyRequestFixture uri​(String uri)
        The URI of the request.

        No encoding is performed on the given value. It is expected to be a well formed URI path string (potentially including query and fragment strings)

        Specified by:
        uri in interface RequestFixture
        Parameters:
        uri - the URI of the request
        Returns:
        this
      • timeout

        GroovyRequestFixture timeout​(int timeoutSeconds)
        Sets the maximum time to allow the handler under test to produce a result.

        As handlers may execute asynchronously, a maximum time limit must be used to guard against never ending handlers.

        Specified by:
        timeout in interface RequestFixture
        Parameters:
        timeoutSeconds - the maximum number of seconds to allow the handler(s) under test to produce a result
        Returns:
        this
      • pathBinding

        GroovyRequestFixture pathBinding​(Map<String,​String> pathTokens)
        Adds a path binding, with the given path tokens.

        By default, there are no path tokens and no path binding.

        Specified by:
        pathBinding in interface RequestFixture
        Parameters:
        pathTokens - the path tokens to make available to the handler(s) under test
        Returns:
        this
      • pathBinding

        GroovyRequestFixture pathBinding​(String boundTo,
                                         String pastBinding,
                                         Map<String,​String> pathTokens)
        Adds a path binding, with the given path tokens and parts.

        By default, there are no path tokens and no path binding.

        Specified by:
        pathBinding in interface RequestFixture
        Parameters:
        boundTo - the part of the request path that the binding bound to
        pastBinding - the part of the request path past boundTo
        pathTokens - the path tokens and binding to make available to the handler(s) under test
        Returns:
        this
      • pathBinding

        GroovyRequestFixture pathBinding​(String boundTo,
                                         String pastBinding,
                                         Map<String,​String> pathTokens,
                                         String description)
        Adds a path binding, with the given path tokens and parts.

        By default, there are no path tokens and no path binding.

        Specified by:
        pathBinding in interface RequestFixture
        Parameters:
        boundTo - the part of the request path that the binding bound to
        pastBinding - the part of the request path past boundTo
        pathTokens - the path tokens and binding to make available to the handler(s) under test
        description - the description of the request path binding
        Returns:
        this