use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class CloudantClientTests method gatewayStyleURL.
@Test
public void gatewayStyleURL() throws Exception {
final String gatewayPath = "/gateway";
// Set a dispatcher that returns 200 if the requests have the correct path /gateway/_all_dbs
// Otherwise return 400.
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals(gatewayPath + "/_all_dbs")) {
return new MockResponse();
} else {
return new MockResponse().setResponseCode(400);
}
}
});
// Build a client with a URL that includes a path
CloudantClient c = ClientBuilder.url(new URL(server.url(gatewayPath).toString())).build();
// If the request path is wrong this call will return 400 and throw an exception failing the
// test.
c.getAllDbs();
// Build a client with a URL that includes a path with a trailing /
c = ClientBuilder.url(new URL(server.url(gatewayPath + "/").toString())).build();
// If the request path is wrong this call will return 400 and throw an exception failing the
// test.
c.getAllDbs();
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class CloudantClientTests method testBasicAuthFromCredentials.
/**
* Test that configuring the Basic Authentication interceptor from credentials and adding to
* the CloudantClient works.
*/
@Test
public void testBasicAuthFromCredentials() throws Exception {
BasicAuthInterceptor interceptor = BasicAuthInterceptor.createFromCredentials("username", "password");
// send back a mock OK 200
server.enqueue(new MockResponse());
CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(server).interceptors(interceptor).build();
client.getAllDbs();
// expected 'username:password'
assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", server.takeRequest().getHeader("Authorization"));
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class CloudantClientTests method testDefaultPorts.
@Test
public void testDefaultPorts() throws Exception {
CloudantClient c = null;
c = CloudantClientHelper.newTestAddressClient().build();
assertEquals(80, c.getBaseUri().getPort(), "The http port should be 80");
c = CloudantClientHelper.newHttpsTestAddressClient().build();
assertEquals(443, c.getBaseUri().getPort(), "The http port should be 443");
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class DatabaseTest method permissionsParsing.
@Test
public void permissionsParsing() throws Exception {
CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
Database db = client.database("notarealdb", false);
// Mock up a request of all permissions
// for GET _security
mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
// for PUT _security
mockWebServer.enqueue(MockWebServerResources.JSON_OK);
db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
// Mock up a failing request
String testError = "test error";
String testReason = "test reason";
// for GET _security
mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("{\"reason\":\"" + testReason + "\", \"error\":\"" + testError + "\"}"));
try {
db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
} catch (CouchDbException e) {
assertEquals(testError, e.getError());
assertEquals(testReason, e.getReason());
}
}
use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.
the class DatabaseTest method customGsonDeserializerTest.
// Test case for issue #31
@Test
public void customGsonDeserializerTest() throws MalformedURLException {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
CloudantClient account = CloudantClientHelper.getClientBuilder().gsonBuilder(builder).build();
Database db = account.database(dbResource.getDatabaseName(), false);
Map<String, Object> h = new HashMap<String, Object>();
h.put("_id", "serializertest");
h.put("date", "2015-01-23T18:25:43.511Z");
db.save(h);
// should not throw a JsonSyntaxException
db.find(Foo.class, "serializertest");
}
Aggregations