Skip to content

Commit dde2652

Browse files
author
Stephane Landelle
committed
Backport test for wss proxy tunneling: both providers are fine on this branch
1 parent 28ce378 commit dde2652

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.websocket;
14+
15+
import static org.testng.Assert.assertEquals;
16+
17+
import java.io.File;
18+
import java.net.URL;
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.atomic.AtomicReference;
21+
22+
import javax.servlet.http.HttpServletRequest;
23+
24+
import org.eclipse.jetty.server.Connector;
25+
import org.eclipse.jetty.server.Server;
26+
import org.eclipse.jetty.server.handler.AbstractHandler;
27+
import org.eclipse.jetty.server.handler.ProxyHandler;
28+
import org.eclipse.jetty.server.nio.SelectChannelConnector;
29+
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
30+
import org.testng.annotations.AfterClass;
31+
import org.testng.annotations.BeforeClass;
32+
import org.testng.annotations.Test;
33+
34+
import com.ning.http.client.AsyncHttpClient;
35+
import com.ning.http.client.AsyncHttpClientConfig;
36+
import com.ning.http.client.ProxyServer;
37+
import com.ning.http.client.websocket.TextMessageTest.EchoTextWebSocket;
38+
39+
/**
40+
* Proxy usage tests.
41+
*/
42+
public abstract class ProxyTunnellingTest extends AbstractBasicTest {
43+
44+
int port2;
45+
private Server server2;
46+
47+
public AbstractHandler configureHandler() throws Exception {
48+
ProxyHandler proxy = new ProxyHandler();
49+
return proxy;
50+
}
51+
52+
@BeforeClass(alwaysRun = true)
53+
public void setUpGlobal() throws Exception {
54+
server2 = new Server();
55+
56+
port1 = findFreePort();
57+
port2 = findFreePort();
58+
59+
Connector listener = new SelectChannelConnector();
60+
61+
listener.setHost("127.0.0.1");
62+
listener.setPort(port1);
63+
64+
addConnector(listener);
65+
66+
SslSelectChannelConnector connector = new SslSelectChannelConnector();
67+
connector.setHost("127.0.0.1");
68+
connector.setPort(port2);
69+
70+
ClassLoader cl = getClass().getClassLoader();
71+
URL keystoreUrl = cl.getResource("ssltest-keystore.jks");
72+
String keyStoreFile = new File(keystoreUrl.toURI()).getAbsolutePath();
73+
connector.setKeystore(keyStoreFile);
74+
connector.setKeyPassword("changeit");
75+
connector.setKeystoreType("JKS");
76+
77+
server2.addConnector(connector);
78+
79+
setHandler(configureHandler());
80+
start();
81+
82+
server2.setHandler(getWebSocketHandler());
83+
server2.start();
84+
log.info("Local HTTP server started successfully");
85+
}
86+
87+
@Override
88+
public WebSocketHandler getWebSocketHandler() {
89+
return new WebSocketHandler() {
90+
@Override
91+
public org.eclipse.jetty.websocket.WebSocket doWebSocketConnect(HttpServletRequest httpServletRequest, String s) {
92+
return new EchoTextWebSocket();
93+
}
94+
};
95+
}
96+
97+
@AfterClass(alwaysRun = true)
98+
public void tearDownGlobal() throws Exception {
99+
stop();
100+
server2.stop();
101+
}
102+
103+
protected String getTargetUrl() {
104+
return String.format("wss://127.0.0.1:%d/", port2);
105+
}
106+
107+
@Test(timeOut = 60000)
108+
public void echoText() throws Exception {
109+
110+
ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", port1);
111+
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setProxyServer(ps).build();
112+
AsyncHttpClient asyncHttpClient = getAsyncHttpClient(config);
113+
try {
114+
final CountDownLatch latch = new CountDownLatch(1);
115+
final AtomicReference<String> text = new AtomicReference<String>("");
116+
117+
WebSocket websocket = asyncHttpClient.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
118+
119+
@Override
120+
public void onMessage(String message) {
121+
text.set(message);
122+
latch.countDown();
123+
}
124+
125+
@Override
126+
public void onFragment(String fragment, boolean last) {
127+
}
128+
129+
@Override
130+
public void onOpen(WebSocket websocket) {
131+
}
132+
133+
@Override
134+
public void onClose(WebSocket websocket) {
135+
latch.countDown();
136+
}
137+
138+
@Override
139+
public void onError(Throwable t) {
140+
t.printStackTrace();
141+
latch.countDown();
142+
}
143+
}).build()).get();
144+
145+
websocket.sendTextMessage("ECHO");
146+
147+
latch.await();
148+
assertEquals(text.get(), "ECHO");
149+
} finally {
150+
asyncHttpClient.close();
151+
}
152+
}
153+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.websocket.grizzly;
14+
15+
import org.testng.annotations.Test;
16+
17+
import com.ning.http.client.AsyncHttpClient;
18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
import com.ning.http.client.async.ProviderUtil;
20+
import com.ning.http.client.websocket.ProxyTunnellingTest;
21+
22+
@Test
23+
public class GrizzlyProxyTunnellingTest extends ProxyTunnellingTest {
24+
25+
@Override
26+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
27+
return ProviderUtil.grizzlyProvider(config);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.websocket.netty;
14+
15+
import org.testng.annotations.Test;
16+
17+
import com.ning.http.client.AsyncHttpClient;
18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
import com.ning.http.client.async.ProviderUtil;
20+
import com.ning.http.client.websocket.ProxyTunnellingTest;
21+
22+
@Test
23+
public class NettyProxyTunnellingTest extends ProxyTunnellingTest {
24+
25+
@Override
26+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
27+
return ProviderUtil.nettyProvider(config);
28+
}
29+
}

0 commit comments

Comments
 (0)