use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method serverMessage.
@Test
public void serverMessage() throws IOException {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
server.send("Hello, WebSockets!");
clientListener.assertTextMessage("Hello, WebSockets!");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method wrongUpgradeHeader.
@Test
public void wrongUpgradeHeader() throws IOException {
webServer.enqueue(new MockResponse().setResponseCode(101).setHeader("Connection", "Upgrade").setHeader("Upgrade", "Pepsi").setHeader("Sec-WebSocket-Accept", "ujmZX4KXZqjwy6vi1aQFH5p4Ygk="));
newWebSocket();
clientListener.assertFailure(101, null, ProtocolException.class, "Expected 'Upgrade' header value 'websocket' but was 'Pepsi'");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method readTimeoutDoesNotApplyAcrossFrames.
@Test
public void readTimeoutDoesNotApplyAcrossFrames() throws Exception {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
// Sleep longer than the HTTP client's read timeout.
Thread.sleep(client.readTimeoutMillis() + 500);
server.send("abc");
clientListener.assertTextMessage("abc");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method overflowOutgoingQueue.
@Test
public void overflowOutgoingQueue() throws IOException {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
// Send messages until the client's outgoing buffer overflows!
ByteString message = ByteString.of(new byte[1024 * 1024]);
int messageCount = 0;
while (true) {
boolean success = webSocket.send(message);
if (!success)
break;
messageCount++;
long queueSize = webSocket.queueSize();
assertTrue(queueSize >= 0 && queueSize <= messageCount * message.size());
// Expect to fail before enqueueing 32 MiB.
assertTrue(messageCount < 32);
}
// Confirm all sent messages were received, followed by a client-initiated close.
WebSocket server = serverListener.assertOpen();
for (int i = 0; i < messageCount; i++) {
serverListener.assertBinaryMessage(message);
}
serverListener.assertClosing(1001, "");
// When the server acknowledges the close the connection shuts down gracefully.
server.close(1000, null);
clientListener.assertClosing(1000, "");
clientListener.assertClosed(1000, "");
serverListener.assertClosed(1001, "");
}
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() throws IOException {
webServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
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");
assertFalse(webSocket.close(1000, null));
}
Aggregations