use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method wrongMagicHeader.
@Test
public void wrongMagicHeader() throws IOException {
webServer.enqueue(new MockResponse().setResponseCode(101).setHeader("Connection", "Upgrade").setHeader("Upgrade", "websocket").setHeader("Sec-WebSocket-Accept", "magic"));
newWebSocket();
clientListener.assertFailure(101, null, ProtocolException.class, "Expected 'Sec-WebSocket-Accept' header value 'ujmZX4KXZqjwy6vi1aQFH5p4Ygk=' but was 'magic'");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method throwingOnOpenFailsImmediately.
@Test
public void throwingOnOpenFailsImmediately() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
final RuntimeException e = new RuntimeException();
clientListener.setNextEventDelegate(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
throw e;
}
});
newWebSocket();
serverListener.assertOpen();
serverListener.assertExhausted();
clientListener.assertFailure(e);
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method webSocketAndApplicationInterceptors.
@Test
public void webSocketAndApplicationInterceptors() throws IOException {
final AtomicInteger interceptedCount = new AtomicInteger();
client = client.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
assertNull(chain.request().body());
Response response = chain.proceed(chain.request());
assertEquals("Upgrade", response.header("Connection"));
assertTrue("", response.body().source().exhausted());
interceptedCount.incrementAndGet();
return response;
}
}).build();
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
assertEquals(1, interceptedCount.get());
webSocket.close(1000, null);
WebSocket server = serverListener.assertOpen();
server.close(1000, null);
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method nullStringThrows.
@Test
public void nullStringThrows() throws IOException {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
try {
webSocket.send((String) null);
fail();
} catch (NullPointerException e) {
assertEquals("text == null", e.getMessage());
}
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method throwingOnClosingClosesImmediatelyAndFails.
@Test
public void throwingOnClosingClosesImmediatelyAndFails() throws IOException {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
final RuntimeException e = new RuntimeException();
clientListener.setNextEventDelegate(new WebSocketListener() {
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
throw e;
}
});
server.close(1000, "bye");
clientListener.assertFailure(e);
serverListener.assertExhausted();
}
Aggregations