Search in sources :

Example 1 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method testCookieAuthWithoutRetry.

// NOTE: This test doesn't work with specified couch servers,
// the URL will always include the creds specified for the test
// 
// A couchdb server needs to be set and running with the correct
// security settings, the database *must* not be public, it *must*
// be named cookie_test
// 
@TestTemplate
@RequiresCloudant
public void testCookieAuthWithoutRetry() throws IOException {
    CookieInterceptor interceptor = new CookieInterceptor(CloudantClientHelper.COUCH_USERNAME, CloudantClientHelper.COUCH_PASSWORD, clientResource.get().getBaseUri().toString());
    HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json");
    conn.responseInterceptors.add(interceptor);
    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 : InputStreamReader(java.io.InputStreamReader) HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) CookieInterceptor(com.cloudant.http.internal.interceptors.CookieInterceptor) 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 2 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method inputStreamRetryBytes.

@TestTemplate
public void inputStreamRetryBytes() throws Exception {
    HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
    byte[] content = "abcde".getBytes("UTF-8");
    request.setRequestBody(content);
    testInputStreamRetry(request, content);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 3 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method inputStreamRetryWithLength.

@TestTemplate
public void inputStreamRetryWithLength() throws Exception {
    HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
    final byte[] content = "abcde".getBytes("UTF-8");
    // Mock up an input stream that doesn't support marking
    request.setRequestBody(new UnmarkableInputStream(content), content.length);
    testInputStreamRetry(request, content);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 4 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method testCookieRenewOnPost.

@TestTemplate
public void testCookieRenewOnPost() throws Exception {
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    mockWebServer.enqueue(new MockResponse().setResponseCode(403).setBody("{\"error\":\"credentials_expired\", \"reason\":\"Session expired\"}\r\n"));
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    mockWebServer.enqueue(new MockResponse());
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
    HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
    request.setRequestBody("{\"some\": \"json\"}");
    HttpConnection response = c.executeRequest(request);
    String responseStr = response.responseAsString();
    assertTrue(responseStr.isEmpty(), "There should be no response body on the mock response");
    response.getConnection().getResponseCode();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) HttpConnection(com.cloudant.http.HttpConnection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 5 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method inputStreamRetry.

@TestTemplate
public void inputStreamRetry() throws Exception {
    HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
    final byte[] content = "abcde".getBytes("UTF-8");
    // Mock up an input stream that doesn't support marking
    request.setRequestBody(new UnmarkableInputStream(content));
    testInputStreamRetry(request, content);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

HttpConnection (com.cloudant.http.HttpConnection)27 TestTemplate (org.junit.jupiter.api.TestTemplate)11 JsonObject (com.google.gson.JsonObject)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 URI (java.net.URI)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 URL (java.net.URL)4 CloudantClient (com.cloudant.client.api.CloudantClient)3 DatabaseURIHelper (com.cloudant.client.internal.DatabaseURIHelper)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 HttpConnectionResponseInterceptor (com.cloudant.http.HttpConnectionResponseInterceptor)2 Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 InputStreamReader (java.io.InputStreamReader)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.jupiter.api.Test)2