Skip to content
Merged
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
12 changes: 12 additions & 0 deletions independent-projects/resteasy-reactive/client/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,17 @@
<artifactId>jboss-logging</artifactId>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.resteasy.reactive.client.spi.FieldFiller;
import org.jboss.resteasy.reactive.client.spi.MultipartResponseData;
import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl;
import org.jboss.resteasy.reactive.common.jaxrs.StatusTypeImpl;

import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.FileUpload;
Expand All @@ -38,10 +39,19 @@ public void handle(RestClientRequestContext context) throws Exception {
public static ResponseImpl mapToResponse(RestClientRequestContext context,
boolean parseContent)
throws IOException {
ClientResponseContextImpl responseContext = context.getOrCreateClientResponseContext();
Response.StatusType statusType = new StatusTypeImpl(responseContext.getStatus(), responseContext.getReasonPhrase());
return mapToResponse(context, statusType, parseContent);
}

public static ResponseImpl mapToResponse(RestClientRequestContext context,
Response.StatusType effectiveResponseStatus,
boolean parseContent)
throws IOException {
Map<Class<?>, MultipartResponseData> multipartDataMap = context.getMultipartResponsesData();
ClientResponseContextImpl responseContext = context.getOrCreateClientResponseContext();
ClientResponseBuilderImpl builder = new ClientResponseBuilderImpl();
builder.status(responseContext.getStatus(), responseContext.getReasonPhrase());
builder.status(effectiveResponseStatus);
builder.setAllHeaders(responseContext.getHeaders());
builder.invocationState(context);
InputStream entityStream = responseContext.getEntityStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.StatusType;

import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.client.api.WebClientApplicationException;
import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl;
import org.jboss.resteasy.reactive.client.impl.ClientResponseContextImpl;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
Expand All @@ -28,8 +28,8 @@ public void handle(RestClientRequestContext context) throws Exception {
if (context.isCheckSuccessfulFamily()) {
StatusType effectiveResponseStatus = determineEffectiveResponseStatus(context, requestContext);
if (Response.Status.Family.familyOf(effectiveResponseStatus.getStatusCode()) != Response.Status.Family.SUCCESSFUL) {
throw new WebClientApplicationException(effectiveResponseStatus.getStatusCode(),
effectiveResponseStatus.getReasonPhrase());
Response response = ClientResponseCompleteRestHandler.mapToResponse(context, effectiveResponseStatus, true);
throw new ClientWebApplicationException(response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.jboss.resteasy.reactive.client.handlers;

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.urlPathEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Map;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.WireMockServer;

import wiremock.com.fasterxml.jackson.core.JsonProcessingException;
import wiremock.com.fasterxml.jackson.core.type.TypeReference;
import wiremock.com.fasterxml.jackson.databind.ObjectMapper;

class ClientResponseCompleteRestHandlerTest {

private static final int MOCK_SERVER_PORT = 9300;
private static final WireMockServer wireMockServer = new WireMockServer(MOCK_SERVER_PORT);
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Client httpClient = ClientBuilder.newBuilder().build();

@BeforeAll
static void start() {
wireMockServer.stubFor(get(urlPathEqualTo("/get-400"))
.willReturn(aResponse().withStatus(400).withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody("{\"error\": true, \"message\": \"Invalid parameter\"}")));
wireMockServer.stubFor(get(urlPathEqualTo("/get-500"))
.willReturn(aResponse().withStatus(500).withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody("{\"error\": true, \"message\": \"Unexpected error.\"}")));
wireMockServer.start();
}

@AfterAll
static void stop() {
wireMockServer.stop();
}

private String generateURL(String path) {
return UriBuilder.fromUri("http://localhost:" + MOCK_SERVER_PORT).path(path).build().toString();
}

@Test
void testResponseOnClientError() throws Exception {
try {
httpClient.target(generateURL("/get-400")).request().get(String.class);
fail("Should have thrown an exception");
} catch (WebApplicationException e) {
Response response = e.getResponse();
checkResponse(response);
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
assertEquals("Invalid parameter", parsedResponseContent.get("message"));
}

// Test when we manually handle the response.
try (Response response = httpClient.target(generateURL("/get-400")).request().get()) {
checkResponse(response);
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
assertEquals("Invalid parameter", parsedResponseContent.get("message"));
}
}

@Test
void testResponseOnServerError() throws Exception {
try {
httpClient.target(generateURL("/get-500")).request().get(String.class);
fail("Should have thrown an exception");
} catch (WebApplicationException e) {
Response response = e.getResponse();
checkResponse(response);
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
assertEquals("Unexpected error.", parsedResponseContent.get("message"));
}

// Test when we manually handle the response.
try (Response response = httpClient.target(generateURL("/get-500")).request().get()) {
checkResponse(response);
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
assertEquals("Unexpected error.", parsedResponseContent.get("message"));
}
}

private void checkResponse(Response response) {
assertNotNull(response);
assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE));
assertTrue(response.hasEntity());
response.bufferEntity();
}

private Map<String, Object> readAndParseResponse(Response response) throws JsonProcessingException {
String responseContent = response.readEntity(String.class);
return objectMapper.readValue(responseContent,
new TypeReference<>() {
});
}

}
8 changes: 8 additions & 0 deletions independent-projects/resteasy-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<smallrye-mutiny-vertx-core.version>3.18.1</smallrye-mutiny-vertx-core.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
<mockito.version>5.16.0</mockito.version>
<wiremock.version>3.11.0</wiremock.version>
<mutiny-zero.version>1.1.1</mutiny-zero.version>

<!-- Forbidden API checks -->
Expand Down Expand Up @@ -337,6 +338,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down
Loading