use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method inputStreamRetryWithLength.
@TestTemplate
public void inputStreamRetryWithLength() throws Exception {
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
final byte[] content = "abcde".getBytes("UTF-8");
// Mock up an input stream that doesn't support marking
request.setRequestBody(new UnmarkableInputStream(content), content.length);
testInputStreamRetry(request, content);
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method test429BackoffMaxConfigured.
/**
* Test that the configured maximum number of retries is reached and the backoff is of at least
* the expected duration.
*
* @throws Exception
*/
@TestTemplate
public void test429BackoffMaxConfigured() throws Exception {
// Always respond 429 for this test
mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
TestTimer t = TestTimer.startTimer();
try {
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(10, 1, true)).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);
// 9 backoff periods for 10 attempts: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511 ms
assertTrue(duration >= 511, "The duration should be at least 511 ms, but was " + duration);
assertEquals(10, mockWebServer.getRequestCount(), "There should be 10 request attempts");
}
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method cookieRenewal.
/**
* This test check that the cookie is renewed if the server presents a Set-Cookie header
* after the cookie authentication.
*
* @throws Exception
*/
@TestTemplate
public void cookieRenewal() throws Exception {
final String hello = "{\"hello\":\"world\"}\r\n";
final String renewalCookieToken = "RenewCookie_a2ltc3RlYmVsOjUxMzRBQTUzOtiY2_IDUIdsTJEVNEjObAbyhrgz";
// Request sequence
// _session request to get Cookie
// GET request -> 200 with a Set-Cookie
// GET replay -> 200
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
mockWebServer.enqueue(new MockResponse().setResponseCode(200).addHeader("Set-Cookie", MockWebServerResources.authSessionCookie(renewalCookieToken, null)).setBody(hello));
mockWebServer.enqueue(new MockResponse());
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertEquals(hello, response, "The expected response should be received");
// assert that there were 2 calls
assertEquals(2, mockWebServer.getRequestCount(), "The server should have received 2 requests");
assertEquals("/_session", MockWebServerResources.takeRequestWithTimeout(mockWebServer).getPath(), "The request should have been for /_session");
assertEquals("/", MockWebServerResources.takeRequestWithTimeout(mockWebServer).getPath(), "The request should have been for /");
String secondResponse = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertTrue(secondResponse.isEmpty(), "There should be no response body on the mock response" + secondResponse);
// also assert that there were 3 calls
assertEquals(3, mockWebServer.getRequestCount(), "The server should have received 3 requests");
// this is the request that should have the new cookie.
RecordedRequest request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
assertEquals("/", request.getPath(), "The request should have been for path /");
String headerValue = request.getHeader("Cookie");
// The cookie may or may not have the session id quoted, so check both
assertThat("The Cookie header should contain the expected session value", headerValue, anyOf(containsString(MockWebServerResources.authSession(renewalCookieToken)), containsString(MockWebServerResources.authSessionUnquoted(renewalCookieToken))));
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testCookieRenewOnPost.
@TestTemplate
public void testCookieRenewOnPost() throws Exception {
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
mockWebServer.enqueue(new MockResponse().setResponseCode(403).setBody("{\"error\":\"credentials_expired\", \"reason\":\"Session expired\"}\r\n"));
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
mockWebServer.enqueue(new MockResponse());
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
request.setRequestBody("{\"some\": \"json\"}");
HttpConnection response = c.executeRequest(request);
String responseStr = response.responseAsString();
assertTrue(responseStr.isEmpty(), "There should be no response body on the mock response");
response.getConnection().getResponseCode();
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method inputStreamRetry.
@TestTemplate
public void inputStreamRetry() throws Exception {
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
final byte[] content = "abcde".getBytes("UTF-8");
// Mock up an input stream that doesn't support marking
request.setRequestBody(new UnmarkableInputStream(content));
testInputStreamRetry(request, content);
}
Aggregations