|
| 1 | +/* |
| 2 | + * Copyright © 2012 The Feign Authors (feign@commonhaus.dev) |
| 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 static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import feign.Request.HttpMethod; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.net.HttpURLConnection; |
| 25 | +import java.net.URL; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +class DefaultClientContentLengthTest { |
| 33 | + |
| 34 | + /** |
| 35 | + * {@link HttpURLConnection#getContentLength()} reports the {@code Content-Length} header as-is |
| 36 | + * once it fits in an {@code int}, so a malformed negative header reaches the client unchanged. |
| 37 | + */ |
| 38 | + private static HttpURLConnection connectionWithContentLength(int contentLength) |
| 39 | + throws IOException { |
| 40 | + return new HttpURLConnection(new URL("http://localhost")) { |
| 41 | + |
| 42 | + @Override |
| 43 | + public int getResponseCode() { |
| 44 | + return 200; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String getResponseMessage() { |
| 49 | + return "OK"; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Map<String, List<String>> getHeaderFields() { |
| 54 | + return Collections.singletonMap( |
| 55 | + "Content-Length", Collections.singletonList(String.valueOf(contentLength))); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public int getContentLength() { |
| 60 | + return contentLength; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public InputStream getInputStream() { |
| 65 | + return new ByteArrayInputStream(new byte[0]); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void connect() {} |
| 70 | + |
| 71 | + @Override |
| 72 | + public void disconnect() {} |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean usingProxy() { |
| 76 | + return false; |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private static Response decode(int contentLength) throws IOException { |
| 82 | + final Request request = |
| 83 | + Request.create( |
| 84 | + HttpMethod.GET, |
| 85 | + "http://localhost", |
| 86 | + Collections.emptyMap(), |
| 87 | + null, |
| 88 | + StandardCharsets.UTF_8, |
| 89 | + null); |
| 90 | + return new DefaultClient(null, null) |
| 91 | + .convertResponse(connectionWithContentLength(contentLength), request); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void unknownContentLengthIsReportedAsUnknown() throws IOException { |
| 96 | + // -1 is also what getContentLength() returns for a header above Integer.MAX_VALUE |
| 97 | + assertThat(decode(-1).body().length()).isNull(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void negativeContentLengthIsReportedAsUnknown() throws IOException { |
| 102 | + assertThat(decode(-5).body().length()).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void contentLengthWithinIntRangeIsPreserved() throws IOException { |
| 107 | + assertThat(decode(1024).body().length()).isEqualTo(1024); |
| 108 | + } |
| 109 | +} |
0 commit comments