Search in sources :

Example 1 with TestCloudantService

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);
}
Also used : TestCloudantService(com.ibm.cloud.cloudant.internal.TestCloudantService) HttpConfigOptions(com.ibm.cloud.sdk.core.http.HttpConfigOptions) Test(org.testng.annotations.Test)

Example 2 with TestCloudantService

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.");
}
Also used : TestCloudantService(com.ibm.cloud.cloudant.internal.TestCloudantService) Test(org.testng.annotations.Test)

Example 3 with TestCloudantService

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();
    }
}
Also used : TestCloudantService(com.ibm.cloud.cloudant.internal.TestCloudantService) MockResponse(okhttp3.mockwebserver.MockResponse) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.testng.annotations.Test)

Example 4 with TestCloudantService

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.");
}
Also used : TestCloudantService(com.ibm.cloud.cloudant.internal.TestCloudantService) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 5 with TestCloudantService

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();
    }
}
Also used : TestCloudantService(com.ibm.cloud.cloudant.internal.TestCloudantService) MockResponse(okhttp3.mockwebserver.MockResponse) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.testng.annotations.Test)

Aggregations

TestCloudantService (com.ibm.cloud.cloudant.internal.TestCloudantService)6 Test (org.testng.annotations.Test)6 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 ServiceResponseException (com.ibm.cloud.sdk.core.service.exception.ServiceResponseException)2 HashMap (java.util.HashMap)2 HttpConfigOptions (com.ibm.cloud.sdk.core.http.HttpConfigOptions)1 List (java.util.List)1 Map (java.util.Map)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1