|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | + * 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 | + |
| 17 | +package org.springframework.boot.web.embedded.jetty; |
| 18 | + |
| 19 | +import java.net.InetSocketAddress; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory; |
| 24 | +import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; |
| 25 | +import org.eclipse.jetty.server.ConnectionFactory; |
| 26 | +import org.eclipse.jetty.server.HttpConnectionFactory; |
| 27 | +import org.eclipse.jetty.server.Server; |
| 28 | +import org.eclipse.jetty.server.SslConnectionFactory; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import org.springframework.boot.web.server.Http2; |
| 32 | +import org.springframework.boot.web.server.Ssl; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link SslServerCustomizer}. |
| 38 | + * |
| 39 | + * @author Andy Wilkinson |
| 40 | + */ |
| 41 | +public class SslServerCustomizerTests { |
| 42 | + |
| 43 | + @Test |
| 44 | + @SuppressWarnings("rawtypes") |
| 45 | + public void whenHttp2IsNotEnabledServerConnectorHasSslAndHttpConnectionFactories() { |
| 46 | + Server server = createCustomizedServer(); |
| 47 | + assertThat(server.getConnectors()).hasSize(1); |
| 48 | + List<ConnectionFactory> factories = new ArrayList<>( |
| 49 | + server.getConnectors()[0].getConnectionFactories()); |
| 50 | + assertThat(factories).extracting((factory) -> (Class) factory.getClass()) |
| 51 | + .containsExactly(SslConnectionFactory.class, HttpConnectionFactory.class); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @SuppressWarnings("rawtypes") |
| 56 | + public void whenHttp2IsEnabledServerConnectorsHasSslAlpnH2AndHttpConnectionFactories() { |
| 57 | + Http2 http2 = new Http2(); |
| 58 | + http2.setEnabled(true); |
| 59 | + Server server = createCustomizedServer(http2); |
| 60 | + assertThat(server.getConnectors()).hasSize(1); |
| 61 | + List<ConnectionFactory> factories = new ArrayList<>( |
| 62 | + server.getConnectors()[0].getConnectionFactories()); |
| 63 | + assertThat(factories).extracting((factory) -> (Class) factory.getClass()) |
| 64 | + .containsExactly(SslConnectionFactory.class, |
| 65 | + ALPNServerConnectionFactory.class, |
| 66 | + HTTP2ServerConnectionFactory.class, HttpConnectionFactory.class); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void alpnConnectionFactoryHasNullDefaultProtocolToAllowNegotiationToHttp11() { |
| 71 | + Http2 http2 = new Http2(); |
| 72 | + http2.setEnabled(true); |
| 73 | + Server server = createCustomizedServer(http2); |
| 74 | + assertThat(server.getConnectors()).hasSize(1); |
| 75 | + List<ConnectionFactory> factories = new ArrayList<>( |
| 76 | + server.getConnectors()[0].getConnectionFactories()); |
| 77 | + assertThat(((ALPNServerConnectionFactory) factories.get(1)).getDefaultProtocol()) |
| 78 | + .isNull(); |
| 79 | + } |
| 80 | + |
| 81 | + private Server createCustomizedServer() { |
| 82 | + return createCustomizedServer(new Http2()); |
| 83 | + } |
| 84 | + |
| 85 | + private Server createCustomizedServer(Http2 http2) { |
| 86 | + Ssl ssl = new Ssl(); |
| 87 | + ssl.setKeyStore("classpath:test.jks"); |
| 88 | + return createCustomizedServer(ssl, http2); |
| 89 | + } |
| 90 | + |
| 91 | + private Server createCustomizedServer(Ssl ssl, Http2 http2) { |
| 92 | + Server server = new Server(); |
| 93 | + new SslServerCustomizer(new InetSocketAddress(0), ssl, null, http2) |
| 94 | + .customize(server); |
| 95 | + return server; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments