|
| 1 | +/** |
| 2 | + * Copyright 2012-2021 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign; |
| 15 | + |
| 16 | +import feign.codec.EncodeException; |
| 17 | +import feign.codec.Encoder; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | +import java.io.IOException; |
| 21 | +import java.lang.annotation.ElementType; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.Target; |
| 24 | +import java.lang.reflect.Type; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 28 | + |
| 29 | +public class AlwaysEncodeBodyContractTest { |
| 30 | + |
| 31 | + @Retention(RUNTIME) |
| 32 | + @Target(ElementType.METHOD) |
| 33 | + private @interface SampleMethodAnnotation { |
| 34 | + } |
| 35 | + |
| 36 | + private static class SampleContract extends AlwaysEncodeBodyContract { |
| 37 | + SampleContract() { |
| 38 | + AnnotationProcessor<SampleMethodAnnotation> annotationProcessor = |
| 39 | + (annotation, metadata) -> metadata.template().method(Request.HttpMethod.POST); |
| 40 | + super.registerMethodAnnotation(SampleMethodAnnotation.class, annotationProcessor); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private interface SampleTargetMultipleNonAnnotatedParameters { |
| 45 | + @SampleMethodAnnotation |
| 46 | + String concatenate(String word1, String word2, String word3); |
| 47 | + } |
| 48 | + |
| 49 | + private interface SampleTargetNoParameters { |
| 50 | + @SampleMethodAnnotation |
| 51 | + String concatenate(); |
| 52 | + } |
| 53 | + |
| 54 | + private interface SampleTargetOneParameter { |
| 55 | + @SampleMethodAnnotation |
| 56 | + String concatenate(String word1); |
| 57 | + } |
| 58 | + |
| 59 | + private static class AllParametersSampleEncoder implements Encoder { |
| 60 | + @Override |
| 61 | + public void encode(Object object, Type bodyType, RequestTemplate template) |
| 62 | + throws EncodeException { |
| 63 | + Object[] methodParameters = (Object[]) object; |
| 64 | + String body = |
| 65 | + Arrays.stream(methodParameters).map(String::valueOf).collect(Collectors.joining()); |
| 66 | + template.body(body); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static class BodyParameterSampleEncoder implements Encoder { |
| 71 | + @Override |
| 72 | + public void encode(Object object, Type bodyType, RequestTemplate template) |
| 73 | + throws EncodeException { |
| 74 | + template.body(String.valueOf(object)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static class SampleClient implements Client { |
| 79 | + @Override |
| 80 | + public Response execute(Request request, Request.Options options) throws IOException { |
| 81 | + return Response.builder() |
| 82 | + .status(200) |
| 83 | + .request(request) |
| 84 | + .body(request.body()) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * This test makes sure Feign calls the client provided encoder regardless of how many |
| 91 | + * non-annotated parameters the client method has, as alwaysEncodeBody is set to true. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + public void alwaysEncodeBodyTrueTest() { |
| 95 | + SampleTargetMultipleNonAnnotatedParameters sampleClient1 = Feign.builder() |
| 96 | + .contract(new SampleContract()) |
| 97 | + .encoder(new AllParametersSampleEncoder()) |
| 98 | + .client(new SampleClient()) |
| 99 | + .target(SampleTargetMultipleNonAnnotatedParameters.class, "http://localhost"); |
| 100 | + Assert.assertEquals("foobarchar", sampleClient1.concatenate("foo", "bar", "char")); |
| 101 | + |
| 102 | + SampleTargetNoParameters sampleClient2 = Feign.builder() |
| 103 | + .contract(new SampleContract()) |
| 104 | + .encoder(new AllParametersSampleEncoder()) |
| 105 | + .client(new SampleClient()) |
| 106 | + .target(SampleTargetNoParameters.class, "http://localhost"); |
| 107 | + Assert.assertEquals("", sampleClient2.concatenate()); |
| 108 | + |
| 109 | + SampleTargetOneParameter sampleClient3 = Feign.builder() |
| 110 | + .contract(new SampleContract()) |
| 111 | + .encoder(new AllParametersSampleEncoder()) |
| 112 | + .client(new SampleClient()) |
| 113 | + .target(SampleTargetOneParameter.class, "http://localhost"); |
| 114 | + Assert.assertEquals("moo", sampleClient3.concatenate("moo")); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments