Search in sources :

Example 51 with Response

use of feign.Response in project feign by OpenFeign.

the class JsonDecoderTest method badJsonThrowsWrappedJSONException.

@Test
public void badJsonThrowsWrappedJSONException() throws IOException {
    String json = "{\"a\":\"b\",\"c\":1}";
    Response response = Response.builder().status(204).reason("OK").headers(Collections.emptyMap()).body(json, UTF_8).request(request).build();
    Exception exception = assertThrows(DecodeException.class, () -> new JsonDecoder().decode(response, JSONArray.class));
    assertEquals("A JSONArray text must start with '[' at 1 [character 2 line 1]", exception.getMessage());
    assertTrue(exception.getCause() instanceof JSONException);
}
Also used : Response(feign.Response) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) DecodeException(feign.codec.DecodeException) Test(org.junit.Test)

Example 52 with Response

use of feign.Response in project feign by OpenFeign.

the class HttpProtocolVersionTest method testMockProtocolVersion.

@Test
public void testMockProtocolVersion() {
    Remote remote = Feign.builder().client(new MockClient().ok(HttpMethod.GET, "/test")).target(new MockTarget<>(Remote.class));
    Response response = remote.test();
    assertNotNull(response.protocolVersion());
    assertEquals("MOCK", response.protocolVersion().toString());
}
Also used : Response(feign.Response) Test(org.junit.Test)

Example 53 with Response

use of feign.Response in project feign by OpenFeign.

the class Http2ClientAsyncTest method decodingExceptionGetWrappedInDecode404Mode.

@Test
public void decodingExceptionGetWrappedInDecode404Mode() throws Throwable {
    server.enqueue(new MockResponse().setResponseCode(404));
    thrown.expect(DecodeException.class);
    thrown.expectCause(isA(NoSuchElementException.class));
    final TestInterfaceAsync api = newAsyncBuilder().decode404().decoder((response, type) -> {
        assertEquals(404, response.status());
        throw new NoSuchElementException();
    }).target("http://localhost:" + server.getPort());
    unwrap(api.post());
}
Also used : ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) Body(feign.Body) TypeToken(com.google.gson.reflect.TypeToken) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Decoder(feign.codec.Decoder) RequestInterceptor(feign.RequestInterceptor) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) AsyncFeign(feign.AsyncFeign) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AsyncClient(feign.AsyncClient) FeignException(feign.FeignException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Collection(java.util.Collection) RequestLine(feign.RequestLine) PropertyPojo(feign.PropertyPojo) Feign(feign.Feign) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Encoder(feign.codec.Encoder) List(java.util.List) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) Http2Client(feign.http2client.Http2Client) ChildPojo(feign.ChildPojo) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) RequestTemplate(feign.RequestTemplate) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(feign.Headers) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StringDecoder(feign.codec.StringDecoder) Util(feign.Util) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) Buffer(okio.Buffer) Target(feign.Target) HeaderMap(feign.HeaderMap) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) QueryMap(feign.QueryMap) HardCodedTarget(feign.Target.HardCodedTarget) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Param(feign.Param) DecodeException(feign.codec.DecodeException) Request(feign.Request) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EncodeException(feign.codec.EncodeException) MockResponse(okhttp3.mockwebserver.MockResponse) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 54 with Response

use of feign.Response in project feign by OpenFeign.

the class Http2ClientAsyncTest method throwsFeignExceptionWithoutBody.

@Test
public void throwsFeignExceptionWithoutBody() {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = newAsyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target("http://localhost:" + server.getPort());
    try {
        api.noContent();
    } catch (final FeignException e) {
        Assertions.assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        Assertions.assertThat(e.contentUTF8()).isEqualTo("");
    }
}
Also used : ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) Body(feign.Body) TypeToken(com.google.gson.reflect.TypeToken) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Decoder(feign.codec.Decoder) RequestInterceptor(feign.RequestInterceptor) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) AsyncFeign(feign.AsyncFeign) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AsyncClient(feign.AsyncClient) FeignException(feign.FeignException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Collection(java.util.Collection) RequestLine(feign.RequestLine) PropertyPojo(feign.PropertyPojo) Feign(feign.Feign) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Encoder(feign.codec.Encoder) List(java.util.List) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) Http2Client(feign.http2client.Http2Client) ChildPojo(feign.ChildPojo) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) RequestTemplate(feign.RequestTemplate) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(feign.Headers) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StringDecoder(feign.codec.StringDecoder) Util(feign.Util) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) Buffer(okio.Buffer) Target(feign.Target) HeaderMap(feign.HeaderMap) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) QueryMap(feign.QueryMap) HardCodedTarget(feign.Target.HardCodedTarget) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Param(feign.Param) DecodeException(feign.codec.DecodeException) Request(feign.Request) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EncodeException(feign.codec.EncodeException) MockResponse(okhttp3.mockwebserver.MockResponse) FeignException(feign.FeignException) IOException(java.io.IOException) Test(org.junit.Test)

Example 55 with Response

use of feign.Response in project feign by OpenFeign.

the class Http2ClientAsyncTest method whenReturnTypeIsResponseNoErrorHandling.

@SuppressWarnings("deprecation")
@Test
public void whenReturnTypeIsResponseNoErrorHandling() throws Throwable {
    final Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
    headers.put("Location", Arrays.asList("http://bar.com"));
    final Response response = Response.builder().status(302).reason("Found").headers(headers).request(Request.create(HttpMethod.GET, "/", Collections.emptyMap(), null, Util.UTF_8)).body(new byte[0]).build();
    final ExecutorService execs = Executors.newSingleThreadExecutor();
    // fake client as Client.Default follows redirects.
    final TestInterfaceAsync api = AsyncFeign.<Void>asyncBuilder().client(new AsyncClient.Default<>((request, options) -> response, execs)).target(TestInterfaceAsync.class, "http://localhost:" + server.getPort());
    assertThat(unwrap(api.response()).headers().get("Location")).contains("http://bar.com");
    execs.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(feign.Response) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) 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