Search in sources :

Example 36 with Response

use of feign.Response in project incubator-skywalking by apache.

the class DefaultHttpClientInterceptorTest method testMethodsAroundError.

@Test
public void testMethodsAroundError() throws Throwable {
    defaultHttpClientInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, result);
    Response response = mock(Response.class);
    when(response.status()).thenReturn(404);
    defaultHttpClientInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, response);
    assertThat(segmentStorage.getTraceSegments().size(), is(1));
    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    Assert.assertEquals(1, SegmentHelper.getSpans(traceSegment).size());
    AbstractTracingSpan finishedSpan = SegmentHelper.getSpans(traceSegment).get(0);
    assertSpan(finishedSpan);
    List<KeyValuePair> tags = SpanHelper.getTags(finishedSpan);
    assertThat(tags.size(), is(3));
    assertThat(tags.get(0).getValue(), is("GET"));
    assertThat(tags.get(1).getValue(), is("http://skywalking.org/"));
    assertThat(tags.get(2).getValue(), is("404"));
    Assert.assertEquals(true, SpanHelper.getErrorOccurred(finishedSpan));
}
Also used : Response(feign.Response) KeyValuePair(org.apache.skywalking.apm.agent.core.context.util.KeyValuePair) AbstractTracingSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan) TraceSegment(org.apache.skywalking.apm.agent.core.context.trace.TraceSegment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with Response

use of feign.Response 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 38 with Response

use of feign.Response in project feign by OpenFeign.

the class MeteredDecoder method decode.

@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
    final Optional<MeteredBody> body = Optional.ofNullable(response.body()).map(MeteredBody::new);
    Response meteredResponse = body.map(b -> response.toBuilder().body(b).build()).orElse(response);
    Object decoded;
    final Timer.Sample sample = Timer.start(meterRegistry);
    Timer timer = null;
    try {
        decoded = decoder.decode(meteredResponse, type);
        timer = createTimer(response, type, null);
    } catch (IOException | RuntimeException e) {
        timer = createTimer(response, type, e);
        createExceptionCounter(response, type, e).count();
        throw e;
    } catch (Exception e) {
        timer = createTimer(response, type, e);
        createExceptionCounter(response, type, e).count();
        throw new IOException(e);
    } finally {
        if (timer == null) {
            timer = createTimer(response, type, null);
        }
        sample.stop(timer);
    }
    body.ifPresent(b -> createSummary(response, type).record(b.count()));
    return decoded;
}
Also used : Response(feign.Response) Response(feign.Response) Type(java.lang.reflect.Type) Decoder(feign.codec.Decoder) io.micrometer.core.instrument(io.micrometer.core.instrument) Optional(java.util.Optional) IOException(java.io.IOException) EMPTY_TAGS_ARRAY(feign.micrometer.MetricTagResolver.EMPTY_TAGS_ARRAY) RequestTemplate(feign.RequestTemplate) FeignException(feign.FeignException) IOException(java.io.IOException) IOException(java.io.IOException) FeignException(feign.FeignException)

Example 39 with Response

use of feign.Response in project feign by OpenFeign.

the class OkHttpClientTest method testFollowRedirect.

@Test
public void testFollowRedirect() throws Exception {
    String expectedBody = "Hello";
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location", server.url("redirect")));
    server.enqueue(new MockResponse().setBody(expectedBody));
    OkHttpClientTestInterface api = newBuilder().options(new Request.Options(10_000, TimeUnit.MILLISECONDS, 10_000, TimeUnit.MILLISECONDS, true)).target(OkHttpClientTestInterface.class, "http://localhost:" + server.getPort());
    Response response = api.get();
    // Response length should not be null
    assertEquals(200, response.status());
    String payload = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
    assertEquals(expectedBody, payload);
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) AbstractClientTest(feign.client.AbstractClientTest) Test(org.junit.Test)

Example 40 with Response

use of feign.Response in project feign by OpenFeign.

the class OkHttpClientTest method testContentTypeWithoutCharset.

@Test
public void testContentTypeWithoutCharset() throws Exception {
    server.enqueue(new MockResponse().setBody("AAAAAAAA"));
    OkHttpClientTestInterface api = newBuilder().target(OkHttpClientTestInterface.class, "http://localhost:" + server.getPort());
    Response response = api.getWithContentType();
    // Response length should not be null
    assertEquals("AAAAAAAA", Util.toString(response.body().asReader(Util.UTF_8)));
    MockWebServerAssertions.assertThat(server.takeRequest()).hasHeaders(MapEntry.entry("Accept", Collections.singletonList("text/plain")), MapEntry.entry("Content-Type", Collections.singletonList("text/plain"))).hasMethod("GET");
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) AbstractClientTest(feign.client.AbstractClientTest) Test(org.junit.Test)

Aggregations

Response (feign.Response)169 Test (org.junit.Test)118 IOException (java.io.IOException)32 MockResponse (okhttp3.mockwebserver.MockResponse)26 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)22 Test (org.testng.annotations.Test)20 Collection (java.util.Collection)19 HashMap (java.util.HashMap)16 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)16 ArrayList (java.util.ArrayList)15 Gson (com.google.gson.Gson)13 FeignException (feign.FeignException)13 OAuth2ServiceStubs (org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs)13 IdentityProviderException (org.wso2.carbon.apimgt.core.exception.IdentityProviderException)13 Request (feign.Request)11 RetryableException (feign.RetryableException)11 DecodeException (feign.codec.DecodeException)10 Map (java.util.Map)10 ExpectedException (org.junit.rules.ExpectedException)10 Type (java.lang.reflect.Type)9