Search in sources :

Example 6 with TestTemplate

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);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 7 with TestTemplate

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");
    }
}
Also used : Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) TooManyRequestsException(com.cloudant.client.org.lightcouch.TooManyRequestsException) 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 8 with TestTemplate

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

Example 9 with TestTemplate

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

Example 10 with TestTemplate

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);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) 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