Search in sources :

Example 36 with CloudantClient

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

the class IndexTests method getUseIndexFromRequest.

/**
 * Uses a mock web server to record a _find request using the specified options
 *
 * @param builder query to make
 * @return the JsonElement from the use_index property of the JsonObject POSTed with the request
 * @throws Exception
 */
private JsonElement getUseIndexFromRequest(QueryBuilder builder) throws Exception {
    JsonElement useIndexRequestProperty = null;
    MockWebServer mockWebServer = new MockWebServer();
    // Return 200 OK with empty array of docs (once for each request)
    mockWebServer.enqueue(new MockResponse().setBody("{ \"docs\" : []}"));
    mockWebServer.start();
    try {
        CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
        Database db = client.database("mock", false);
        db.query(builder.build(), Movie.class);
        RecordedRequest request = mockWebServer.takeRequest(1, TimeUnit.SECONDS);
        JsonObject body = new Gson().fromJson(request.getBody().readUtf8(), JsonObject.class);
        useIndexRequestProperty = body.get("use_index");
    } finally {
        mockWebServer.shutdown();
    }
    return useIndexRequestProperty;
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) JsonElement(com.google.gson.JsonElement) MockWebServer(okhttp3.mockwebserver.MockWebServer) Database(com.cloudant.client.api.Database) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 37 with CloudantClient

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

the class ResponseTest method testJsonErrorStreamFromLB.

/**
 * Test that an error stream is correctly assigned to a CouchDbException error field if it comes
 * directly from the lb and not the service.
 * <P>
 * This is a Cloudant service test, where a HTTP proxy may send an error.
 * We can provoke it into doing so by sending an invalid HTTP request - in this case an
 * invalid header field.
 * Originally these errors were wrapped in HTML and so exercised a different path - they are now
 * JSON body responses like most other Cloudant requests so should be on the normal exception
 * handling path, but it is worth checking that we do work with these error types.
 * </P>
 */
@RequiresCloudant
@Test
public void testJsonErrorStreamFromLB() throws Exception {
    final AtomicBoolean badHeaderEnabled = new AtomicBoolean(false);
    CloudantClient c = CloudantClientHelper.getClientBuilder().interceptors(new HttpConnectionRequestInterceptor() {

        @Override
        public HttpConnectionInterceptorContext interceptRequest(HttpConnectionInterceptorContext context) {
            if (badHeaderEnabled.get()) {
                // Note space is also a bad char, but OkHttp prohibits them
                String badHeaderCharacters = "()<>@,;\\/[]?=";
                // set a header using characters that are not permitted
                context.connection.requestProperties.put(badHeaderCharacters, badHeaderCharacters);
            }
            return context;
        }
    }).build();
    try {
        // Make a good request, which will set up the session etc
        HttpConnection d = c.executeRequest(Http.GET(c.getBaseUri()));
        d.responseAsString();
        assertTrue(d.getConnection().getResponseCode() / 100 == 2, "The first request should " + "succeed");
        // Enable the bad headers and expect the exception on the next request
        badHeaderEnabled.set(true);
        String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
        fail("A CouchDbException should be thrown, but had response " + response);
    } catch (CouchDbException e) {
        // we expect a CouchDbException
        assertEquals(400, e.getStatusCode(), "The exception should be for a bad request");
        assertNotNull(e.getError(), "The exception should have an error set");
        assertEquals("bad_request", e.getError(), "The exception error should be bad request");
    } finally {
        // Disable the bad header to allow a clean shutdown
        badHeaderEnabled.set(false);
        c.shutdown();
    }
}
Also used : HttpConnectionRequestInterceptor(com.cloudant.http.HttpConnectionRequestInterceptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) HttpConnection(com.cloudant.http.HttpConnection) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) Test(org.junit.jupiter.api.Test) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

Example 38 with CloudantClient

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

the class SslAuthenticationTest method localSslAuthenticationEnabled.

/**
 * Connect to the local simple https server with SSL authentication enabled explicitly.
 * This should throw an exception because the SSL authentication fails.
 */
@TestTemplate
public void localSslAuthenticationEnabled() throws Exception {
    CouchDbException thrownException = null;
    try {
        CloudantClient dbClient = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
        // Queue a 200 OK response
        server.enqueue(new MockResponse());
        // Make an arbitrary connection to the DB.
        dbClient.getAllDbs();
    } catch (CouchDbException e) {
        thrownException = e;
    }
    validateClientAuthenticationException(thrownException);
}
Also used : CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 39 with CloudantClient

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

the class SslAuthenticationTest method localSslAuthenticationDisabled.

/**
 * Connect to the local simple https server with SSL authentication disabled.
 */
@TestTemplate
public void localSslAuthenticationDisabled() throws Exception {
    // Build a client that connects to the mock server with SSL authentication disabled
    CloudantClient dbClient = CloudantClientHelper.newMockWebServerClientBuilder(server).disableSSLAuthentication().build();
    // Queue a 200 OK response
    server.enqueue(new MockResponse());
    // Make an arbitrary connection to the DB.
    dbClient.getAllDbs();
// Test is successful if no exception is thrown, so no explicit check is needed.
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 40 with CloudantClient

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

the class SslAuthenticationTest method remoteSslAuthenticationEnabledTest.

/**
 * Connect to the remote Cloudant server with SSL Authentication enabled.
 * This shouldn't throw an exception as the Cloudant server has a valid
 * SSL certificate, so should be authenticated.
 */
@TestTemplate
@RequiresCloudantService
public void remoteSslAuthenticationEnabledTest() {
    CloudantClient dbClient = CloudantClientHelper.getClientBuilder().build();
    // Make an arbitrary connection to the DB.
    dbClient.getAllDbs();
// Test is successful if no exception is thrown, so no explicit check is needed.
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate) RequiresCloudantService(com.cloudant.test.main.RequiresCloudantService)

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