Search in sources :

Example 6 with RequiresCloudant

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();
    }
}
Also used : BasicAuthInterceptor(com.cloudant.http.interceptors.BasicAuthInterceptor) InputStreamReader(java.io.InputStreamReader) HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) TestTemplate(org.junit.jupiter.api.TestTemplate) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

Example 7 with RequiresCloudant

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();
    }
}
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 8 with RequiresCloudant

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());
}
Also used : Membership(com.cloudant.client.api.model.Membership) Test(org.junit.jupiter.api.Test) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

Aggregations

RequiresCloudant (com.cloudant.test.main.RequiresCloudant)8 Test (org.junit.jupiter.api.Test)6 HttpConnection (com.cloudant.http.HttpConnection)3 CloudantClient (com.cloudant.client.api.CloudantClient)2 Membership (com.cloudant.client.api.model.Membership)2 Shard (com.cloudant.client.api.model.Shard)2 BasicAuthInterceptor (com.cloudant.http.interceptors.BasicAuthInterceptor)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 TestTemplate (org.junit.jupiter.api.TestTemplate)2 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)1 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)1 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)1 CookieInterceptor (com.cloudant.http.internal.interceptors.CookieInterceptor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1