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;
}
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();
}
}
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);
}
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.
}
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.
}
Aggregations