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

The following examples show how to use com.squareup.okhttp.mockwebserver.MockResponse#setStatus() . 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 shouldFireOnFailureWhenInternalServerErrorReceived() throws InterruptedException, IOException {
    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.setResponseCode(500);
    response.setStatus("HTTP/1.1 500 Internal Server Error");
    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 onFailure(Exception e) {
            events.offer("onFailed");
        }
    });
    connection.open();

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

    mockWebServer.shutdown();
}
 
Example 2
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void isOpenWhenOnOpenAndFailure() throws InterruptedException, IOException {
    final MockWebServer mockWebServer = new MockWebServer();
    final MockResponse response = new MockResponse();
    response.setResponseCode(500);
    response.setStatus("HTTP/1.1 500 Internal Server Error");
    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 onFailure(Exception e) {
            events.offer("onFailed");
        }
    });

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

    connection.open();

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

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

    mockWebServer.shutdown();
}