How to retrieve full endpoint URL in Quarkus test?

Viewed 72

I am looking a solution to retrieve full endpoint URLs within Quarkus application to use it in tests to avoid hard-coding the paths.

The official guide suggests using the @TestHTTPEndpoint and @TestHTTPResource annotations.

If I annotate my test class with @TestHTTPEndpoint(MyResource::class), then all calls via RestAssured without specifying the path work just fine. The problem is, when I try to retrieve the endpoint URL like this (let's say, I need to call multiple endpoints in one test):

@TestHTTPEndpoint(MyResource::class)
@TestHTTPResource
lateinit var myResourceUrl: URL

it kind of works, but the injected URL does not include the quarkus.http.root-path value.

Instead of http://localhost:8081/root-path/my-resource I get just http://localhost:8081/my-resource.

Is there a way to retrieve a full endpoint path that includes the quarkus.http.root-path value?

2 Answers

Introduction

Let's consider the following versions as the current versions.

  • Quarkus: 2.11.2.Final.

Root cause analysis

The io.quarkus.test.common.http.TestHTTPResourceManager (TestHTTPResourceManager for short) class performs the value injection into the fields annotated with the io.quarkus.test.common.http.TestHTTPEndpoint (TestHTTPEndpoint for short) annotation:

  • The TestHTTPResourceManager class retrieves an instance of the org.eclipse.microprofile.config.Config (Config for short) from the org.eclipse.microprofile.config.ConfigProvider (ConfigProvider for short) class and uses the test.url configuration property value retrieved from the Config instance (the Config.getValue(…) method call) as the base URL.
  • The retrieved test.url configuration property value seems to correspond to the test.url system property value that was provided by the io.quarkus.test.common.http.TestHTTPConfigSourceProvider (TestHTTPConfigSourceProvider for short) class.

Possible root cause

For some reason the TestHTTPConfigSourceProvider class does not take into account the quarkus.http.root-path property value, when providing the test.url system property value, which seems to be used as the base URL: quarkus/TestHTTPConfigSourceProvider.java at 2.11.2.Final · quarkusio/quarkus:

    static final String TEST_URL_VALUE = "http://${quarkus.http.host:localhost}:${quarkus.http.test-port:8081}${quarkus.servlet.context-path:}";
    static final String TEST_URL_KEY = "test.url";

Therefore, it looks like a Quarkus issue: a defect (a bug) or a lack of a feature (or a lack of the feature implementation).
Therefore, it is worth reporting it as a Quarkus issue.

Related