Java Code Examples for com.squareup.okhttp.mockwebserver.MockResponse#withWebSocketUpgrade()

The following examples show how to use com.squareup.okhttp.mockwebserver.MockResponse#withWebSocketUpgrade() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 6 votes vote down vote up
@Test(timeout = TIMEOUT)
public void shouldFireOnOpenWhenConnected() throws InterruptedException, IOException {
    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener());
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final URI uri = mockWebServer.url("/").uri();
    final Connection connection = new Connection(uri, new Consumer.Options());

    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    connection.setListener(new DefaultConnectionListener() {
        @Override
        public void onOpen() {
            events.offer("onOpen");
        }
    });
    connection.open();

    assertThat(events.take(), is("onOpen"));

    mockWebServer.shutdown();
}
 
Example 2
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void createAfterOpeningConnection() throws URISyntaxException, IOException, InterruptedException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer("onMessage:" + payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    consumer.connect();

    final Subscriptions subscriptions = consumer.getSubscriptions();
    final Subscription subscription = subscriptions.create(new Channel("CommentsChannel"));

    // Callback test
    assertThat(events.take(), is("onMessage:" + Command.subscribe(subscription.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example 3
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void createBeforeOpeningConnection() throws URISyntaxException, IOException, InterruptedException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer("onMessage:" + payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());

    final Subscriptions subscriptions = consumer.getSubscriptions();
    final Subscription subscription = subscriptions.create(new Channel("CommentsChannel"));

    consumer.connect();

    // Callback test
    assertThat(events.take(), is("onMessage:" + Command.subscribe(subscription.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example 4
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void removeWhenIdentifierIsUnique() throws IOException, InterruptedException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer("onMessage:" + payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscriptions subscriptions = consumer.getSubscriptions();

    final Subscription subscription1 = subscriptions.create(new Channel("CommentsChannel"));
    final Subscription subscription2 = subscriptions.create(new Channel("NotificationChannel"));

    consumer.connect();

    events.take(); // WebSocketListener#onMessage (subscribe)
    events.take(); // WebSocketListener#onMessage (subscribe)

    subscriptions.remove(subscription1);

    assertThat(subscriptions.contains(subscription1), is(false));
    assertThat(subscriptions.contains(subscription2), is(true));

    assertThat(events.take(), is("onMessage:" + Command.unsubscribe(subscription1.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example 5
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void removeWhenIdentifierIsNotUnique() throws IOException, InterruptedException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer("onMessage:" + payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscriptions subscriptions = consumer.getSubscriptions();

    final Subscription subscription1 = subscriptions.create(new Channel("CommentsChannel"));
    // Channel is same as subscription1
    final Subscription subscription2 = subscriptions.create(new Channel("CommentsChannel"));

    consumer.connect();

    events.take(); // WebSocketListener#onMessage (subscribe)
    events.take(); // WebSocketListener#onMessage (subscribe)

    subscriptions.remove(subscription1);

    assertThat(subscriptions.contains(subscription1), is(false));
    assertThat(subscriptions.contains(subscription2), is(true));

    assertThat(events.take(), is("onMessage:" + Command.unsubscribe(subscription1.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example 6
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void shouldFireOnCloseWhenDisconnectedByClient() throws InterruptedException, IOException {
    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener());
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final URI uri = mockWebServer.url("/").uri();
    final Connection connection = new Connection(uri, new Consumer.Options());

    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    connection.setListener(new DefaultConnectionListener() {
        @Override
        public void onOpen() {
            events.offer("onOpen");
        }

        @Override
        public void onClose() {
            events.offer("onClose");
        }
    });

    connection.open();
    assertThat(events.take(), is("onOpen"));

    connection.close();
    assertThat(events.take(), is("onClose"));

    mockWebServer.shutdown();
}
 
Example 7
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performWithDataByDefaultInterface() throws URISyntaxException, InterruptedException, IOException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"));
    consumer.connect();

    events.take(); // { command: subscribe }

    final JsonObject data = new JsonObject();
    data.addProperty("foo", "bar");
    subscription.perform("follow", data);

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", data.toString());
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example 8
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performWithDataByCustomInterface() throws URISyntaxException, InterruptedException, IOException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"), CustomSubscription.class);
    consumer.connect();

    events.take(); // { command: subscribe }

    final JsonObject data = new JsonObject();
    data.addProperty("foo", "bar");
    subscription.perform("follow", data);

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", data.toString());
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example 9
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performByDefaultInterface() throws URISyntaxException, InterruptedException, IOException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"));
    consumer.connect();

    events.take(); // { command: subscribe }

    subscription.perform("follow");

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", "{\"action\":\"follow\"}");
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example 10
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performByCustomInterface() throws URISyntaxException, InterruptedException, IOException {
    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener() {
        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            events.offer(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"), CustomSubscription.class);
    consumer.connect();

    events.take(); // { command: subscribe }

    subscription.perform("follow");

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", "{\"action\":\"follow\"}");
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example 11
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Test(timeout = TIMEOUT)
public void isOpenWhenOnOpenAndCloseByClient() throws InterruptedException, IOException {
    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.withWebSocketUpgrade(new DefaultWebSocketListener());
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final URI uri = mockWebServer.url("/").uri();
    final Connection connection = new Connection(uri, new Consumer.Options());

    final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

    connection.setListener(new DefaultConnectionListener() {
        @Override
        public void onOpen() {
            events.offer("onOpen");
        }

        @Override
        public void onClose() {
            events.offer("onClose");
        }
    });

    assertThat(connection.isOpen(), is(false));

    connection.open();

    assertThat(events.take(), is("onOpen"));

    assertThat(connection.isOpen(), is(true));

    connection.close();

    assertThat(events.take(), is("onClose"));

    assertThat(connection.isOpen(), is(false));

    mockWebServer.shutdown();
}