use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class DatabaseURIHelperTest method revsDiffUri.
@TestTemplate
public void revsDiffUri(String path) throws Exception {
URI expected = new URI(uriBase + "/test/_revs_diff");
URI actual = helper(path + "/test").revsDiffUri();
Assertions.assertEquals(expected, actual);
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class DatabaseURIHelperTest method buildDocumentUri_emptyDb.
// get a document with a db 'mounted' at /
@TestTemplate
public void buildDocumentUri_emptyDb(String path) throws Exception {
URI expected = new URI(uriBase + "/documentId");
URI actual = helper(path).documentUri("documentId");
Assertions.assertEquals(expected, actual);
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method inputStreamRetryString.
@TestTemplate
public void inputStreamRetryString() throws Exception {
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
String content = "abcde";
request.setRequestBody(content);
testInputStreamRetry(request, content.getBytes("UTF-8"));
}
use of org.junit.jupiter.api.TestTemplate 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 org.junit.jupiter.api.TestTemplate 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");
}
}
Aggregations