use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testWriteToServerOk.
/*
* Basic test that we can write a document body by POSTing to a known database
*/
@TestTemplate
public void testWriteToServerOk() throws Exception {
HttpConnection conn = new HttpConnection("POST", new URL(dbResource.getDbURIWithUserInfo()), "application/json");
ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes());
// nothing read from stream
assertEquals(data.getBytes().length, bis.available());
conn.setRequestBody(bis);
HttpConnection response = conn.execute();
// Consume response stream
String responseStr = response.responseAsString();
String okPattern = ".*\"ok\"\\s*:\\s*true.*";
assertTrue(Pattern.compile(okPattern, Pattern.DOTALL).matcher(responseStr).matches(), "There should be an ok response: " + "" + responseStr);
// stream was read to end
assertEquals(0, bis.available());
assertEquals(2, response.getConnection().getResponseCode() / 100, "Should be a 2XX response code");
}
use of org.junit.jupiter.api.TestTemplate 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 org.junit.jupiter.api.TestTemplate 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 org.junit.jupiter.api.TestTemplate 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");
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method okUsableClassLoad.
/**
* Tests that loading the OkHelper will not try to use any classes from outside the
* cloudant-http built classes. Specifically it won't cause any okhttp classes to try to be
* loaded outside of the controlled check for the presence of okhttp.
*
* @throws ClassNotFoundException if the load tries to use any classes it shouldn't
* @throws Exception if there is another issue in the test
*/
@TestTemplate
public void okUsableClassLoad() throws ClassNotFoundException, Exception {
// Point to the built classes, it's a bit awkward but we need to load the class cleanly
File f = new File("../cloudant-http/build/classes/java/main/");
ClassLoader loader = new CloudantHttpIsolationClassLoader(new URL[] { f.toURI().toURL() });
Class<?> okHelperClass = Class.forName("com.cloudant.http.internal.ok.OkHelper", true, loader);
assertEquals(isOkUsable, okHelperClass.getMethod("isOkUsable").invoke(null), "The isOkUsable value should be correct");
}
Aggregations