Skip to content

Commit 05d66a1

Browse files
committed
WebSocketUpgradeHandler.onCompleted should throw Exception when status != 101, close AsyncHttpClient#836
1 parent 89a811a commit 05d66a1

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

src/main/java/com/ning/http/client/ws/WebSocketUpgradeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public WebSocket onCompleted() throws Exception {
7777
for (WebSocketListener listener : listeners) {
7878
listener.onError(e);
7979
}
80-
return null;
80+
throw e;
8181
}
8282

8383
if (webSocket == null) {
84-
throw new IllegalStateException("WebSocket is null");
84+
throw new NullPointerException("webSocket");
8585
}
8686
return webSocket;
8787
}

src/test/java/com/ning/http/client/ws/CloseCodeReasonMessageTest.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@
1212
*/
1313
package com.ning.http.client.ws;
1414

15-
import com.ning.http.client.AsyncHttpClient;
16-
import com.ning.http.client.ws.WebSocket;
17-
import com.ning.http.client.ws.WebSocketCloseCodeReasonListener;
18-
import com.ning.http.client.ws.WebSocketListener;
19-
import com.ning.http.client.ws.WebSocketTextListener;
20-
import com.ning.http.client.ws.WebSocketUpgradeHandler;
15+
import static org.testng.Assert.assertEquals;
16+
import static org.testng.Assert.assertNotNull;
17+
import static org.testng.Assert.assertTrue;
2118

2219
import org.testng.annotations.Test;
2320

21+
import com.ning.http.client.AsyncHttpClient;
22+
2423
import java.util.concurrent.CountDownLatch;
24+
import java.util.concurrent.ExecutionException;
2525
import java.util.concurrent.atomic.AtomicReference;
2626

27-
import static org.testng.Assert.assertEquals;
28-
import static org.testng.Assert.assertNotNull;
29-
import static org.testng.Assert.assertTrue;
30-
3127
public abstract class CloseCodeReasonMessageTest extends TextMessageTest {
3228

3329
@Test(timeOut = 60000)
@@ -94,13 +90,41 @@ public void onError(Throwable t) {
9490
}
9591
}
9692

93+
@Test(timeOut = 60000, expectedExceptions = { ExecutionException.class })
94+
public void getWebSocketThrowsException() throws Throwable {
95+
final CountDownLatch latch = new CountDownLatch(1);
96+
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
97+
client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
98+
99+
@Override
100+
public void onMessage(String message) {
101+
}
102+
103+
@Override
104+
public void onOpen(com.ning.http.client.ws.WebSocket websocket) {
105+
}
106+
107+
@Override
108+
public void onClose(com.ning.http.client.ws.WebSocket websocket) {
109+
}
110+
111+
@Override
112+
public void onError(Throwable t) {
113+
latch.countDown();
114+
}
115+
}).build()).get();
116+
}
117+
118+
latch.await();
119+
}
120+
97121
@Test(timeOut = 60000)
98122
public void wrongStatusCode() throws Throwable {
99123
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
100124
final CountDownLatch latch = new CountDownLatch(1);
101125
final AtomicReference<Throwable> throwable = new AtomicReference<>();
102126

103-
WebSocket websocket = client.prepareGet("http://apache.org/").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
127+
client.prepareGet("http://apache.org/").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
104128

105129
@Override
106130
public void onMessage(String message) {
@@ -119,7 +143,7 @@ public void onError(Throwable t) {
119143
throwable.set(t);
120144
latch.countDown();
121145
}
122-
}).build()).get();
146+
}).build());
123147

124148
latch.await();
125149
assertNotNull(throwable.get());
@@ -133,7 +157,7 @@ public void wrongProtocolCode() throws Throwable {
133157
final CountDownLatch latch = new CountDownLatch(1);
134158
final AtomicReference<Throwable> throwable = new AtomicReference<>();
135159

136-
WebSocket websocket = client.prepareGet("ws://www.google.com/").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
160+
client.prepareGet("ws://www.google.com/").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
137161

138162
@Override
139163
public void onMessage(String message) {
@@ -152,7 +176,7 @@ public void onError(Throwable t) {
152176
throwable.set(t);
153177
latch.countDown();
154178
}
155-
}).build()).get();
179+
}).build());
156180

157181
latch.await();
158182
assertNotNull(throwable.get());

0 commit comments

Comments
 (0)