-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for rest-client-reactive URL override feature
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/UrlOverrideableClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
57 changes: 57 additions & 0 deletions
57
.../java/io/quarkus/ts/http/restclient/reactive/resources/UrlOverrideClientTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...e/src/main/java/io/quarkus/ts/http/restclient/reactive/resources/UrlOverrideResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
http/rest-client-reactive/src/main/resources/urlOverride.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
54 changes: 54 additions & 0 deletions
54
...nt-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/UrlOverrideClientIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |