Skip to content

Commit 1866a34

Browse files
committed
WebSocketUpgradeHandler.onCompleted should throw Exception when status != 101, close AsyncHttpClient#836
1 parent 961e0f9 commit 1866a34

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

api/src/main/java/org/asynchttpclient/ws/WebSocketUpgradeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public final WebSocket onCompleted() throws Exception {
9191
for (WebSocketListener listener : listeners) {
9292
listener.onError(e);
9393
}
94-
return null;
94+
throw e;
9595
}
9696

9797
if (webSocket == null) {
98-
throw new IllegalStateException("WebSocket is null");
98+
throw new NullPointerException("webSocket");
9999
}
100100
return webSocket;
101101
}

api/src/test/java/org/asynchttpclient/ws/CloseCodeReasonMessageTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.testng.annotations.Test;
2828

2929
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.ExecutionException;
3031
import java.util.concurrent.atomic.AtomicReference;
3132

3233
public abstract class CloseCodeReasonMessageTest extends AbstractBasicTest {
@@ -100,6 +101,34 @@ public void onError(Throwable t) {
100101
}
101102
}
102103

104+
@Test(timeOut = 60000, expectedExceptions = { ExecutionException.class })
105+
public void getWebSocketThrowsException() throws Throwable {
106+
final CountDownLatch latch = new CountDownLatch(1);
107+
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
108+
client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
109+
110+
@Override
111+
public void onMessage(String message) {
112+
}
113+
114+
@Override
115+
public void onOpen(WebSocket websocket) {
116+
}
117+
118+
@Override
119+
public void onClose(WebSocket websocket) {
120+
}
121+
122+
@Override
123+
public void onError(Throwable t) {
124+
latch.countDown();
125+
}
126+
}).build()).get();
127+
}
128+
129+
latch.await();
130+
}
131+
103132
@Test(timeOut = 60000)
104133
public void wrongStatusCode() throws Throwable {
105134
try (AsyncHttpClient c = getAsyncHttpClient(null)) {
@@ -125,7 +154,7 @@ public void onError(Throwable t) {
125154
throwable.set(t);
126155
latch.countDown();
127156
}
128-
}).build()).get();
157+
}).build());
129158

130159
latch.await();
131160
assertNotNull(throwable.get());
@@ -158,7 +187,7 @@ public void onError(Throwable t) {
158187
throwable.set(t);
159188
latch.countDown();
160189
}
161-
}).build()).get();
190+
}).build());
162191

163192
latch.await();
164193
assertNotNull(throwable.get());

0 commit comments

Comments
 (0)