Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for rest-client-reactive URL override feature #2275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.ts.http.restclient.reactive;

import java.net.URI;
import java.net.URL;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.quarkus.rest.client.reactive.Url;

/**
* RestClient which provides capability to override base URL.
* URL might be overridden to access different service.
* Base URL can be overridden by String, URL or URI parameter type.
*/
@RegisterRestClient
@Path("urlOverride")
public interface UrlOverrideableClient {

@GET
@Path("basic")
String getByString(@Url String uri);

@GET
@Path("basic")
String getByUrl(@Url URL uri);

@GET
@Path("basic")
String getByUri(@Url URI uri);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.ts.http.restclient.reactive.resources;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;

import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.quarkus.ts.http.restclient.reactive.UrlOverrideableClient;

@Path("testUrlOverride")
public class UrlOverrideClientTestResource {
@Inject
@RestClient
UrlOverrideableClient urlOverrideableClient;

@Path("/defaultString")
@GET
public String defaultString() {
return urlOverrideableClient.getByString(null);
}

@Path("/overrideString/")
@GET
public String overrideString(@QueryParam("port") Integer port) {
return urlOverrideableClient.getByString(String.format("http://localhost:%d/", port));
}

@Path("/defaultUrl")
@GET
public String defaultUrl() {
return urlOverrideableClient.getByUrl(null);
}

@Path("/overrideUrl/")
@GET
public String overrideUrl(@QueryParam("port") Integer port) throws MalformedURLException {
return urlOverrideableClient.getByUrl(new URL(String.format("http://localhost:%d/", port)));
}

@Path("/defaultUri")
@GET
public String defaultUri() {
return urlOverrideableClient.getByUri(null);
}

@Path("/overrideUri/")
@GET
public String overrideUri(@QueryParam("port") Integer port) {
return urlOverrideableClient.getByUri(URI.create(String.format("http://localhost:%d/", port)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.ts.http.restclient.reactive.resources;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
@Path("urlOverride")
public class UrlOverrideResource {

@ConfigProperty(name = "ts.quarkus.urlOverride.response", defaultValue = "empty")
String response;

@GET
@Path("basic")
public String basic() {
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.UrlOverrideableClient".uri=http://localhost:${quarkus.http.port}
ts.quarkus.urlOverride.response=default
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.ts.http.restclient.reactive;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.Protocol;
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;

@QuarkusScenario
public class UrlOverrideClientIT {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. What if we use non-existent url for override? How good/terrible is the error?
  2. Does app writes overridden URL into log?

Copy link
Contributor Author

@mocenas mocenas Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Trying to access non-existing URL causes exception on the service. It is possible to catch it and pretend it is OK, but that might cover some actual failures going on.
  2. No it does not not log rewriting URL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I thought, that maybe we should check that error, and that it really happens, to have some non-happy-path tests. Do not know, whether it is a good idea, but please think about it.
  2. And what about tracing? Is there any way for an observer to distinguish these calls?

/*
* Start two application. By default, restClient in app will do HTTP requests against app.
* But if we override the URL, it will do requests against app2.
* Both apps will be distinguished by value of "ts.quarkus.urlOverride.response" which they return as HTTP response.
* The URL override feature overrides base URL, but not HTTP path. So we cannot easily test it using just one app.
*/
@QuarkusApplication()
static RestService app = new RestService()
.withProperties("urlOverride.properties");

@QuarkusApplication()
static RestService app2 = new RestService()
.withProperties("urlOverride.properties")
.withProperty("quarkus.http.ssl-port", "8444")
.withProperty("ts.quarkus.urlOverride.response", "overridden");

@Test
public void shouldOverrideUrlByString() {
testEndpoints("defaultString", "overrideString");
}

@Test
public void shouldOverrideUrlByUrl() {
testEndpoints("defaultUrl", "overrideUrl");
}

@Test
public void shouldOverrideUrlByUri() {
testEndpoints("defaultUri", "overrideUri");
}

private void testEndpoints(String defaultEndpoint, String overrideEndpoint) {
String defaultResponse = app.given().get("/testUrlOverride/" + defaultEndpoint).asString();
assertEquals("default", defaultResponse);

String overriddenResponse = app.given()
.get("/testUrlOverride/" + overrideEndpoint + "/?port=" + app2.getURI(Protocol.HTTP).getPort())
.body().asString();
assertEquals("overridden", overriddenResponse);
}
}
Loading