Skip to content

Commit

Permalink
Tests for rest-client-reactive URL override feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mocenas committed Jan 17, 2025
1 parent 1c5d859 commit 5ff1054
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
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")
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 {
/*
* 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);
}
}

0 comments on commit 5ff1054

Please sign in to comment.