Skip to content

Commit

Permalink
KNOX-1996: Code review fixes - Renaming test case and adding new test…
Browse files Browse the repository at this point in the history
… case for backedn url validation
  • Loading branch information
rajat.goel committed Sep 27, 2019
1 parent cb8b8ef commit 1f7a433
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
Expand Up @@ -150,7 +150,7 @@ public void beforeRequest(final Map<String, List<String>> headers) {
* ws://{host}:{port} which might or might not be right.
* @return Websocket backend url
*/
private synchronized String getMatchedBackendURL(final String path, URI requestURI) {
protected synchronized String getMatchedBackendURL(final String path, URI requestURI) {

final ServiceRegistry serviceRegistryService = services
.getService(ServiceType.SERVICE_REGISTRY_SERVICE);
Expand Down
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.knox.gateway.websockets;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.net.URI;
import java.util.Locale;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* A basic test that attempts to test the backedn url generated by gateway proxy.
*/
public class WebsocketBackendUrlTest extends WebsocketEchoTestBase {
private static Server backendServer;
public static URI backendServerUri;

public WebsocketBackendUrlTest() {
super();
}

@BeforeClass
public static void setUpBeforeClass() throws Exception {
WebsocketEchoTestBase.setUpBeforeClass();

backendServer = new Server();
ServerConnector connector = new ServerConnector(backendServer);
backendServer.addConnector(connector);

String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
backendServerUri = new URI(String.format(Locale.ROOT, "ws://%s:%d/testpart/", host, port));
WebsocketEchoTestBase.setupGatewayConfig(backendServerUri.toString());
}

@AfterClass
public static void tearDownAfterClass() {
}

/*
* Test url generated for websocket backend connection
*/
@Test
public void testWebsocketBackendUrl() throws Exception {
URI requestURI = new URI(serverUri.toString() + "gateway/websocket/123foo456bar/channels");
final String path = requestURI.getPath();
GatewayWebsocketHandler gwh = new GatewayWebsocketHandler(gatewayConfig, services);
String backendUrl = gwh.getMatchedBackendURL(path, requestURI);
String expectedBackendUrl = backendServerUri.toString() + "channels";
assertThat(backendUrl, is(expectedBackendUrl));
}
}
Expand Up @@ -51,15 +51,16 @@
*
* @since 0.10
*/
public class WebsocketEcho1Test extends WebsocketEchoTestBase {
public class WebsocketEchoHTTPServiceRoleTest extends WebsocketEchoTestBase {

public WebsocketEcho1Test() {
public WebsocketEchoHTTPServiceRoleTest() {
super();
}

@BeforeClass
public static void setUpBeforeClass() throws Exception {
WebsocketEchoTestBase.setUpBeforeClass("http");
WebsocketEchoTestBase.setUpBeforeClass();
WebsocketEchoTestBase.startServers("http");
}

@AfterClass
Expand Down
Expand Up @@ -58,7 +58,8 @@ public WebsocketEchoTest() {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
WebsocketEchoTestBase.setUpBeforeClass("ws");
WebsocketEchoTestBase.setUpBeforeClass();
WebsocketEchoTestBase.startServers("ws");
}

@AfterClass
Expand Down
Expand Up @@ -84,9 +84,9 @@ public class WebsocketEchoTestBase {
/**
* Mock gateway config
*/
private static GatewayConfig gatewayConfig;
public static GatewayConfig gatewayConfig;

private static GatewayServices services;
public static GatewayServices services;

/**
* URI for gateway server
Expand All @@ -103,13 +103,15 @@ public WebsocketEchoTestBase() {
super();
}

public static void setUpBeforeClass(String type) throws Exception {
public static void setUpBeforeClass() throws Exception {
topoDir = createDir();
dataDir = Paths.get(topoDir.getAbsolutePath(), "data").toAbsolutePath();
securityDir = dataDir.resolve("security");
keystoresDir = securityDir.resolve("keystores");
keystoreFile = keystoresDir.resolve("tls.jks");
}

public static void startServers(String type) throws Exception {
startWebsocketServer(type);
startGatewayServer();
}
Expand Down Expand Up @@ -201,7 +203,7 @@ private static void startGatewayServer() throws Exception {
* @param backend topology to use
* @throws IOException exception on setting up the gateway
*/
private static void setupGatewayConfig(final String backend) throws IOException {
public static void setupGatewayConfig(final String backend) throws IOException {
services = new DefaultGatewayServices();

URL serviceUrl = ClassLoader.getSystemResource("websocket-services");
Expand Down

0 comments on commit 1f7a433

Please sign in to comment.