Skip to content
This repository was archived by the owner on Mar 6, 2021. It is now read-only.

Commit 5b46273

Browse files
committed
1 parent 9c22be1 commit 5b46273

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2012 Sonatype, Inc. 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+
14+
package com.ning.http.client.websocket;
15+
16+
17+
import com.ning.http.client.AsyncHttpClient;
18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
import org.eclipse.jetty.server.Connector;
20+
import org.eclipse.jetty.server.Request;
21+
import org.eclipse.jetty.server.Server;
22+
import org.eclipse.jetty.server.handler.AbstractHandler;
23+
import org.eclipse.jetty.server.handler.HandlerList;
24+
import org.eclipse.jetty.server.nio.SelectChannelConnector;
25+
import org.testng.annotations.BeforeClass;
26+
import org.testng.annotations.Test;
27+
28+
import javax.servlet.ServletException;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
import java.io.IOException;
32+
import java.util.concurrent.CountDownLatch;
33+
import java.util.concurrent.atomic.AtomicReference;
34+
35+
import static org.testng.Assert.assertEquals;
36+
37+
public abstract class RedirectTest extends AbstractBasicTest {
38+
39+
protected int port2;
40+
41+
// ------------------------------------------ Methods from AbstractBasicTest
42+
43+
@BeforeClass
44+
@Override
45+
public void setUpGlobal() throws Exception {
46+
port1 = findFreePort();
47+
48+
_connector = new SelectChannelConnector();
49+
_connector.setPort(port1);
50+
51+
addConnector(_connector);
52+
53+
54+
55+
56+
port2 = findFreePort();
57+
final SelectChannelConnector connector2 = new SelectChannelConnector();
58+
connector2.setPort(port2);
59+
addConnector(connector2);
60+
WebSocketHandler _wsHandler = getWebSocketHandler();
61+
HandlerList list = new HandlerList();
62+
list.addHandler(new AbstractHandler() {
63+
@Override
64+
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
65+
if (request.getLocalPort() == port2) {
66+
httpServletResponse.sendRedirect(getTargetUrl());
67+
}
68+
}
69+
});
70+
list.addHandler(_wsHandler);
71+
setHandler(list);
72+
73+
start();
74+
log.info("Local HTTP server started successfully");
75+
}
76+
77+
@Override
78+
public WebSocketHandler getWebSocketHandler() {
79+
return new WebSocketHandler() {
80+
@Override
81+
public org.eclipse.jetty.websocket.WebSocket doWebSocketConnect(HttpServletRequest httpServletRequest, String s) {
82+
return new TextMessageTest.EchoTextWebSocket();
83+
}
84+
};
85+
}
86+
87+
// ------------------------------------------------------------ Test Methods
88+
89+
@Test(timeOut = 60000)
90+
public void testRedirectToWSResource() throws Exception {
91+
AsyncHttpClient c = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build());
92+
final CountDownLatch latch = new CountDownLatch(1);
93+
final AtomicReference<String> text = new AtomicReference<String>("");
94+
95+
WebSocket websocket = c.prepareGet(getRedirectURL())
96+
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {
97+
98+
@Override
99+
public void onOpen(com.ning.http.client.websocket.WebSocket websocket) {
100+
text.set("OnOpen");
101+
latch.countDown();
102+
}
103+
104+
@Override
105+
public void onClose(com.ning.http.client.websocket.WebSocket websocket) {
106+
}
107+
108+
@Override
109+
public void onError(Throwable t) {
110+
t.printStackTrace();
111+
latch.countDown();
112+
}
113+
}).build()).get();
114+
115+
116+
latch.await();
117+
assertEquals(text.get(), "OnOpen");
118+
websocket.close();
119+
}
120+
121+
122+
// --------------------------------------------------------- Private Methods
123+
124+
125+
private String getRedirectURL() {
126+
return String.format("ws://127.0.0.1:%d/", port2);
127+
}
128+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2012 Sonatype, Inc. 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+
14+
package com.ning.http.client.websocket.grizzly;
15+
16+
import com.ning.http.client.AsyncHttpClient;
17+
import com.ning.http.client.AsyncHttpClientConfig;
18+
import com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider;
19+
import com.ning.http.client.websocket.RedirectTest;
20+
21+
public class GrizzlyRedirectTest extends RedirectTest {
22+
23+
@Override
24+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
25+
if (config == null) {
26+
config = new AsyncHttpClientConfig.Builder().build();
27+
}
28+
return new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2012 Sonatype, Inc. 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 com.ning.http.client.AsyncHttpClient;
16+
import com.ning.http.client.AsyncHttpClientConfig;
17+
import com.ning.http.client.async.ProviderUtil;
18+
import com.ning.http.client.websocket.RedirectTest;
19+
20+
public class NettyRedirectTest extends RedirectTest {
21+
22+
@Override
23+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
24+
return ProviderUtil.nettyProvider(config);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)