Search in sources :

Example 11 with CloudantClient

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

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

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class URIBaseTest method buildAccountUri_noTrailingPathSeparator.

@Test
public void buildAccountUri_noTrailingPathSeparator() throws Exception {
    CloudantClient client = ClientBuilder.url(clientResource.get().getBaseUri().toURL()).build();
    Assertions.assertFalse(client.getBaseUri().toString().endsWith("/"));
    URI clientUri = new URIBase(client.getBaseUri()).build();
    Assertions.assertFalse(clientUri.toString().endsWith("/"));
    // Check that path is not missing / separators
    clientUri = new URIBase(client.getBaseUri()).path("").path("api").path("couch").build();
    URI expectedAccountUri = new URI(clientResource.get().getBaseUri().toString() + "/api/couch");
    Assertions.assertEquals(expectedAccountUri, clientUri);
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) URI(java.net.URI) URIBase(com.cloudant.client.internal.URIBase) Test(org.junit.jupiter.api.Test)

Example 14 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class ChangeNotificationsTest method changesCustomParameter.

/**
 * Test that a custom parameter can be added to a changes request.
 */
@Test
public void changesCustomParameter() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database db = client.database("notreal", false);
    // Mock up an empty set of changes
    mockWebServer.enqueue(new MockResponse().setBody("{\"results\": []}"));
    db.changes().filter("myFilter").parameter("myParam", "paramValue").getChanges();
    RecordedRequest request = mockWebServer.takeRequest(1, TimeUnit.SECONDS);
    assertNotNull(request, "There should be a changes request");
    assertTrue(request.getPath().contains("filter=myFilter"), "There should be a filter parameter on the request");
    assertTrue(request.getPath().contains("myParam=paramValue"), "There should be a custom parameter on the " + "request");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) Test(org.junit.jupiter.api.Test)

Example 15 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class CloudantClientTests method testBasicAuth.

/**
 * Test that adding the Basic Authentication interceptor to CloudantClient works.
 */
@Test
@RequiresCloudant
public void testBasicAuth() throws IOException {
    BasicAuthInterceptor interceptor = new BasicAuthInterceptor(CloudantClientHelper.COUCH_USERNAME + ":" + CloudantClientHelper.COUCH_PASSWORD);
    CloudantClient client = ClientBuilder.account(CloudantClientHelper.COUCH_USERNAME).interceptors(interceptor).build();
    // Test passes if there are no exceptions
    client.getAllDbs();
}
Also used : BasicAuthInterceptor(com.cloudant.http.interceptors.BasicAuthInterceptor) CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

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