use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method readTimeoutAppliesWithinFrames.
/**
* There's no read timeout when reading the first byte of a new frame. But as soon as we start
* reading a frame we enable the read timeout. In this test we have the server returning the first
* byte of a frame but no more frames.
*/
@Test
public void readTimeoutAppliesWithinFrames() {
webServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
return upgradeResponse(request).setBody(// Truncated frame.
new Buffer().write(ByteString.decodeHex("81"))).removeHeader("Content-Length").setSocketPolicy(SocketPolicy.KEEP_OPEN);
}
});
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
assertThat(webSocket.close(1000, null)).isFalse();
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method textMessage.
@Test
public void textMessage() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
webSocket.send("Hello, WebSockets!");
serverListener.assertTextMessage("Hello, WebSockets!");
closeWebSockets(webSocket, server);
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method closeWithoutSuccessfulConnect.
/**
* https://github.com/square/okhttp/issues/5705
*/
@Test
public void closeWithoutSuccessfulConnect() {
Request request = new Request.Builder().url(webServer.url("/")).build();
WebSocket webSocket = client.newWebSocket(request, clientListener);
webSocket.send("hello");
webSocket.close(1000, null);
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method closeReasonTooLong.
@Test
public void closeReasonTooLong() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
WebSocket server = serverListener.assertOpen();
clientListener.assertOpen();
String reason = repeat('X', 124);
try {
webSocket.close(1000, reason);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage()).isEqualTo(("reason.size() > 123: " + reason));
}
webSocket.close(1000, null);
serverListener.assertClosing(1000, "");
server.close(1000, null);
clientListener.assertClosing(1000, "");
clientListener.assertClosed(1000, "");
serverListener.assertClosed(1000, "");
}
use of okhttp3.WebSocket in project okhttp by square.
the class OkHttpClient method newWebSocket.
/**
* Uses {@code request} to connect a new web socket.
*/
@Override
public WebSocket newWebSocket(Request request, WebSocketListener listener) {
RealWebSocket webSocket = new RealWebSocket(request, listener, new SecureRandom());
webSocket.connect(this);
return webSocket;
}
Aggregations