use of okhttp3.Response in project mosby by sockeqwe.
the class HomePresenterTest method loadingFirstPageAndNextPageAndFailPullToRefresh.
@Test
public void loadingFirstPageAndNextPageAndFailPullToRefresh() throws IOException {
//
// Prepare mock server to deliver mock response on incoming http request
//
List<Product> mockProductsFirstPage = Arrays.asList(new Product(1, "image", "name", "category1", "description", 21.9), new Product(2, "image", "name", "category1", "description", 21.9), new Product(3, "image", "name", "category1", "description", 21.9), new Product(4, "image", "name", "category1", "description", 21.9), new Product(5, "image", "name", "category1", "description", 21.9));
List<Product> mockProductsNextPage = Arrays.asList(new Product(6, "image", "name", "category2", "description", 21.9), new Product(7, "image", "name", "category2", "description", 21.9), new Product(8, "image", "name", "category2", "description", 21.9), new Product(9, "image", "name", "category2", "description", 21.9));
List<Product> mockProductsPullToRefresh = Arrays.asList(new Product(10, "image", "name", "category3", "description", 21.9), new Product(11, "image", "name", "category3", "description", 21.9), new Product(12, "image", "name", "category3", "description", 21.9));
mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsFirstPage)));
mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsNextPage)));
mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsPullToRefresh)));
//
// init the robot to drive to View which triggers intents on the presenter
//
HomePresenter presenter = // In a real app you could use dagger or instantiate the Presenter manually like new HomePresenter(...)
new DependencyInjection().newHomePresenter();
HomeViewRobot robot = new HomeViewRobot(presenter);
//
// We are ready, so let's start: fire intents
//
robot.fireLoadFirstPageIntent();
//
// we expect that 2 view.render() events happened with the following HomeViewState:
// 1. show loading indicator
// 2. show the items with the first page
//
List<FeedItem> expectedDataAfterFristPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null));
HomeViewState loadingFirstPage = new HomeViewState.Builder().firstPageLoading(true).build();
HomeViewState firstPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).build();
// Check if as expected
robot.assertViewStateRendered(loadingFirstPage, firstPage);
//
// Fire next page intent
//
robot.fireLoadNextPageIntent();
//
// we expect that 4 view.render() events happened with the following HomeViewState:
// 1. show loading indicator (caused by loadFirstPageIntent)
// 2. show the items with the first page (caused by loadFirstPageIntent)
// 3. show loading next page indicator
// 4. show next page content (plus original first page content)
//
List<FeedItem> expectedDataAfterNextPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null), new SectionHeader("category2"), mockProductsNextPage.get(0), mockProductsNextPage.get(1), mockProductsNextPage.get(2), new AdditionalItemsLoadable(1, "category2", false, null));
HomeViewState nextPageLoading = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageLoading(true).build();
HomeViewState nextPage = new HomeViewState.Builder().data(expectedDataAfterNextPage).build();
// Check if as expected
robot.assertViewStateRendered(loadingFirstPage, firstPage, nextPageLoading, nextPage);
//
// fire pull to refresh intent
//
// Error: no connection to server
mockWebServer.shutdown();
robot.firePullToRefreshIntent();
//
// we expect that 6 view.render() events happened with the following HomeViewState:
// 1. show loading indicator (caused by loadFirstPageIntent)
// 2. show the items with the first page (caused by loadFirstPageIntent)
// 3. show loading next page indicator
// 4. show next page content (plus original first page content)
// 5. show loading - pull to refresh indicator
// 6. show error loading pull to refresh (plus original first page + next page content)
//
HomeViewState pullToRefreshLoading = new HomeViewState.Builder().data(expectedDataAfterNextPage).pullToRefreshLoading(true).build();
HomeViewState pullToRefreshError = new HomeViewState.Builder().data(expectedDataAfterNextPage).pullToRefreshError(new ConnectException()).build();
robot.assertViewStateRendered(loadingFirstPage, firstPage, nextPageLoading, nextPage, pullToRefreshLoading, pullToRefreshError);
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method getRequest.
private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) {
if (request.getMethod().equals("OPTIONS")) {
return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE");
}
Buffer buf = new Buffer();
buf.write(body);
MockResponse response = new MockResponse().setHeader("Content-Length", body.length).setBody(buf).setResponseCode(200);
if (contentType != null) {
response = response.setHeader("Content-Type", contentType);
}
return response;
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method plainText.
@Test
public void plainText() throws Exception {
this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
Mono<String> result = this.webClient.get().uri("/greeting?name=Spring").header("X-Test-Header", "testvalue").exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("testvalue", recordedRequest.getHeader("X-Test-Header"));
Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method headers.
@Test
public void headers() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
Mono<HttpHeaders> result = this.webClient.get().uri("/greeting?name=Spring").exchange().map(response -> response.headers().asHttpHeaders());
StepVerifier.create(result).consumeNextWith(httpHeaders -> {
assertEquals(MediaType.TEXT_PLAIN, httpHeaders.getContentType());
assertEquals(13L, httpHeaders.getContentLength());
}).expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method jsonString.
@Test
public void jsonString() throws Exception {
String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}";
this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody(content));
Mono<String> result = this.webClient.get().uri("/json").accept(MediaType.APPLICATION_JSON).exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext(content).expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("/json", recordedRequest.getPath());
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
}
Aggregations