use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method setHttpConfigOptions.
/**
* Test that options set on the client apply to the token request
*/
@Test
void setHttpConfigOptions() {
CouchDbSessionAuthenticator a = Mockito.spy(testAuthenticator);
TestCloudantService testCloudantService = new TestCloudantService("test", a);
HttpConfigOptions options = new HttpConfigOptions.Builder().build();
testCloudantService.configureClient(options);
// Verify the setter was called
Mockito.verify(a).setHttpConfigOptions(options);
}
use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method getCookieJar.
/**
* Validate that the cookie jar used by the authenticator matches the one on the service client
*/
@Test
void getCookieJar() {
TestCloudantService testCloudantService = new TestCloudantService("test", testAuthenticator);
assertEquals(testAuthenticator.getCookieJar(), testCloudantService.getClient().cookieJar(), "The " + "authenticator and client should use the same cookie jar.");
}
use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method requestTokenNoCookie.
/**
* Test that an exception is thrown in the case there is no cookie returned.
*
* @throws Exception
*/
@Test
void requestTokenNoCookie() throws Exception {
MockWebServer server = new MockWebServer();
MockResponse sessionPostErrorResponse = new MockResponse().setBody("{\"ok\": true}");
server.start();
try {
server.getPort();
server.enqueue(sessionPostErrorResponse);
TestCloudantService testCloudantService = new TestCloudantService("test", testAuthenticator);
testCloudantService.setServiceUrl("http://" + server.getHostName() + ":" + server.getPort());
try {
testCloudantService.getSessionInformation().execute().getResult();
fail("A ServiceResponseException should be thrown.");
} catch (ServiceResponseException e) {
assertEquals(200, e.getStatusCode(), "The status code should be 200.");
}
} finally {
server.shutdown();
}
}
use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method setHeaders.
/**
* Test that setting default headers for the client apply to the token request
*/
@Test
void setHeaders() {
CouchDbSessionAuthenticator a = Mockito.spy(testAuthenticator);
TestCloudantService testCloudantService = new TestCloudantService("test", a);
testCloudantService.setDefaultHeaders(customHeaders);
// Verify the setter was called
Mockito.verify(a).setHeaders(customHeaders);
// Assert that the headers are the custom header + the SDK defaults
Map<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.putAll(customHeaders);
expectedHeaders.putAll(SdkCommon.getSdkHeaders("authenticatorPostSession"));
assertEquals(expectedHeaders, a.getHeaders(), "The expected headers should match.");
}
use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method requestTokenUnauthorized.
/**
* Test that an exception from requestToken propagates correctly.
*
* @throws Exception
*/
@Test
void requestTokenUnauthorized() throws Exception {
MockWebServer server = new MockWebServer();
MockResponse sessionPostErrorResponse = new MockResponse().setStatus("HTTP/1.1 401 UNAUTHORIZED").setBody("{\"error\":\"unauthorized\",\"reason\":\"Name or password is incorrect" + ".\"}");
server.start();
try {
server.getPort();
server.enqueue(sessionPostErrorResponse);
TestCloudantService testCloudantService = new TestCloudantService("test", testAuthenticator);
testCloudantService.setServiceUrl("http://" + server.getHostName() + ":" + server.getPort());
try {
testCloudantService.getSessionInformation().execute().getResult();
fail("A ServiceResponseException should be thrown.");
} catch (ServiceResponseException e) {
assertEquals(401, e.getStatusCode(), "The status code should be 401.");
assertEquals("unauthorized", e.getMessage(), "The error should be " + "unauthorized.");
assertEquals("Name or password is incorrect.", e.getDebuggingInfo().get("reason"), "The reason should be as expected.");
}
} finally {
server.shutdown();
}
}
Aggregations