Search in sources :

Example 96 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonRoutingFilterTests method createClientHttpResponse.

private ClientHttpResponse createClientHttpResponse() {
    return new ClientHttpResponse() {

        @Override
        public HttpStatus getStatusCode() throws IOException {
            return HttpStatus.OK;
        }

        @Override
        public int getRawStatusCode() throws IOException {
            return 200;
        }

        @Override
        public String getStatusText() throws IOException {
            return "OK";
        }

        @Override
        public void close() {
        }

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("OK".getBytes());
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            return httpHeaders;
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 97 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonRoutingFilterTests method testSetResponseWithNonHttpStatusCode.

@Test
public void testSetResponseWithNonHttpStatusCode() throws Exception {
    ClientHttpResponse response = this.createClientHttpResponseWithNonStatus();
    this.filter.setResponse(response);
    assertThat(517).isEqualTo(this.requestContext.get("responseStatusCode"));
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 98 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonCommandCauseFallbackPropagationTest method causeIsProvidedForNewInterface.

@Test
public void causeIsProvidedForNewInterface() throws Exception {
    TestFallbackProvider provider = TestFallbackProvider.withResponse(HttpStatus.NOT_FOUND);
    RuntimeException exception = new RuntimeException("Failed!");
    TestRibbonCommand testCommand = new TestRibbonCommand(new TestClient(exception), provider, context);
    ClientHttpResponse response = testCommand.execute();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    Throwable cause = provider.getCause();
    assertThat(cause.getClass()).isEqualTo(exception.getClass());
    assertThat(cause.getMessage()).isEqualTo(exception.getMessage());
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 99 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonCommandCauseFallbackPropagationTest method timeoutExceptionIsPropagated.

@Test
public void timeoutExceptionIsPropagated() throws Exception {
    TestFallbackProvider provider = TestFallbackProvider.withResponse(HttpStatus.CONFLICT);
    RuntimeException exception = new RuntimeException("Failed!");
    TestRibbonCommand testCommand = new TestRibbonCommand(new TestClient(exception), provider, 1, context) {

        @Override
        protected ClientRequest createRequest() throws Exception {
            Thread.sleep(5);
            return super.createRequest();
        }
    };
    ClientHttpResponse response = testCommand.execute();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
    assertThat(provider.getCause()).isNotNull();
    assertThat(provider.getCause().getClass()).isEqualTo(HystrixTimeoutException.class);
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 100 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonCommandCauseFallbackPropagationTest method providerIsCalledInCaseOfException.

@Test
public void providerIsCalledInCaseOfException() throws Exception {
    FallbackProvider provider = new TestFallbackProvider(getClientHttpResponse(HttpStatus.INTERNAL_SERVER_ERROR));
    RuntimeException exception = new RuntimeException("Failed!");
    TestRibbonCommand testCommand = new TestRibbonCommand(new TestClient(exception), provider, context);
    ClientHttpResponse response = testCommand.execute();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : FallbackProvider(org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)142 IOException (java.io.IOException)38 Test (org.junit.Test)38 HttpHeaders (org.springframework.http.HttpHeaders)35 ByteArrayInputStream (java.io.ByteArrayInputStream)23 URI (java.net.URI)23 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)23 Test (org.junit.jupiter.api.Test)21 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)20 RestTemplate (org.springframework.web.client.RestTemplate)20 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)18 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)9 ResponseExtractor (org.springframework.web.client.ResponseExtractor)9 Tag (io.micrometer.core.instrument.Tag)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Invocation (org.apache.servicecomb.core.Invocation)6