Skip to content

Commit 6abc9e4

Browse files
committed
Added test for #102.
1 parent f970bde commit 6abc9e4

File tree

4 files changed

+179
-1
lines changed

4 files changed

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

src/test/java/com/ning/http/client/websocket/TextMessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
public abstract class TextMessageTest extends AbstractBasicTest {
2929

30-
private final class EchoTextWebSocket implements org.eclipse.jetty.websocket.WebSocket, org.eclipse.jetty.websocket.WebSocket.OnTextMessage {
30+
public static final class EchoTextWebSocket implements org.eclipse.jetty.websocket.WebSocket, org.eclipse.jetty.websocket.WebSocket.OnTextMessage {
3131

3232
private Connection connection;
3333

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)