MockResponse response = new MockResponse() .addHeader("Content-Type", "application/json; charset=utf-8") .addHeader("Cache-Control", "no-cache") .setBody("{}"); response.throttleBody(1024, 1, TimeUnit.SECONDS);
@Test public void testSetsReadTimeout() throws Exception { MockWebServer tempWebServer = new MockWebServer(); tempWebServer.enqueue( new MockResponse().setBody("test").throttleBody(1, TIMEOUT_TIME_MS, TimeUnit.MILLISECONDS)); tempWebServer.start(); try { getFetcher().loadData(Priority.HIGH, callback); } finally { tempWebServer.shutdown(); // shutdown() called before any enqueue() blocks until it times out. mockWebServer.enqueue(new MockResponse().setResponseCode(200)); } verify(callback).onLoadFailed(isA(IOException.class)); }
@Test public void override_timeout_on_post() { underTest = HttpConnector.newBuilder().url(serverUrl).build(); // Headers are not affected by setBodyDelay, let's throttle the answer server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); expectedException.expect(IllegalStateException.class); expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); assertThat(call.content()).equals("Hello delayed"); }
@Test public void override_timeout_on_post_with_redirect() { underTest = HttpConnector.newBuilder().url(serverUrl).build(); server.enqueue(new MockResponse().setResponseCode(301).setHeader("Location:", "/redirect")); // Headers are not affected by setBodyDelay, let's throttle the answer server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); expectedException.expect(IllegalStateException.class); expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); assertThat(call.content()).equals("Hello delayed"); }
@Test public void testDemoResponseErrorSocket() { reset(mainSceneMock); MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokeDataSource); MockResponse response = new MockResponse(); response.setBody("\"message\":\"Hello\"").throttleBody(1, 2, TimeUnit.SECONDS); getErrorMockWebServer().enqueue(response); presenter.onSceneAdded(mainSceneMock, null); testScheduler.triggerActions(); verify(mainSceneMock, times(1)).showErrorDialog(anyString()); verify(mainSceneMock, times(0)).setApiText(anyString()); }
server.enqueue(new MockResponse().throttleBody(1, 1, TimeUnit.SECONDS));