-1

I have a Micronaut application that uses JWT and JWKS.

In my application yaml for tests, I have configured the jwks url as follows:

micronaut:
  security:
    token:
      jwt:
        signatures:
          jwks:
            securityservice:
              url: "http://127.0.0.1:9090/keys"

I'm using WireMock for mocking the /keys endpoint that Micronaut exposes for JWT validation. I have a hard-coded port that matches the configuration one:

new WireMockServer(wireMockConfig().port(9090)

And everything works fine so far.

The point is that I want to be able to generate a random port to avoid concurrent test collisions with the WireMock port.

So, I've tried with url: "http://127.0.0.1:${wiremock.server.port}/keys" (inspiration from here) as well by using the micronaut.server.port set to -1 and something like url: "http://127.0.0.1:${micronaut.server.port + 1}/keys" but non of them are resolved by Micronaut.

Do you know how this could be achieved? Any workaround maybe by not using WireMock and following another approach?

1 Answer 1

0

I finally managed to solve it by using ${random.port}.

micronaut:
  security:
    token:
      jwt:
        signatures:
          jwks:
            securityservice:
              url: "http://127.0.0.1:${random.port}/keys"

After that, I inject my configuration property in the test with:

@Value("${micronaut.security.token.jwt.signatures.jwks.securityservice.url}")
public String configJwksUrl;

and finally, I can set the random port in the WireMock port server inside my @BeforeAll method:

URL jwksUrl = new URL(configJwksUrl);
jwksApiMockServer = new WireMockServer(wireMockConfig().port(jwksUrl.getPort())                      
    .notifier(new Slf4jNotifier(log.isDebugEnabled())));

Not the answer you're looking for? Browse other questions tagged or ask your own question.