|
| 1 | +/* |
| 2 | + * This program and the accompanying materials are made available under the terms of the |
| 3 | + * Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 4 | + * https://www.eclipse.org/legal/epl-v20.html |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: EPL-2.0 |
| 7 | + * |
| 8 | + * Copyright Contributors to the Zowe Project. |
| 9 | + */ |
| 10 | + |
| 11 | +package org.zowe.apiml.gateway.ws; |
| 12 | + |
| 13 | +import org.apache.http.conn.ssl.TrustStrategy; |
| 14 | +import org.apache.http.ssl.SSLContexts; |
| 15 | +import org.eclipse.jetty.util.ssl.SslContextFactory; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.springframework.cloud.client.DefaultServiceInstance; |
| 18 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 19 | +import org.springframework.web.socket.WebSocketHttpHeaders; |
| 20 | +import org.springframework.web.socket.WebSocketSession; |
| 21 | +import org.zowe.apiml.product.routing.RoutedService; |
| 22 | +import org.zowe.apiml.product.routing.RoutedServices; |
| 23 | + |
| 24 | +import javax.net.ssl.SSLContext; |
| 25 | +import java.net.URI; |
| 26 | +import java.security.cert.X509Certificate; |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.*; |
| 30 | +import static org.mockito.Mockito.*; |
| 31 | + |
| 32 | +class WebSocketProxyServerHandlerTest { |
| 33 | + private WebSocketSession session; |
| 34 | + private WebSocketProxyServerHandler webSocketProxyServerHandler; |
| 35 | + private SslContextFactory jettySslContextFactory; |
| 36 | + private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class); |
| 37 | + |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + public void setup() { |
| 41 | + session = mock(WebSocketSession.class); |
| 42 | +// session = new StandardWebSocketSession(); |
| 43 | +// jettySslContextFactory = mock(SslContextFactory.class); |
| 44 | + jettySslContextFactory = new SslContextFactory(); |
| 45 | + webSocketProxyServerHandler = new WebSocketProxyServerHandler(discoveryClient, jettySslContextFactory); |
| 46 | + } |
| 47 | + |
| 48 | +// @Test |
| 49 | + public void should() throws Exception { |
| 50 | + jettySslContextFactory.setKeyStoreType("PKCS12"); |
| 51 | + jettySslContextFactory.setCertAlias("localhost"); |
| 52 | + jettySslContextFactory.setTrustStoreType("type"); |
| 53 | + jettySslContextFactory.setProtocol("SSL"); |
| 54 | + jettySslContextFactory.setKeyStorePath("src/test/resources/certs/keystore.jks"); |
| 55 | + jettySslContextFactory.setTrustStorePath("src/test/resources/certs/truststore.jks"); |
| 56 | + jettySslContextFactory.setKeyManagerFactoryAlgorithm("SunX509"); |
| 57 | + jettySslContextFactory.setTrustManagerFactoryAlgorithm("PKIX"); |
| 58 | + jettySslContextFactory.setKeyStorePassword("pass"); |
| 59 | + jettySslContextFactory.setTrustStorePassword("pass"); |
| 60 | + TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
| 61 | + public boolean isTrusted(X509Certificate[] certificate, String authType) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + }; |
| 65 | + SSLContext sslContext = SSLContexts.custom() |
| 66 | + .loadTrustMaterial(null, acceptingTrustStrategy).build(); |
| 67 | +// SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 68 | + |
| 69 | + jettySslContextFactory.setSslContext(sslContext); |
| 70 | +// when(session.isOpen()).thenReturn(true); |
| 71 | + RoutedServices routedServices = new RoutedServices(); |
| 72 | + routedServices.addRoutedService( |
| 73 | + new RoutedService("api-v1", "api/v1", "/service/api/v1")); |
| 74 | + routedServices.addRoutedService( |
| 75 | + new RoutedService("ui-v1", "ui/v1", "/service")); |
| 76 | + routedServices.addRoutedService( |
| 77 | + new RoutedService("ws-v1", "ws/v1", "/service/ws")); |
| 78 | + WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); |
| 79 | + |
| 80 | + headers.add("X-Test", "value"); |
| 81 | + when(session.getUri()).thenReturn(new URI("/ws/v1/service/uppercase")); |
| 82 | + when(discoveryClient.getInstances("service")).thenReturn( |
| 83 | + Collections.singletonList(new DefaultServiceInstance("service", "localhost", 80, false, null))); |
| 84 | + when(session.getHandshakeHeaders()).thenReturn(headers); |
| 85 | + webSocketProxyServerHandler.addRoutedServices("service", routedServices); |
| 86 | + webSocketProxyServerHandler.afterConnectionEstablished(session); |
| 87 | + assertTrue(session.isOpen()); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +} |
0 commit comments