Search in sources :

Example 11 with TestTemplate

use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.

the class HttpTest method testHttpConnectionRetries.

/**
 * Test the global number of retries
 *
 * @throws Exception
 */
@TestTemplate
public void testHttpConnectionRetries() throws Exception {
    // Just return 200 OK
    mockWebServer.setDispatcher(new MockWebServerResources.ConstantResponseDispatcher(200));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new HttpConnectionResponseInterceptor() {

        @Override
        public HttpConnectionInterceptorContext interceptResponse(HttpConnectionInterceptorContext context) {
            // At least do something with the connection, otherwise we risk breaking it
            try {
                context.connection.getConnection().getResponseCode();
            } catch (IOException e) {
                fail("IOException getting response code");
            }
            // Set to always replay
            context.replayRequest = true;
            return context;
        }
    }).build();
    String response = c.executeRequest(Http.GET(c.getBaseUri()).setNumberOfRetries(5)).responseAsString();
    assertTrue(response.isEmpty(), "There should be no response body on the mock response");
    assertEquals(5, mockWebServer.getRequestCount(), "There should be 5 request attempts");
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) HttpConnectionResponseInterceptor(com.cloudant.http.HttpConnectionResponseInterceptor) IOException(java.io.IOException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockWebServerResources(com.cloudant.tests.util.MockWebServerResources) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 12 with TestTemplate

use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.

the class SslAuthenticationTest method localSSLAuthenticationDisabledWithCookieAuth.

/**
 * Repeat the localSSLAuthenticationDisabled, but with the cookie auth enabled.
 * This test validates that the SSL settings also get applied to the cookie interceptor.
 */
@TestTemplate
public void localSSLAuthenticationDisabledWithCookieAuth() throws Exception {
    // Mock up an OK cookie response then an OK response for the getAllDbs()
    server.enqueue(MockWebServerResources.OK_COOKIE);
    // OK 200
    server.enqueue(new MockResponse());
    // Use a username and password to enable the cookie auth interceptor
    CloudantClient dbClient = CloudantClientHelper.newMockWebServerClientBuilder(server).username("user").password("password").disableSSLAuthentication().build();
    dbClient.getAllDbs();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 13 with TestTemplate

use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.

the class SslAuthenticationTest method localSSLAuthenticationEnabledWithCookieAuth.

/**
 * Repeat the localSSLAuthenticationEnabled, but with the cookie auth enabled.
 * This test validates that the SSL settings also get applied to the cookie interceptor.
 */
@TestTemplate
public void localSSLAuthenticationEnabledWithCookieAuth() throws Exception {
    // Mock up an OK cookie response then an OK response for the getAllDbs()
    server.enqueue(MockWebServerResources.OK_COOKIE);
    // OK 200
    server.enqueue(new MockResponse());
    // Use a username and password to enable the cookie auth interceptor
    CloudantClient dbClient = CloudantClientHelper.newMockWebServerClientBuilder(server).username("user").password("password").build();
    try {
        dbClient.getAllDbs();
        fail("The SSL authentication failure should result in a CouchDbException");
    } catch (CouchDbException e) {
        validateClientAuthenticationException(e);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 14 with TestTemplate

use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.

the class DatabaseURIHelperTest method buildDocumentUri_slashInDocumentId.

@TestTemplate
public void buildDocumentUri_slashInDocumentId(String path) throws Exception {
    URI expected = new URI(uriBase + "/test/path1%2Fpath2");
    URI actual = helper(path + "/test").documentUri("path1/path2");
    Assertions.assertEquals(expected, actual);
}
Also used : URI(java.net.URI) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 15 with TestTemplate

use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.

the class DatabaseURIHelperTest method buildVeryEscapedUri.

// this test shows that non-ascii characters will be represented correctly
// in the url but that we don't escape characters like / in the root url, but that they are
// correctly escaped in the document part of the url
@TestTemplate
public void buildVeryEscapedUri(String path) throws Exception {
    URI expected = new URI(uriBase + "/SDF@%23%25$%23)KLDfdffdg%C3%A9/%2FSF@%23%25$%23)" + "DFGKLDfdffdg%C3%A9%2Fpath2?detail=/SDF@%23%25$%23)%C3%A9&revs=%5B1-2%5D");
    Map<String, Object> options = new TreeMap<String, Object>();
    options.put("revs", "[1-2]");
    options.put("detail", "/SDF@#%$#)\u00E9");
    URI actual = helper(path + "/SDF@#%$#)KLDfdffdg\u00E9").documentId("/SF@#%$#)" + "DFGKLDfdffdg\u00E9/path2").query(options).build();
    Assertions.assertEquals(expected.toASCIIString(), actual.toASCIIString());
}
Also used : TreeMap(java.util.TreeMap) URI(java.net.URI) 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