Search in sources :

Example 26 with CloudantClient

use of com.cloudant.client.api.CloudantClient 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");
}
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 27 with CloudantClient

use of com.cloudant.client.api.CloudantClient 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");
    }
}
Also used : 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 28 with CloudantClient

use of com.cloudant.client.api.CloudantClient 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 29 with CloudantClient

use of com.cloudant.client.api.CloudantClient 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 30 with CloudantClient

use of com.cloudant.client.api.CloudantClient 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)

Aggregations

CloudantClient (com.cloudant.client.api.CloudantClient)47 MockResponse (okhttp3.mockwebserver.MockResponse)32 TestTemplate (org.junit.jupiter.api.TestTemplate)23 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)21 Test (org.junit.jupiter.api.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 Database (com.cloudant.client.api.Database)8 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)7 TestTimer (com.cloudant.tests.util.TestTimer)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 HttpConnection (com.cloudant.http.HttpConnection)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 Dispatcher (okhttp3.mockwebserver.Dispatcher)3 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)2 BasicAuthInterceptor (com.cloudant.http.interceptors.BasicAuthInterceptor)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)2