ehaydenr
ehaydenr

Reputation: 240

Lagom - Testing Service Call with Headers

I'm trying to follow the documentation here regarding "Handling headers." I can't figure out how to test service call invocation with headers. Normally, I would just do

service.sayHello().invoke(...)

I noticed that ServerServiceCall and HeaderServiceCall accept invokeWithHeaders(...) and my ServiceCall is implemented as a HeaderServiceCall, but whenever I try to change my service API to ServerServiceCall or HeaderServiceCall, i get:

 Error in custom provider, java.lang.IllegalArgumentException: Service calls must return ServiceCall, subtypes are not allowed

How can I write a test that invokes the service call with custom request headers? I've tried keeping the call as ServiceCall in the API, implementing with HeaderServiceCall, and casting the call to HeaderServiceCall in the test, but I got a cast exception when trying to do that. Any help is much appreciated. Thanks.

Upvotes: 0

Views: 1112

Answers (1)

jarek000000
jarek000000

Reputation: 96

I've just adopted my test code to check it:

 public HeaderServiceCall<NewUser, RegUserStatus> addUser(String id) {

    return (reqHeaders, postedUser) -> {
        System.out.println(reqHeaders.getHeader("Referer"));
        PersistentEntityRef<UserCommand> ref = persistentEntityRegistry.refFor(UserEntity.class, id);
        return ref.ask(new UserCommand.RegisterUser(id, postedUser)).thenApply( stat -> Pair.create(ResponseHeader.OK, stat));
    };
}

my api:

ServiceCall<NewUser, RegUserStatus> addUser(String id);

and test:

@Test
public void testIt() {
    withServer(defaultSetup(), server -> {
        UsersService service = server.client(UsersService.class);
        RegUserStatus created = service.addUser("aaa").handleRequestHeader(
                rh -> rh.withHeader("Referer" ,"winter")
        ).invoke(new NewUser("aaa")).toCompletableFuture().get(5, SECONDS);
        assertEquals(true, created.ok); // default greeting

    });
}

Upvotes: 3

Related Questions