|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.boot.web.embedded.tomcat; |
| 18 | + |
| 19 | +import org.apache.catalina.connector.Connector; |
| 20 | +import org.apache.coyote.http11.AbstractHttp11Protocol; |
| 21 | +import org.apache.coyote.http2.Http2Protocol; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.springframework.boot.web.server.Compression; |
| 26 | +import org.springframework.util.unit.DataSize; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link CompressionConnectorCustomizer} |
| 32 | + * |
| 33 | + * @author Rudy Adams |
| 34 | + */ |
| 35 | +public class CompressionConnectorCustomizerTests { |
| 36 | + |
| 37 | + private static final int MIN_SIZE = 100; |
| 38 | + |
| 39 | + private final String[] mimeTypes = { "text/html", "text/xml", "text/xhtml" }; |
| 40 | + |
| 41 | + private final String[] excludedUserAgents = { "SomeUserAgent", "AnotherUserAgent" }; |
| 42 | + |
| 43 | + private Compression compression; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() { |
| 47 | + this.compression = new Compression(); |
| 48 | + this.compression.setEnabled(true); |
| 49 | + this.compression.setMinResponseSize(DataSize.ofBytes(MIN_SIZE)); |
| 50 | + this.compression.setMimeTypes(this.mimeTypes); |
| 51 | + this.compression.setExcludedUserAgents(this.excludedUserAgents); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldCustomizeCompressionForHttp1AndHttp2Protocol() { |
| 56 | + CompressionConnectorCustomizer compressionConnectorCustomizer = new CompressionConnectorCustomizer( |
| 57 | + this.compression); |
| 58 | + Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); |
| 59 | + connector.addUpgradeProtocol(new Http2Protocol()); |
| 60 | + compressionConnectorCustomizer.customize(connector); |
| 61 | + AbstractHttp11Protocol abstractHttp11Protocol = (AbstractHttp11Protocol) connector |
| 62 | + .getProtocolHandler(); |
| 63 | + verifyHttp1(abstractHttp11Protocol); |
| 64 | + Http2Protocol http2Protocol = (Http2Protocol) connector.findUpgradeProtocols()[0]; |
| 65 | + verifyHttp2Upgrade(http2Protocol); |
| 66 | + } |
| 67 | + |
| 68 | + private void verifyHttp1(AbstractHttp11Protocol protocol) { |
| 69 | + compressionOn(protocol.getCompression()); |
| 70 | + minSize(protocol.getCompressionMinSize()); |
| 71 | + mimeType(protocol.getCompressibleMimeTypes()); |
| 72 | + excludedUserAgents(protocol.getNoCompressionUserAgents()); |
| 73 | + } |
| 74 | + |
| 75 | + private void verifyHttp2Upgrade(Http2Protocol protocol) { |
| 76 | + compressionOn(protocol.getCompression()); |
| 77 | + minSize(protocol.getCompressionMinSize()); |
| 78 | + mimeType(protocol.getCompressibleMimeTypes()); |
| 79 | + excludedUserAgents(protocol.getNoCompressionUserAgents()); |
| 80 | + excludedUserAgents(protocol.getNoCompressionUserAgents()); |
| 81 | + } |
| 82 | + |
| 83 | + private void compressionOn(String compression) { |
| 84 | + assertThat(compression).isEqualTo("on"); |
| 85 | + } |
| 86 | + |
| 87 | + private void minSize(int minSize) { |
| 88 | + assertThat(minSize).isEqualTo(MIN_SIZE); |
| 89 | + } |
| 90 | + |
| 91 | + private void mimeType(String[] mimeTypes) { |
| 92 | + assertThat(mimeTypes).isEqualTo(mimeTypes); |
| 93 | + } |
| 94 | + |
| 95 | + private void excludedUserAgents(String combinedUserAgents) { |
| 96 | + assertThat(combinedUserAgents).isEqualTo("SomeUserAgent,AnotherUserAgent"); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments