Search in sources :

Example 26 with TestTemplate

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");
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URL(java.net.URL) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 27 with TestTemplate

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 28 with TestTemplate

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) TestTimer(com.cloudant.tests.util.TestTimer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 29 with TestTemplate

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) CloudantClient(com.cloudant.client.api.CloudantClient) TestTimer(com.cloudant.tests.util.TestTimer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 30 with TestTemplate

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");
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

TestTemplate (org.junit.jupiter.api.TestTemplate)51 CloudantClient (com.cloudant.client.api.CloudantClient)23 URI (java.net.URI)18 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)18 MockResponse (okhttp3.mockwebserver.MockResponse)16 HttpConnection (com.cloudant.http.HttpConnection)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 TreeMap (java.util.TreeMap)5 TestTimer (com.cloudant.tests.util.TestTimer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)2 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2