use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class HttpTest method cookieInterceptorURLEncoding.
/**
* This test mocks up a server to receive the _session request and asserts that the request
* body is correctly encoded (per application/x-www-form-urlencoded). Because it requires a
* body this test also relies on Expect: 100-continue working in the client as that is enabled
* by default.
*
* @throws Exception
*/
@TestTemplate
public void cookieInterceptorURLEncoding() throws Exception {
final String mockUser = "myStrangeUsername=&?";
String mockPass = "?&=NotAsStrangeInAPassword";
// expect a cookie request then a GET
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
mockWebServer.enqueue(new MockResponse());
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username(mockUser).password(mockPass).build();
// the GET request will try to get a session, then perform the GET
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertTrue(response.isEmpty(), "There should be no response body on the mock response");
RecordedRequest r = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
String sessionRequestContent = r.getBody().readString(Charset.forName("UTF-8"));
assertNotNull(sessionRequestContent, "The _session request should have non-null content");
// expecting name=...&password=...
String[] parts = Utils.splitAndAssert(sessionRequestContent, "&", 1);
String username = URLDecoder.decode(Utils.splitAndAssert(parts[0], "=", 1)[1], "UTF-8");
assertEquals(mockUser, username, "The username URL decoded username should match");
String password = URLDecoder.decode(Utils.splitAndAssert(parts[1], "=", 1)[1], "UTF-8");
assertEquals(mockPass, password, "The username URL decoded password should match");
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class HttpTest method test429BackoffMaxDefault.
/**
* Test that the default maximum number of retries is reached and the backoff is of at least the
* expected duration.
*
* @throws Exception
*/
@TestTemplate
public void test429BackoffMaxDefault() throws Exception {
// Always respond 429 for this test
mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
TestTimer t = TestTimer.startTimer();
try {
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(Replay429Interceptor.WITH_DEFAULTS).build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
fail("There should be a TooManyRequestsException instead had response " + response);
} catch (TooManyRequestsException e) {
long duration = t.stopTimer(TimeUnit.MILLISECONDS);
// 3 backoff periods for 4 attempts: 250 + 500 + 1000 = 1750 ms
assertTrue(duration >= 1750, "The duration should be at least 1750 ms, but was " + duration);
assertEquals(4, mockWebServer.getRequestCount(), "There should be 4 request attempts");
}
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class HttpTest method test429Backoff.
/**
* Test that a request is replayed in response to a 429.
*
* @throws Exception
*/
@TestTemplate
public void test429Backoff() throws Exception {
mockWebServer.enqueue(MockWebServerResources.get429());
mockWebServer.enqueue(new MockResponse());
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(Replay429Interceptor.WITH_DEFAULTS).build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertTrue(response.isEmpty(), "There should be no response body on the mock response");
assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 requests");
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class HttpTest method test429BackoffRetryAfter.
/**
* Test that an integer number of seconds delay specified by a Retry-After header is honoured.
*
* @throws Exception
*/
@TestTemplate
public void test429BackoffRetryAfter() throws Exception {
mockWebServer.enqueue(MockWebServerResources.get429().addHeader("Retry-After", "1"));
mockWebServer.enqueue(new MockResponse());
TestTimer t = TestTimer.startTimer();
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(Replay429Interceptor.WITH_DEFAULTS).build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertTrue(response.isEmpty(), "There should be no response body on the mock response");
long duration = t.stopTimer(TimeUnit.MILLISECONDS);
assertTrue(duration >= 1000, "The duration should be at least 1000 ms, but was " + duration);
assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 request attempts");
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class HttpTest method test429IgnoreRetryAfter.
@TestTemplate
public void test429IgnoreRetryAfter() throws Exception {
mockWebServer.enqueue(MockWebServerResources.get429().addHeader("Retry-After", "1"));
mockWebServer.enqueue(new MockResponse());
TestTimer t = TestTimer.startTimer();
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(1, 1, false)).build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertTrue(response.isEmpty(), "There should be no response body on the mock response");
long duration = t.stopTimer(TimeUnit.MILLISECONDS);
assertTrue(duration < 1000, "The duration should be less than 1000 ms, but was " + duration);
assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 request attempts");
}
Aggregations