Feign { public static Builder builder() { return new Builder(); }  static Builder builder(); static String configKey(Class targetType, Method method); @Deprecated static String configKey(Method method); abstract T newInstance(Target<T> target);  }
@Test public void throwsFeignExceptionIncludingBody() { server.enqueue(new MockResponse().setBody("success!")); TestInterface api = Feign.builder() .decoder((response, type) -> { throw new IOException("timeout"); }) .target(TestInterface.class, "http: try { api.body("Request body"); } catch (FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http: assertThat(e.contentUTF8()).isEqualTo("Request body"); } }
@Test public void throwsFeignExceptionWithoutBody() { server.enqueue(new MockResponse().setBody("success!")); TestInterface api = Feign.builder() .decoder((response, type) -> { throw new IOException("timeout"); }) .target(TestInterface.class, "http: try { api.noContent(); } catch (FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http: assertThat(e.contentUTF8()).isEqualTo(""); } }
@Test public void ensureRetryerClonesItself() throws Exception { server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1")); server.enqueue(new MockResponse().setResponseCode(200).setBody("foo 2")); server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 3")); server.enqueue(new MockResponse().setResponseCode(200).setBody("foo 4")); MockRetryer retryer = new MockRetryer(); TestInterface api = Feign.builder() .retryer(retryer) .errorDecoder(new ErrorDecoder() { @Override public Exception decode(String methodKey, Response response) { return new RetryableException(response.status(), "play it again sam!", HttpMethod.POST, null, response.request()); } }).target(TestInterface.class, "http: api.post(); api.post(); assertEquals(4, server.getRequestCount()); }
@Test public void throwsOriginalExceptionAfterFailedRetries() throws Exception { server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1")); server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 2")); final String message = "the innerest"; thrown.expect(TestInterfaceException.class); thrown.expectMessage(message); TestInterface api = Feign.builder() .exceptionPropagationPolicy(UNWRAP) .retryer(new Retryer.Default(1, 1, 2)) .errorDecoder(new ErrorDecoder() { @Override public Exception decode(String methodKey, Response response) { return new RetryableException(response.status(), "play it again sam!", HttpMethod.POST, new TestInterfaceException(message), null, response.request()); } }).target(TestInterface.class, "http: api.post(); }
@Test public void throwsRetryableExceptionIfNoUnderlyingCause() throws Exception { server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1")); server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 2")); String message = "play it again sam!"; thrown.expect(RetryableException.class); thrown.expectMessage(message); TestInterface api = Feign.builder() .exceptionPropagationPolicy(UNWRAP) .retryer(new Retryer.Default(1, 1, 2)) .errorDecoder(new ErrorDecoder() { @Override public Exception decode(String methodKey, Response response) { return new RetryableException(response.status(), message, HttpMethod.POST, null, response.request()); } }).target(TestInterface.class, "http: api.post(); }
@Test public void whenReturnTypeIsResponseNoErrorHandling() { Map<String, Collection<String>> headers = new LinkedHashMap<>(); headers.put("Location", Collections.singletonList("http: 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(); TestInterface api = Feign.builder() .client((request, options) -> response) .target(TestInterface.class, "http: assertEquals(api.response().headers().get("Location"), Collections.singletonList("http: }
@Test public void equalsHashCodeAndToStringWork() { Target<TestInterface> t1 = new HardCodedTarget<TestInterface>(TestInterface.class, "http: Target<TestInterface> t2 = new HardCodedTarget<TestInterface>(TestInterface.class, "http: Target<OtherTestInterface> t3 = new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http: TestInterface i1 = Feign.builder().target(t1); TestInterface i2 = Feign.builder().target(t1); TestInterface i3 = Feign.builder().target(t2); OtherTestInterface i4 = Feign.builder().target(t3); assertThat(i1) .isEqualTo(i2) .isNotEqualTo(i3) .isNotEqualTo(i4); assertThat(i1.hashCode()) .isEqualTo(i2.hashCode()) .isNotEqualTo(i3.hashCode()) .isNotEqualTo(i4.hashCode()); assertThat(i1.toString()) .isEqualTo(i2.toString()) .isNotEqualTo(i3.toString()) .isNotEqualTo(i4.toString()); assertThat(t1) .isNotEqualTo(i1); assertThat(t1.hashCode()) .isEqualTo(i1.hashCode()); assertThat(t1.toString()) .isEqualTo(i1.toString()); }
@Test public void decodeLogicSupportsByteArray() throws Exception { byte[] expectedResponse = {12, 34, 56}; server.enqueue(new MockResponse().setBody(new Buffer().write(expectedResponse))); OtherTestInterface api = Feign.builder().target(OtherTestInterface.class, "http: assertThat(api.binaryResponseBody()) .containsExactly(expectedResponse); }
@Test public void encodeLogicSupportsByteArray() throws Exception { byte[] expectedRequest = {12, 34, 56}; server.enqueue(new MockResponse()); OtherTestInterface api = Feign.builder().target(OtherTestInterface.class, "http: api.binaryRequestBody(expectedRequest); assertThat(server.takeRequest()) .hasBody(expectedRequest); }