Java Code Examples for okhttp3.mockwebserver.MockResponse#setStatus()

The following examples show how to use okhttp3.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: GeoApiContextTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testRetryCanBeDisabled() throws Exception {
  // Set up 2 mock responses, an error that shouldn't be retried and a success
  MockResponse errorResponse = new MockResponse();
  errorResponse.setStatus("HTTP/1.1 500 Internal server error");
  errorResponse.setBody("Uh-oh. Server Error.");
  server.enqueue(errorResponse);

  MockResponse goodResponse = new MockResponse();
  goodResponse.setResponseCode(200);
  goodResponse.setBody("{\n   \"results\" : [],\n   \"status\" : \"ZERO_RESULTS\"\n}");
  server.enqueue(goodResponse);

  server.start();
  setMockBaseUrl();

  // This should disable the retry, ensuring that the success response is NOT returned
  builder.disableRetries();

  // We should get the error response here, not the success response.
  builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
}
 
Example 2
Source File: GeoApiContextTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryEventuallyReturnsTheRightException() throws Exception {
  MockResponse errorResponse = new MockResponse();
  errorResponse.setStatus("HTTP/1.1 500 Internal server error");
  errorResponse.setBody("Uh-oh. Server Error.");

  // Enqueue some error responses.
  for (int i = 0; i < 10; i++) {
    server.enqueue(errorResponse);
  }
  server.start();

  // Wire the mock web server to the context
  setMockBaseUrl();
  builder.retryTimeout(5, TimeUnit.SECONDS);

  try {
    builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
  } catch (IOException ioe) {
    // Ensure the message matches the status line in the mock responses.
    assertEquals("Server Error: 500 Internal server error", ioe.getMessage());
    return;
  }
  fail("Internal server error was expected but not observed.");
}
 
Example 3
Source File: GeoApiContextTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
private MockResponse createMockBadResponse() {
  MockResponse response = new MockResponse();
  response.setStatus("HTTP/1.1 500 Internal server error");
  response.setBody("Uh-oh. Server Error.");

  return response;
}
 
Example 4
Source File: GeoApiContextTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testToggleIfExceptionIsAllowedToRetry() throws Exception {
  // Enqueue some error responses, although only the first should be used because the response's
  // exception is not allowed to be retried.
  MockResponse overQueryLimitResponse = new MockResponse();
  overQueryLimitResponse.setStatus("HTTP/1.1 400 Internal server error");
  overQueryLimitResponse.setBody(TestUtils.retrieveBody("OverQueryLimitResponse.json"));
  server.enqueue(overQueryLimitResponse);
  server.enqueue(overQueryLimitResponse);
  server.enqueue(overQueryLimitResponse);
  server.start();

  builder.retryTimeout(1, TimeUnit.MILLISECONDS);
  builder.maxRetries(10);
  builder.setIfExceptionIsAllowedToRetry(OverQueryLimitException.class, false);

  setMockBaseUrl();

  try {
    builder
        .build()
        .get(new ApiConfig("/"), GeocodingApi.Response.class, "any-key", "any-value")
        .await();
  } catch (OverQueryLimitException e) {
    assertEquals(1, server.getRequestCount());
    return;
  }

  fail("OverQueryLimitException was expected but not observed.");
}