|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 the original author or authors. |
| 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 | + * https://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 | + |
| 17 | +package org.springframework.cloud.openfeign.encoding; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import feign.RequestTemplate; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for {@link FeignContentGzipEncodingInterceptor}. |
| 29 | + * |
| 30 | + * @author André Teigler |
| 31 | + */ |
| 32 | +class FeignContentGzipEncodingInterceptorTests { |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldNotAddCompressionHeaderWhenSizeBelowThreshold() { |
| 36 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 37 | + final RequestTemplate template = new RequestTemplate(); |
| 38 | + template.header(HttpEncoding.CONTENT_LENGTH, "2047"); |
| 39 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 40 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 41 | + |
| 42 | + interceptor.apply(template); |
| 43 | + |
| 44 | + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldNotAddCompressionHeaderWhenSizeEqualsThreshold() { |
| 49 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 50 | + final RequestTemplate template = new RequestTemplate(); |
| 51 | + template.header(HttpEncoding.CONTENT_LENGTH, "2048"); |
| 52 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 53 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 54 | + |
| 55 | + interceptor.apply(template); |
| 56 | + |
| 57 | + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void shouldAddCompressionHeaderWhenSizeAboveThreshold() { |
| 62 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 63 | + final RequestTemplate template = new RequestTemplate(); |
| 64 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 65 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 66 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 67 | + |
| 68 | + interceptor.apply(template); |
| 69 | + |
| 70 | + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 71 | + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldNotAddCompressionHeaderWhenTypeMismatch() { |
| 76 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 77 | + properties.setMimeTypes(new String[] { "application/xml" }); |
| 78 | + final RequestTemplate template = new RequestTemplate(); |
| 79 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 80 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 81 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 82 | + |
| 83 | + interceptor.apply(template); |
| 84 | + |
| 85 | + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void shouldAddDefaultCompressionHeaderWhenMimeTypeNotSet() { |
| 90 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 91 | + properties.setMimeTypes(new String[] {}); |
| 92 | + final RequestTemplate template = new RequestTemplate(); |
| 93 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 94 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 95 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 96 | + |
| 97 | + interceptor.apply(template); |
| 98 | + |
| 99 | + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 100 | + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldAddDefaultCompressionHeaderWhenNullMimeTypeSet() { |
| 105 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 106 | + properties.setMimeTypes(null); |
| 107 | + final RequestTemplate template = new RequestTemplate(); |
| 108 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 109 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 110 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 111 | + |
| 112 | + interceptor.apply(template); |
| 113 | + |
| 114 | + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 115 | + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void shouldAddCustomCompressionHeader() { |
| 120 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 121 | + properties.setContentEncodingTypes(new String[] { "gzip" }); |
| 122 | + final RequestTemplate template = new RequestTemplate(); |
| 123 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 124 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 125 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 126 | + |
| 127 | + interceptor.apply(template); |
| 128 | + |
| 129 | + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); |
| 130 | + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip")); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void shouldThrowExceptionWhenContentEncodingTypesAreEmpty() { |
| 135 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 136 | + properties.setContentEncodingTypes(new String[] {}); |
| 137 | + final RequestTemplate template = new RequestTemplate(); |
| 138 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 139 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 140 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 141 | + |
| 142 | + assertThatThrownBy(() -> interceptor.apply(template)).isInstanceOf(IllegalStateException.class) |
| 143 | + .hasMessage("Invalid ContentEncodingTypes configuration"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void shouldThrowExceptionWhenNullContentEncodingTypes() { |
| 148 | + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); |
| 149 | + properties.setContentEncodingTypes(null); |
| 150 | + final RequestTemplate template = new RequestTemplate(); |
| 151 | + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); |
| 152 | + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); |
| 153 | + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); |
| 154 | + |
| 155 | + assertThatThrownBy(() -> interceptor.apply(template)).isInstanceOf(IllegalStateException.class) |
| 156 | + .hasMessage("Invalid ContentEncodingTypes configuration"); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments