Search in sources :

Example 1 with Request

use of feign.Request in project spring-cloud-sleuth by spring-cloud.

the class FeignRetriesTests method testRetriedWhenExceededNumberOfRetries.

@Test
public void testRetriedWhenExceededNumberOfRetries() throws Exception {
    Client client = (request, options) -> {
        throw new IOException();
    };
    String url = "http://localhost:" + server.getPort();
    TestInterface api = Feign.builder().client(new TracingFeignClient(this.httpTracing, client)).target(TestInterface.class, url);
    try {
        api.decodedPost();
        failBecauseExceptionWasNotThrown(FeignException.class);
    } catch (FeignException e) {
    }
}
Also used : Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Span(zipkin2.Span) HashMap(java.util.HashMap) Client(feign.Client) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) CurrentTraceContext(brave.propagation.CurrentTraceContext) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Charset(java.nio.charset.Charset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) MockWebServer(okhttp3.mockwebserver.MockWebServer) FeignException(feign.FeignException) Before(org.junit.Before) Tracing(brave.Tracing) HttpTracing(brave.http.HttpTracing) Response(feign.Response) SleuthHttpParserAccessor(org.springframework.cloud.sleuth.instrument.web.SleuthHttpParserAccessor) RequestLine(feign.RequestLine) IOException(java.io.IOException) Test(org.junit.Test) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Feign(feign.Feign) BDDMockito(org.mockito.BDDMockito) Rule(org.junit.Rule) BeanFactory(org.springframework.beans.factory.BeanFactory) Request(feign.Request) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) FeignException(feign.FeignException) IOException(java.io.IOException) Client(feign.Client) Test(org.junit.Test)

Example 2 with Request

use of feign.Request in project spring-cloud-sleuth by spring-cloud.

the class FeignRetriesTests method testRetriedWhenRequestEventuallyIsSent.

@Test
public void testRetriedWhenRequestEventuallyIsSent() throws Exception {
    String url = "http://localhost:" + server.getPort();
    final AtomicInteger atomicInteger = new AtomicInteger();
    // Client to simulate a retry scenario
    final Client client = (request, options) -> {
        // we simulate an exception only for the first request
        if (atomicInteger.get() == 1) {
            throw new IOException();
        } else {
            // with the second retry (first retry) we send back good result
            return Response.builder().status(200).reason("OK").headers(new HashMap<>()).body("OK", Charset.defaultCharset()).build();
        }
    };
    TestInterface api = Feign.builder().client(new TracingFeignClient(this.httpTracing, new Client() {

        @Override
        public Response execute(Request request, Request.Options options) throws IOException {
            atomicInteger.incrementAndGet();
            return client.execute(request, options);
        }
    })).target(TestInterface.class, url);
    then(api.decodedPost()).isEqualTo("OK");
    // request interception should take place only twice (1st request & 2nd retry)
    then(atomicInteger.get()).isEqualTo(2);
    then(this.reporter.getSpans().get(0).tags()).containsEntry("error", "IOException");
    then(this.reporter.getSpans().get(1).kind().ordinal()).isEqualTo(Span.Kind.CLIENT.ordinal());
}
Also used : Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Span(zipkin2.Span) HashMap(java.util.HashMap) Client(feign.Client) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) CurrentTraceContext(brave.propagation.CurrentTraceContext) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Charset(java.nio.charset.Charset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) MockWebServer(okhttp3.mockwebserver.MockWebServer) FeignException(feign.FeignException) Before(org.junit.Before) Tracing(brave.Tracing) HttpTracing(brave.http.HttpTracing) Response(feign.Response) SleuthHttpParserAccessor(org.springframework.cloud.sleuth.instrument.web.SleuthHttpParserAccessor) RequestLine(feign.RequestLine) IOException(java.io.IOException) Test(org.junit.Test) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Feign(feign.Feign) BDDMockito(org.mockito.BDDMockito) Rule(org.junit.Rule) BeanFactory(org.springframework.beans.factory.BeanFactory) Request(feign.Request) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) Response(feign.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Request(feign.Request) IOException(java.io.IOException) Client(feign.Client) Test(org.junit.Test)

Example 3 with Request

use of feign.Request in project feign by OpenFeign.

the class ReactiveFeignIntegrationTest method testErrorDecoder.

@SuppressWarnings({ "ThrowableNotThrown" })
@Test
public void testErrorDecoder() {
    this.webServer.enqueue(new MockResponse().setBody("Bad Request").setResponseCode(400));
    ErrorDecoder errorDecoder = mock(ErrorDecoder.class);
    given(errorDecoder.decode(anyString(), any(Response.class))).willReturn(new IllegalStateException("bad request"));
    TestReactiveXService service = RxJavaFeign.builder().errorDecoder(errorDecoder).target(TestReactiveXService.class, this.getServerUrl());
    StepVerifier.create(service.search(new SearchQuery())).expectErrorSatisfies(ex -> assertThat(ex).isInstanceOf(IllegalStateException.class).hasMessage("bad request")).verify();
    verify(errorDecoder, times(1)).decode(anyString(), any(Response.class));
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) GET(javax.ws.rs.GET) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Path(javax.ws.rs.Path) Decoder(feign.codec.Decoder) Client(feign.Client) Logger(feign.Logger) RequestInterceptor(feign.RequestInterceptor) Retryer(feign.Retryer) Mockito.spy(org.mockito.Mockito.spy) Level(feign.Logger.Level) RetryableException(feign.RetryableException) Answer(org.mockito.stubbing.Answer) Options(feign.Request.Options) Flowable(io.reactivex.Flowable) Charset(java.nio.charset.Charset) BDDMockito.given(org.mockito.BDDMockito.given) MockWebServer(okhttp3.mockwebserver.MockWebServer) JacksonDecoder(feign.jackson.JacksonDecoder) ExpectedException(org.junit.rules.ExpectedException) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) RequestLine(feign.RequestLine) Mockito.times(org.mockito.Mockito.times) QueryMap(feign.QueryMap) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) JAXRSContract(feign.jaxrs.JAXRSContract) AdditionalAnswers(org.mockito.AdditionalAnswers) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) Rule(org.junit.Rule) Param(feign.Param) Type(java.lang.reflect.Type) JacksonEncoder(feign.jackson.JacksonEncoder) Request(feign.Request) MockResponse(okhttp3.mockwebserver.MockResponse) RequestTemplate(feign.RequestTemplate) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) MockResponse(okhttp3.mockwebserver.MockResponse) ErrorDecoder(feign.codec.ErrorDecoder) Test(org.junit.Test)

Example 4 with Request

use of feign.Request in project feign by OpenFeign.

the class ReactiveFeignIntegrationTest method testClient.

@Test
public void testClient() throws Exception {
    Client client = mock(Client.class);
    given(client.execute(any(Request.class), any(Options.class))).willAnswer((Answer<Response>) invocation -> Response.builder().status(200).headers(Collections.emptyMap()).body("1.0", Charset.defaultCharset()).request((Request) invocation.getArguments()[0]).build());
    TestReactorService service = ReactorFeign.builder().client(client).target(TestReactorService.class, this.getServerUrl());
    StepVerifier.create(service.version()).expectNext("1.0").expectComplete().verify();
    verify(client, times(1)).execute(any(Request.class), any(Options.class));
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) GET(javax.ws.rs.GET) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Path(javax.ws.rs.Path) Decoder(feign.codec.Decoder) Client(feign.Client) Logger(feign.Logger) RequestInterceptor(feign.RequestInterceptor) Retryer(feign.Retryer) Mockito.spy(org.mockito.Mockito.spy) Level(feign.Logger.Level) RetryableException(feign.RetryableException) Answer(org.mockito.stubbing.Answer) Options(feign.Request.Options) Flowable(io.reactivex.Flowable) Charset(java.nio.charset.Charset) BDDMockito.given(org.mockito.BDDMockito.given) MockWebServer(okhttp3.mockwebserver.MockWebServer) JacksonDecoder(feign.jackson.JacksonDecoder) ExpectedException(org.junit.rules.ExpectedException) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) RequestLine(feign.RequestLine) Mockito.times(org.mockito.Mockito.times) QueryMap(feign.QueryMap) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) JAXRSContract(feign.jaxrs.JAXRSContract) AdditionalAnswers(org.mockito.AdditionalAnswers) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) Rule(org.junit.Rule) Param(feign.Param) Type(java.lang.reflect.Type) JacksonEncoder(feign.jackson.JacksonEncoder) Request(feign.Request) MockResponse(okhttp3.mockwebserver.MockResponse) RequestTemplate(feign.RequestTemplate) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Options(feign.Request.Options) Request(feign.Request) Client(feign.Client) Test(org.junit.Test)

Example 5 with Request

use of feign.Request in project feign by OpenFeign.

the class RequestKeyTest method create.

@SuppressWarnings("deprecation")
@Test
public void create() throws Exception {
    Map<String, Collection<String>> map = new HashMap<String, Collection<String>>();
    map.put("my-header", Arrays.asList("val"));
    Request request = Request.create(Request.HttpMethod.GET, "a", map, "content".getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_16);
    requestKey = RequestKey.create(request);
    assertThat(requestKey.getMethod(), equalTo(HttpMethod.GET));
    assertThat(requestKey.getUrl(), equalTo("a"));
    assertThat(requestKey.getHeaders().size(), is(1));
    assertThat(requestKey.getHeaders().fetch("my-header"), equalTo((Collection<String>) Arrays.asList("val")));
    assertThat(requestKey.getCharset(), equalTo(StandardCharsets.UTF_16));
    assertThat(requestKey.getBody(), equalTo("content".getBytes(StandardCharsets.UTF_8)));
}
Also used : HashMap(java.util.HashMap) Request(feign.Request) Collection(java.util.Collection) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Request (feign.Request)13 Test (org.junit.Test)9 Response (feign.Response)7 Client (feign.Client)6 RequestLine (feign.RequestLine)5 Collection (java.util.Collection)5 IOException (java.io.IOException)4 Charset (java.nio.charset.Charset)4 MockWebServer (okhttp3.mockwebserver.MockWebServer)4 Rule (org.junit.Rule)4 Feign (feign.Feign)3 FeignException (feign.FeignException)3 Param (feign.Param)3 QueryMap (feign.QueryMap)3 QueryMapEncoder (feign.QueryMapEncoder)3 RequestInterceptor (feign.RequestInterceptor)3 RequestTemplate (feign.RequestTemplate)3 ResponseMapper (feign.ResponseMapper)3 Decoder (feign.codec.Decoder)3 HashMap (java.util.HashMap)3