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