|
24 | 24 |
|
25 | 25 | import java.util.Collection; |
26 | 26 | import java.util.Collections; |
| 27 | +import java.util.HashMap; |
27 | 28 | import java.util.LinkedHashMap; |
28 | 29 | import okio.Buffer; |
29 | 30 | import org.assertj.core.api.Fail; |
|
50 | 51 | import feign.codec.Encoder; |
51 | 52 | import feign.codec.ErrorDecoder; |
52 | 53 | import feign.codec.StringDecoder; |
| 54 | +import feign.Feign.ResponseMappingDecoder; |
53 | 55 |
|
54 | 56 | import static feign.Util.UTF_8; |
55 | 57 | import static feign.assertj.MockWebServerAssertions.assertThat; |
@@ -706,6 +708,49 @@ public void encodedQueryParam() throws Exception { |
706 | 708 | .hasPath("/?trim=5.2FSi+"); |
707 | 709 | } |
708 | 710 |
|
| 711 | + @Test |
| 712 | + public void responseMapperIsAppliedBeforeDelegate() throws IOException { |
| 713 | + ResponseMappingDecoder decoder = new ResponseMappingDecoder(upperCaseResponseMapper(), new StringDecoder()); |
| 714 | + String output = (String) decoder.decode(responseWithText("response"), String.class); |
| 715 | + |
| 716 | + assertThat(output).isEqualTo("RESPONSE"); |
| 717 | + } |
| 718 | + |
| 719 | + private ResponseMapper upperCaseResponseMapper() { |
| 720 | + return new ResponseMapper() { |
| 721 | + @Override |
| 722 | + public Response map(Response response, Type type) { |
| 723 | + try { |
| 724 | + return response |
| 725 | + .toBuilder() |
| 726 | + .body(Util.toString(response.body().asReader()).toUpperCase().getBytes()) |
| 727 | + .build(); |
| 728 | + } catch (IOException e) { |
| 729 | + throw new RuntimeException(e); |
| 730 | + } |
| 731 | + } |
| 732 | + }; |
| 733 | + } |
| 734 | + |
| 735 | + private Response responseWithText(String text) { |
| 736 | + return Response.builder() |
| 737 | + .body(text, Util.UTF_8) |
| 738 | + .status(200) |
| 739 | + .headers(new HashMap<String, Collection<String>>()) |
| 740 | + .build(); |
| 741 | + } |
| 742 | + |
| 743 | + @Test |
| 744 | + public void mapAndDecodeExecutesMapFunction() { |
| 745 | + server.enqueue(new MockResponse().setBody("response!")); |
| 746 | + |
| 747 | + TestInterface api = new Feign.Builder() |
| 748 | + .mapAndDecode(upperCaseResponseMapper(), new StringDecoder()) |
| 749 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 750 | + |
| 751 | + assertEquals(api.post(), "RESPONSE!"); |
| 752 | + } |
| 753 | + |
709 | 754 | interface TestInterface { |
710 | 755 |
|
711 | 756 | @RequestLine("POST /") |
|
0 commit comments