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

configure rest client to proxy request via wiremock using proxy settings #166

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
16 changes: 15 additions & 1 deletion deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,28 @@
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-spi-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<artifactId>quarkus-rest-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.Produce;
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.dev.devservices.DevServiceDescriptionBuildItem;
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig;
import io.quarkus.runtime.configuration.ConfigurationException;
Expand All @@ -52,7 +54,9 @@ FeatureBuildItem feature() {
}

@BuildStep(onlyIf = { WireMockServerEnabled.class, GlobalDevServicesConfig.Enabled.class })
DevServicesResultBuildItem setup(LaunchModeBuildItem launchMode, LiveReloadBuildItem liveReload,
@Produce(WiremockStartedBuildItem.class)
void setup(
LaunchModeBuildItem launchMode, LiveReloadBuildItem liveReload,
CuratedApplicationShutdownBuildItem shutdown, WireMockServerBuildTimeConfig config,
BuildProducer<ValidationErrorBuildItem> configErrors) {

Expand All @@ -62,7 +66,7 @@ DevServicesResultBuildItem setup(LaunchModeBuildItem launchMode, LiveReloadBuild
configErrors.produce(new ValidationErrorBuildItem(new ConfigurationException(
format("The specified port %d is not part of the permitted port range! Please specify a port between %d and %d.",
config.port().getAsInt(), MIN_PORT, MAX_PORT))));
return null;
return;
}

// register shutdown callback once
Expand All @@ -76,7 +80,15 @@ DevServicesResultBuildItem setup(LaunchModeBuildItem launchMode, LiveReloadBuild
if (devService == null) {
devService = startWireMockDevService(config);
}
return devService.toBuildItem();
}

@BuildStep(onlyIf = { WireMockServerEnabled.class, GlobalDevServicesConfig.Enabled.class })
@Consume(WiremockStartedBuildItem.class)
void provideDevServiceResult(BuildProducer<DevServicesResultBuildItem> result) {
// need to be done in a separate step to avoid cycle
if (devService != null) {
result.produce(devService.toBuildItem());
}
}

@BuildStep(onlyIf = { WireMockServerEnabled.class, GlobalDevServicesConfig.Enabled.class })
Expand All @@ -98,6 +110,15 @@ void watchWireMockConfigFiles(WireMockServerBuildTimeConfig config,
}
}

@BuildStep(onlyIf = { WireMockServerEnabled.class, GlobalDevServicesConfig.Enabled.class })
@Consume(WiremockStartedBuildItem.class)
void configureRestClient(BuildProducer<RunTimeConfigurationDefaultBuildItem> producer) {
if (devService != null) {
producer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.rest-client.proxy-address",
"localhost:" + Integer.parseInt(devService.getConfig().get(PORT))));
}
}

private static RunningDevService startWireMockDevService(WireMockServerBuildTimeConfig config) {

final WireMockConfiguration configuration = options()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkiverse.wiremock.devservice;

import io.quarkus.builder.item.SimpleBuildItem;

final public class WiremockStartedBuildItem extends SimpleBuildItem {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkiverse.wiremock.devservice;

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

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

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

class WireMockRestClientTest {

private static final String APP_PROPERTIES = "application.properties";

@RegisterExtension
static final QuarkusUnitTest UNIT_TEST = new QuarkusUnitTest().withConfigurationResource(APP_PROPERTIES)
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(QuarkusClient.class));

@Inject
@RestClient
QuarkusClient quarkusClient;

@Test
void testWireMockMappingsFolder() {

QuarkusClient.User result = quarkusClient.test();
assertEquals("John Doe", result.name());
}

@Path("/mock-rest-client")
@RegisterRestClient(baseUri = "http://imaginary-host.io/", configKey = "quarkus-client")
public interface QuarkusClient {

record User(String name, String email) {
}

@GET
@Consumes("application/json")
User test();
}
}
16 changes: 16 additions & 0 deletions deployment/src/test/resources/mappings/rest-client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"request": {
"method": "GET",
"url": "/mock-rest-client"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"name": "John Doe",
"email": "[email protected]"
}
}
}
8 changes: 6 additions & 2 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive</artifactId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkiverse.wiremock.devservice;

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

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

@Path("/mock-rest-client")
@RegisterRestClient(baseUri = "https://imaginary-host.io/", configKey = "quarkus-client")
public interface QuarkusClient {

record User(String name, String email) {
}

@GET
@Consumes("application/json")
User test();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkiverse.wiremock.devservice;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.inject.Inject;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.client.WireMock;

import io.quarkus.test.junit.QuarkusTest;

@ConnectWireMock
@QuarkusTest
public class WiremockRestClientTest {

@Inject
@RestClient
QuarkusClient quarkusClient;
WireMock wiremock;

@Test
void testWireMockRestClientProxy() {
wiremock.register(get(urlEqualTo("/mock-rest-client"))
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
.withBody("{\"name\": \"John Doe\", \"email\": \"[email protected]\"}")));
QuarkusClient.User result = quarkusClient.test();
assertEquals("John Doe", result.name());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
custom.config.wiremock.url=http://localhost:${quarkus.wiremock.devservices.port}/mock-me
custom.config.wiremock.url=http://localhost:${quarkus.wiremock.devservices.port}/mock-me
quarkus.rest-client."quarkus-client".enable-local-proxy=true
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.version>3.8.5</quarkus.version>
<quarkus.version>3.15.1</quarkus.version>
<wiremock.version>3.6.0</wiremock.version>
</properties>
<dependencyManagement>
Expand Down