Search in sources :

Example 21 with TestTemplate

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

Example 22 with TestTemplate

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

Example 23 with TestTemplate

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

Example 24 with TestTemplate

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");
}
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 25 with TestTemplate

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");
    }
}
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)

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