Skip to content

Commit 3f73055

Browse files
committed
1 parent 17a1a99 commit 3f73055

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class WebSocketUpgradeHandler implements UpgradeHandler<WebSocket>, Async
3636
private final long maxTextSize;
3737
private final AtomicBoolean ok = new AtomicBoolean(false);
3838
private final AtomicBoolean onSuccessCalled = new AtomicBoolean(false);
39+
private int status;
3940

4041
private WebSocketUpgradeHandler(Builder b) {
4142
l = b.l;
@@ -52,7 +53,7 @@ public final void onThrowable(Throwable t) {
5253
onFailure(t);
5354
}
5455

55-
public boolean touchSuccess(){
56+
public boolean touchSuccess() {
5657
return onSuccessCalled.getAndSet(true);
5758
}
5859

@@ -69,6 +70,7 @@ public final STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exce
6970
*/
7071
@Override
7172
public final STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
73+
status = responseStatus.getStatusCode();
7274
if (responseStatus.getStatusCode() == 101) {
7375
return STATE.UPGRADE;
7476
} else {
@@ -89,6 +91,14 @@ public final STATE onHeadersReceived(HttpResponseHeaders headers) throws Excepti
8991
*/
9092
@Override
9193
public final WebSocket onCompleted() throws Exception {
94+
95+
if (status != 101) {
96+
for (WebSocketListener w : l) {
97+
w.onError(new IllegalStateException(String.format("Invalid Status Code %d", status)));
98+
}
99+
return null;
100+
}
101+
92102
if (webSocket == null) {
93103
throw new IllegalStateException("WebSocket is null");
94104
}
@@ -202,6 +212,7 @@ public Builder setMaxTextSize(long maxTextSize) {
202212

203213
/**
204214
* Build a {@link WebSocketUpgradeHandler}
215+
*
205216
* @return a {@link WebSocketUpgradeHandler}
206217
*/
207218
public WebSocketUpgradeHandler build() {

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
*/
1313
package org.asynchttpclient.websocket;
1414

15-
import static org.testng.Assert.*;
16-
17-
import java.util.concurrent.CountDownLatch;
18-
import java.util.concurrent.atomic.AtomicReference;
19-
2015
import org.asynchttpclient.AsyncHttpClient;
2116
import org.eclipse.jetty.websocket.server.WebSocketHandler;
2217
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
2318
import org.testng.annotations.Test;
2419

20+
import java.util.concurrent.CountDownLatch;
21+
import java.util.concurrent.atomic.AtomicReference;
22+
23+
import static org.testng.Assert.assertEquals;
24+
import static org.testng.Assert.assertNotNull;
25+
import static org.testng.Assert.assertTrue;
26+
2527
public abstract class CloseCodeReasonMessageTest extends AbstractBasicTest {
2628

2729
@Override
@@ -99,4 +101,45 @@ public void onError(Throwable t) {
99101
latch.countDown();
100102
}
101103
}
104+
105+
106+
@Test(timeOut = 60000)
107+
public void wrongStatusCode() throws Throwable {
108+
AsyncHttpClient c = getAsyncHttpClient(null);
109+
try {
110+
final CountDownLatch latch = new CountDownLatch(1);
111+
final AtomicReference<Throwable> throwable = new AtomicReference<Throwable>();
112+
113+
WebSocket websocket = c.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
114+
115+
@Override
116+
public void onMessage(String message) {
117+
}
118+
119+
@Override
120+
public void onFragment(String fragment, boolean last) {
121+
}
122+
123+
@Override
124+
public void onOpen(WebSocket websocket) {
125+
}
126+
127+
@Override
128+
public void onClose(WebSocket websocket) {
129+
}
130+
131+
@Override
132+
public void onError(Throwable t) {
133+
throwable.set(t);
134+
latch.countDown();
135+
}
136+
}).build()).get();
137+
138+
latch.await();
139+
assertNotNull(throwable.get());
140+
assertEquals(throwable.get().getClass(), IllegalStateException.class);
141+
} finally {
142+
c.close();
143+
}
144+
}
102145
}

0 commit comments

Comments
 (0)