Skip to content

Commit 3b277e7

Browse files
rober710FroMage
authored andcommitted
Add tests to check response body can be read on error.
1 parent 92f09f2 commit 3b277e7

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

independent-projects/resteasy-reactive/client/runtime/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,17 @@
6464
<artifactId>jboss-logging</artifactId>
6565
</dependency>
6666

67+
<dependency>
68+
<groupId>org.wiremock</groupId>
69+
<artifactId>wiremock-standalone</artifactId>
70+
<scope>test</scope>
71+
<exclusions>
72+
<exclusion>
73+
<groupId>commons-logging</groupId>
74+
<artifactId>commons-logging</artifactId>
75+
</exclusion>
76+
</exclusions>
77+
</dependency>
78+
6779
</dependencies>
6880
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.jboss.resteasy.reactive.client.handlers;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
import static org.junit.jupiter.api.Assertions.fail;
10+
11+
import java.util.Map;
12+
13+
import jakarta.ws.rs.WebApplicationException;
14+
import jakarta.ws.rs.client.Client;
15+
import jakarta.ws.rs.client.ClientBuilder;
16+
import jakarta.ws.rs.core.HttpHeaders;
17+
import jakarta.ws.rs.core.MediaType;
18+
import jakarta.ws.rs.core.Response;
19+
import jakarta.ws.rs.core.UriBuilder;
20+
21+
import org.junit.jupiter.api.AfterAll;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
25+
import com.github.tomakehurst.wiremock.WireMockServer;
26+
27+
import wiremock.com.fasterxml.jackson.core.JsonProcessingException;
28+
import wiremock.com.fasterxml.jackson.core.type.TypeReference;
29+
import wiremock.com.fasterxml.jackson.databind.ObjectMapper;
30+
31+
class ClientResponseCompleteRestHandlerTest {
32+
33+
private static final int MOCK_SERVER_PORT = 9300;
34+
private static final WireMockServer wireMockServer = new WireMockServer(MOCK_SERVER_PORT);
35+
private static final ObjectMapper objectMapper = new ObjectMapper();
36+
private static final Client httpClient = ClientBuilder.newBuilder().build();
37+
38+
@BeforeAll
39+
static void start() {
40+
wireMockServer.stubFor(get(urlPathEqualTo("/get-400"))
41+
.willReturn(aResponse().withStatus(400).withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
42+
.withBody("{\"error\": true, \"message\": \"Invalid parameter\"}")));
43+
wireMockServer.stubFor(get(urlPathEqualTo("/get-500"))
44+
.willReturn(aResponse().withStatus(500).withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
45+
.withBody("{\"error\": true, \"message\": \"Unexpected error.\"}")));
46+
wireMockServer.start();
47+
}
48+
49+
@AfterAll
50+
static void stop() {
51+
wireMockServer.stop();
52+
}
53+
54+
private String generateURL(String path) {
55+
return UriBuilder.fromUri("http://localhost:" + MOCK_SERVER_PORT).path(path).build().toString();
56+
}
57+
58+
@Test
59+
void testResponseOnClientError() throws Exception {
60+
try {
61+
httpClient.target(generateURL("/get-400")).request().get(String.class);
62+
fail("Should have thrown an exception");
63+
} catch (WebApplicationException e) {
64+
Response response = e.getResponse();
65+
checkResponse(response);
66+
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
67+
assertEquals("Invalid parameter", parsedResponseContent.get("message"));
68+
}
69+
70+
// Test when we manually handle the response.
71+
try (Response response = httpClient.target(generateURL("/get-400")).request().get()) {
72+
checkResponse(response);
73+
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
74+
assertEquals("Invalid parameter", parsedResponseContent.get("message"));
75+
}
76+
}
77+
78+
@Test
79+
void testResponseOnServerError() throws Exception {
80+
try {
81+
httpClient.target(generateURL("/get-500")).request().get(String.class);
82+
fail("Should have thrown an exception");
83+
} catch (WebApplicationException e) {
84+
Response response = e.getResponse();
85+
checkResponse(response);
86+
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
87+
assertEquals("Unexpected error.", parsedResponseContent.get("message"));
88+
}
89+
90+
// Test when we manually handle the response.
91+
try (Response response = httpClient.target(generateURL("/get-500")).request().get()) {
92+
checkResponse(response);
93+
Map<String, Object> parsedResponseContent = readAndParseResponse(response);
94+
assertEquals("Unexpected error.", parsedResponseContent.get("message"));
95+
}
96+
}
97+
98+
private void checkResponse(Response response) {
99+
assertNotNull(response);
100+
assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE));
101+
assertTrue(response.hasEntity());
102+
response.bufferEntity();
103+
}
104+
105+
private Map<String, Object> readAndParseResponse(Response response) throws JsonProcessingException {
106+
String responseContent = response.readEntity(String.class);
107+
return objectMapper.readValue(responseContent,
108+
new TypeReference<>() {
109+
});
110+
}
111+
112+
}

independent-projects/resteasy-reactive/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<smallrye-mutiny-vertx-core.version>3.18.1</smallrye-mutiny-vertx-core.version>
7171
<reactive-streams.version>1.0.4</reactive-streams.version>
7272
<mockito.version>5.16.0</mockito.version>
73+
<wiremock.version>3.11.0</wiremock.version>
7374
<mutiny-zero.version>1.1.1</mutiny-zero.version>
7475

7576
<!-- Forbidden API checks -->
@@ -337,6 +338,13 @@
337338
<scope>test</scope>
338339
</dependency>
339340

341+
<dependency>
342+
<groupId>org.wiremock</groupId>
343+
<artifactId>wiremock-standalone</artifactId>
344+
<version>${wiremock.version}</version>
345+
<scope>test</scope>
346+
</dependency>
347+
340348
<dependency>
341349
<groupId>io.rest-assured</groupId>
342350
<artifactId>rest-assured</artifactId>

0 commit comments

Comments
 (0)