|
| 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 | +} |
0 commit comments