use of com.cloudant.test.main.RequiresCloudant in project java-cloudant by cloudant.
the class HttpTest method testBasicAuth.
/**
* Test that adding the Basic Authentication interceptor to HttpConnection
* will complete with a response code of 200. The response input stream
* is expected to hold the newly created document's id and rev.
*/
@TestTemplate
@RequiresCloudant
public void testBasicAuth() throws IOException {
BasicAuthInterceptor interceptor = new BasicAuthInterceptor(CloudantClientHelper.COUCH_USERNAME + ":" + CloudantClientHelper.COUCH_PASSWORD);
HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json");
conn.requestInterceptors.add(interceptor);
ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes());
// nothing read from stream
assertEquals(data.getBytes().length, bis.available());
conn.setRequestBody(bis);
HttpConnection responseConn = conn.execute();
// stream was read to end
assertEquals(0, bis.available());
assertEquals(2, responseConn.getConnection().getResponseCode() / 100);
// check the json
Gson gson = new Gson();
InputStream is = responseConn.responseAsInputStream();
try {
JsonObject response = gson.fromJson(new InputStreamReader(is), JsonObject.class);
assertTrue(response.has("ok"));
assertTrue(response.get("ok").getAsBoolean());
assertTrue(response.has("id"));
assertTrue(response.has("rev"));
} finally {
is.close();
}
}
use of com.cloudant.test.main.RequiresCloudant 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.test.main.RequiresCloudant in project java-cloudant by cloudant.
the class CloudantClientTests method membership.
@Test
@RequiresCloudant
public void membership() {
Membership mship = account.getMembership();
assertNotNull(mship);
assertNotNull(mship.getClusterNodes());
assertNotNull(mship.getClusterNodes().hasNext());
assertNotNull(mship.getAllNodes());
assertNotNull(mship.getAllNodes().hasNext());
}
Aggregations