|
| 1 | +/* |
| 2 | + Copyright (C) 2013-2024 Expedia 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 com.hotels.styx.proxy; |
| 17 | + |
| 18 | +import io.netty.channel.ChannelHandlerContext; |
| 19 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 20 | +import io.netty.handler.codec.http.HttpContentEncoder; |
| 21 | +import io.netty.handler.codec.http.HttpResponse; |
| 22 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 23 | +import io.netty.handler.codec.http.HttpVersion; |
| 24 | +import java.util.stream.Stream; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.Arguments; |
| 28 | +import org.junit.jupiter.params.provider.MethodSource; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 33 | +import static org.mockito.Answers.RETURNS_DEEP_STUBS; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | + |
| 36 | + |
| 37 | +public class HttpCompressorTest { |
| 38 | + private static final String COMPRESSIBLE_MIME_TYPE = "application/json"; |
| 39 | + private static final String NOT_COMPRESSIBLE_MIME_TYPE = "image/jpeg"; |
| 40 | + private HttpCompressor compressor; |
| 41 | + private ChannelHandlerContext ctx; |
| 42 | + |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setUp() throws Exception { |
| 46 | + compressor = new HttpCompressor(); |
| 47 | + ctx = mock(ChannelHandlerContext.class, RETURNS_DEEP_STUBS); |
| 48 | + compressor.handlerAdded(ctx); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("responseEncodingParameters") |
| 54 | + public void validateResponseEncoding(final String acceptEncoding, final String contentType, final String expectedEncoding) throws Exception { |
| 55 | + HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); |
| 56 | + httpResponse.headers().add("Content-Type", contentType); |
| 57 | + HttpContentEncoder.Result result = compressor.beginEncode(httpResponse, acceptEncoding); |
| 58 | + if (expectedEncoding == null) { |
| 59 | + assertNull(result); |
| 60 | + } else { |
| 61 | + assertEquals(result.targetContentEncoding(), expectedEncoding); |
| 62 | + assertNotNull(result.contentEncoder()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static Stream<Arguments> responseEncodingParameters() { |
| 67 | + // Note: "br" is brotli compression |
| 68 | + return Stream.of( |
| 69 | + Arguments.of("br", COMPRESSIBLE_MIME_TYPE, "br"), |
| 70 | + Arguments.of("br", NOT_COMPRESSIBLE_MIME_TYPE, null), |
| 71 | + Arguments.of("gzip", COMPRESSIBLE_MIME_TYPE, "gzip"), |
| 72 | + Arguments.of("gzip", NOT_COMPRESSIBLE_MIME_TYPE, null), |
| 73 | + Arguments.of("br, gzip", COMPRESSIBLE_MIME_TYPE, "br"), |
| 74 | + Arguments.of("gzip, br", COMPRESSIBLE_MIME_TYPE, "br"), |
| 75 | + Arguments.of("", COMPRESSIBLE_MIME_TYPE, null), |
| 76 | + Arguments.of("", NOT_COMPRESSIBLE_MIME_TYPE, null), |
| 77 | + Arguments.of("br, garbage", COMPRESSIBLE_MIME_TYPE, "br"), |
| 78 | + Arguments.of("gzip, garbage", COMPRESSIBLE_MIME_TYPE, "gzip"), |
| 79 | + Arguments.of("garbage", COMPRESSIBLE_MIME_TYPE, null), |
| 80 | + Arguments.of("?", COMPRESSIBLE_MIME_TYPE, null), |
| 81 | + Arguments.of(",", COMPRESSIBLE_MIME_TYPE, null) |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments