|
| 1 | +/* |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign; |
| 17 | + |
| 18 | +import com.squareup.okhttp.mockwebserver.MockResponse; |
| 19 | +import com.squareup.okhttp.mockwebserver.rule.MockWebServerRule; |
| 20 | + |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import feign.Target.HardCodedTarget; |
| 25 | + |
| 26 | +import static feign.assertj.MockWebServerAssertions.assertThat; |
| 27 | + |
| 28 | +public class TargetTest { |
| 29 | + |
| 30 | + @Rule |
| 31 | + public final MockWebServerRule server = new MockWebServerRule(); |
| 32 | + |
| 33 | + interface TestQuery { |
| 34 | + |
| 35 | + @RequestLine("GET /{path}?query={query}") |
| 36 | + Response get(@Param("path") String path, @Param("query") String query); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void baseCaseQueryParamsArePercentEncoded() throws InterruptedException { |
| 41 | + server.enqueue(new MockResponse()); |
| 42 | + |
| 43 | + String baseUrl = server.getUrl("/default").toString(); |
| 44 | + |
| 45 | + Feign.builder().target(TestQuery.class, baseUrl).get("slash/foo", "slash/bar"); |
| 46 | + |
| 47 | + assertThat(server.takeRequest()).hasPath("/default/slash/foo?query=slash%2Fbar"); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Per <a href="https://github.com/Netflix/feign/issues/227">#227</a>, some may want to opt out of |
| 52 | + * percent encoding. Here's how. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + public void targetCanCreateCustomRequest() throws InterruptedException { |
| 56 | + server.enqueue(new MockResponse()); |
| 57 | + |
| 58 | + String baseUrl = server.getUrl("/default").toString(); |
| 59 | + Target<TestQuery> custom = new HardCodedTarget<TestQuery>(TestQuery.class, baseUrl) { |
| 60 | + |
| 61 | + @Override |
| 62 | + public Request apply(RequestTemplate input) { |
| 63 | + Request urlEncoded = super.apply(input); |
| 64 | + return Request.create( |
| 65 | + urlEncoded.method(), |
| 66 | + urlEncoded.url().replace("%2F", "/"), |
| 67 | + urlEncoded.headers(), |
| 68 | + urlEncoded.body(), urlEncoded.charset() |
| 69 | + ); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + Feign.builder().target(custom).get("slash/foo", "slash/bar"); |
| 74 | + |
| 75 | + assertThat(server.takeRequest()).hasPath("/default/slash/foo?query=slash/bar"); |
| 76 | + } |
| 77 | +} |
0 commit comments