How to test which handler is called among handler chain

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test which handler is called among handler chain

Minjae Lee
Hello !
I'm new to ratpack and trying to add some test code.
I was wondering if there's a way to test handler chain.
What I want to test is, when there's a list of handlers in some handler class, how do I test which handler is called.
It might not clear to understand, so let's take a look at code example.

//*** 1. main ***//
    public static void main(String... args) throws Exception {
        RatpackServer.start(server ->
            server
                .handlers(chain -> {
                    chain
                        .prefix("myPath", MyHandlerChain.class);
                })

        );
    }

//*** 2. MyHandlerChain class ***//
public class MyHandlerChain implements Action<Chain> {
    @Override
    public void execute(Chain chain) throws Exception {
        chain.path(ctx -> ctx.byMethod(r -> r
            .get(() -> ctx.get(GetHandler.class).handle(ctx))
            .post(() -> ctx.get(PostHandler.class).handle(ctx))
            .delete(() -> ctx.get(DeleteHandler.class).handle(ctx))
            .put(() -> ctx.get(PutHandler.class).handle(ctx))
        ));
    }
}

So, when code example is like above,
how do I write a test code for MyHandlerChain class,
to test if GetHandler is called for GET method, and PostHandler / POST method ... and so on.

Please tell me if it is not a proper way to test.
Or better solution would be appriciated !
Reply | Threaded
Open this post in threaded view
|

Re: How to test which handler is called among handler chain

Minjae Lee
Answer to my own question,

I'm not quite sure if it's proper way to do the test handler chain. Here's what I did.
If anyone has a better idea, please tell me.


//*** TEST CODE Example ***//
class MyHandlerChainSpec extends Specification {

    RequestFixture fixture = RequestFixture.requestFixture()
    Service service = Mock()

    def 'GET method should succeed via GetHandler'() {
        given:
        // some given condition

        when:
        service.getSomething(params) >> Promise.value(expected)

        fixture.getRegistry().add(new GetHandler(service))
        fixture.getRegistry().add(ServerErrorHandler, new CustomErrorHandler())
        fixture.uri(uri)

        HandlingResult result = fixture.method("GET").handleChain(new MyHandlerChain())

        then:
        result.status == Status.of(200)
    }
}


Thank you !
Reply | Threaded
Open this post in threaded view
|

Re: How to test which handler is called among handler chain

Minjae Lee
It seems that RequestFixture can do many things for testing.

https://ratpack.io/manual/current/api/ratpack/test/handling/RequestFixture.html

public interface RequestFixture
A contrived request environment, suitable for unit testing Handler implementations.
A request fixture emulates a request, and the effective state of the request handling in the handler pipeline.

A request fixture can be obtained by the requestFixture() method. However it is often more convenient to use the alternative handle(Handler, Action) method.