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

Commit 66a9a6c

Browse files
committed
Fix the Grizzly side of AsyncHttpClient#91. - re-worked tests a bit.
1 parent 3a494e5 commit 66a9a6c

File tree

6 files changed

+137
-12
lines changed

6 files changed

+137
-12
lines changed

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.ning.http.client.listener.TransferCompletionHandler;
4141
import com.ning.http.client.websocket.WebSocket;
4242
import com.ning.http.client.websocket.WebSocketByteListener;
43+
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
4344
import com.ning.http.client.websocket.WebSocketListener;
4445
import com.ning.http.client.websocket.WebSocketPingListener;
4546
import com.ning.http.client.websocket.WebSocketPongListener;
@@ -104,6 +105,7 @@
104105
import org.glassfish.grizzly.websockets.Version;
105106
import org.glassfish.grizzly.websockets.WebSocketEngine;
106107
import org.glassfish.grizzly.websockets.WebSocketFilter;
108+
import org.glassfish.grizzly.websockets.draft06.ClosingFrame;
107109
import org.slf4j.Logger;
108110
import org.slf4j.LoggerFactory;
109111

@@ -2665,7 +2667,12 @@ private static final class AHCWebSocketListenerAdapter implements org.glassfish.
26652667

26662668
@Override
26672669
public void onClose(org.glassfish.grizzly.websockets.WebSocket gWebSocket, DataFrame dataFrame) {
2668-
ahcListener.onClose(webSocket);
2670+
if (WebSocketCloseCodeReasonListener.class.isAssignableFrom(ahcListener.getClass())) {
2671+
ClosingFrame cf = ClosingFrame.class.cast(dataFrame);
2672+
WebSocketCloseCodeReasonListener.class.cast(ahcListener).onClose(webSocket, cf.getCode(), cf.getReason());
2673+
} else {
2674+
ahcListener.onClose(webSocket);
2675+
}
26692676
}
26702677

26712678
@Override

src/main/java/com/ning/http/client/websocket/WebSocketCloseCodeReasonListener.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
/**
1616
* Extend the normal close listener with one that support the WebSocket's code and reason.
1717
* @See http://tools.ietf.org/html/rfc6455#section-5.5.1
18-
*
19-
* NOTE: This listener is currently only supported by the NettyAsyncHttpProvider
20-
*
2118
*/
2219
public interface WebSocketCloseCodeReasonListener {
2320

src/test/java/com/ning/http/client/websocket/netty/NettyCloseCodeReasonMessageTest.java renamed to src/test/java/com/ning/http/client/websocket/CloseCodeReasonMessageTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
13-
package com.ning.http.client.websocket.netty;
13+
package com.ning.http.client.websocket;
1414

1515
import com.ning.http.client.AsyncHttpClient;
1616
import com.ning.http.client.AsyncHttpClientConfig;
@@ -20,6 +20,7 @@
2020
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
2121
import com.ning.http.client.websocket.WebSocketListener;
2222
import com.ning.http.client.websocket.WebSocketUpgradeHandler;
23+
import com.ning.http.client.websocket.netty.NettyTextMessageTest;
2324
import org.eclipse.jetty.server.nio.SelectChannelConnector;
2425
import org.testng.annotations.BeforeClass;
2526
import org.testng.annotations.Test;
@@ -30,7 +31,7 @@
3031
import static org.testng.Assert.assertEquals;
3132
import static org.testng.Assert.assertTrue;
3233

33-
public class NettyCloseCodeReasonMessageTest extends NettyTextMessageTest {
34+
public abstract class CloseCodeReasonMessageTest extends TextMessageTest {
3435

3536
@Test(timeOut = 60000)
3637
public void onCloseWithCode() throws Throwable {
@@ -44,7 +45,7 @@ public void onCloseWithCode() throws Throwable {
4445
websocket.close();
4546

4647
latch.await();
47-
assertEquals(text.get(), "1000-Normal closure; the connection successfully completed whatever purpose for which it was created.");
48+
assertTrue(text.get().startsWith("1000"));
4849
}
4950

5051
@Test(timeOut = 60000)
@@ -57,7 +58,13 @@ public void onCloseWithCodeServerClose() throws Throwable {
5758
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get();
5859

5960
latch.await();
60-
assertTrue(text.get().startsWith("1000-Idle"));
61+
final String[] parts = text.get().split(" ");
62+
assertEquals(parts.length, 5);
63+
assertEquals(parts[0], "1000-Idle");
64+
assertEquals(parts[1], "for");
65+
assertTrue(Integer.parseInt(parts[2].substring(0, parts[2].indexOf('m'))) > 10000);
66+
assertEquals(parts[3], ">");
67+
assertEquals(parts[4], "10000ms");
6168
}
6269

6370
public final static class Listener implements WebSocketListener, WebSocketCloseCodeReasonListener {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ public org.eclipse.jetty.websocket.WebSocket doWebSocketConnect(HttpServletReque
6666
};
6767
}
6868

69-
@Override
70-
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
71-
return null; //To change body of implemented methods use File | Settings | File Templates.
72-
}
69+
7370

7471
@Test(timeOut = 60000)
7572
public void onOpen() throws Throwable {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package com.ning.http.client.websocket.grizzly;
41+
42+
import com.ning.http.client.AsyncHttpClient;
43+
import com.ning.http.client.AsyncHttpClientConfig;
44+
import com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider;
45+
import com.ning.http.client.websocket.CloseCodeReasonMessageTest;
46+
import org.testng.annotations.Test;
47+
48+
public class GrizzlyCloseCodeReasonMsgTest extends CloseCodeReasonMessageTest {
49+
50+
@Override
51+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
52+
if (config == null) {
53+
config = new AsyncHttpClientConfig.Builder().build();
54+
}
55+
return new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
56+
}
57+
58+
@Override
59+
@Test
60+
public void onCloseWithCode() throws Throwable {
61+
super.onCloseWithCode(); //To change body of overridden methods use File | Settings | File Templates.
62+
}
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package com.ning.http.client.websocket.netty;
41+
42+
import com.ning.http.client.AsyncHttpClient;
43+
import com.ning.http.client.AsyncHttpClientConfig;
44+
import com.ning.http.client.async.ProviderUtil;
45+
import com.ning.http.client.websocket.CloseCodeReasonMessageTest;
46+
47+
public class NettyCloseCodeReasonMsgTest extends CloseCodeReasonMessageTest {
48+
49+
@Override
50+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
51+
return ProviderUtil.nettyProvider(config);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)