Search in sources :

Example 31 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class HttpTest method noErrorStream401.

/**
 * Test that having no body and hence no error stream on a 401 response correctly results in a
 * 401 cookie renewal without a NPE.
 *
 * @throws Exception
 */
@TestTemplate
public void noErrorStream401() throws Exception {
    // Respond with a cookie init to the first request to _session
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    // Respond to the executeRequest GET of / with a 401 with no body
    mockWebServer.enqueue(new MockResponse().setResponseCode(401));
    // 401 triggers a renewal so respond with a new cookie for renewal request to _session
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    // Finally respond 200 OK with body of "TEST" to the replay of GET to /
    mockWebServer.enqueue(new MockResponse().setBody("TEST"));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
    String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
    assertEquals("TEST", response, "The expected response body should be received");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 32 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class HttpTest method testCustomHeader.

@TestTemplate
public void testCustomHeader() throws Exception {
    mockWebServer.enqueue(new MockResponse());
    final String headerName = "Test-Header";
    final String headerValue = "testHeader";
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new HttpConnectionRequestInterceptor() {

        @Override
        public HttpConnectionInterceptorContext interceptRequest(HttpConnectionInterceptorContext context) {
            context.connection.requestProperties.put(headerName, headerValue);
            return context;
        }
    }).build();
    client.getAllDbs();
    assertEquals(1, mockWebServer.getRequestCount(), "There should have been 1 request");
    RecordedRequest request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
    assertNotNull(request, "The recorded request should not be null");
    assertNotNull(request.getHeader(headerName), "The custom header should have been present");
    assertEquals(headerValue, request.getHeader(headerName), "The custom header should have the specified value");
}
Also used : HttpConnectionRequestInterceptor(com.cloudant.http.HttpConnectionRequestInterceptor) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 33 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class HttpTest method testCookieAuthWithPath.

@TestTemplate
public void testCookieAuthWithPath() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    mockWebServer.enqueue(MockWebServerResources.JSON_OK);
    CloudantClient client = ClientBuilder.url(mockWebServer.url("/pathex").url()).username("user").password("password").build();
    // send single request
    client.database("test", true);
    assertThat("_session is the second element of the path", mockWebServer.takeRequest().getPath(), is("/pathex/_session"));
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 34 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class HttpTest method cookieAppliedToDifferentURL.

/**
 * Test that the stored cookie is applied to requests for different URLs. Most of the other
 * tests just check a single URL.
 *
 * @throws Exception
 */
@TestTemplate
public void cookieAppliedToDifferentURL() throws Exception {
    mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
    mockWebServer.enqueue(new MockResponse().setBody("first"));
    mockWebServer.enqueue(new MockResponse().setBody("second"));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
    URI baseURI = c.getBaseUri();
    URL first = new URL(baseURI.getScheme(), baseURI.getHost(), baseURI.getPort(), "/testdb");
    String response = c.executeRequest(Http.GET(first)).responseAsString();
    assertEquals("first", response, "The correct response body should be present");
    // There should be a request for a cookie followed by a the real request
    assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 requests");
    assertEquals("/_session", MockWebServerResources.takeRequestWithTimeout(mockWebServer).getPath(), "The first request should have been for a cookie");
    RecordedRequest request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
    assertEquals("/testdb", request.getPath(), "The second request should have been for /testdb");
    assertNotNull(request.getHeader("Cookie"), "There should be a cookie on the request");
    // Now make a request to another URL
    URL second = new URL(baseURI.getScheme(), baseURI.getHost(), baseURI.getPort(), "/_all_dbs");
    response = c.executeRequest(Http.GET(second)).responseAsString();
    assertEquals("second", response, "The correct response body should be present");
    // There should now be an additional request
    assertEquals(3, mockWebServer.getRequestCount(), "There should be 3 requests");
    request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
    assertEquals("/_all_dbs", request.getPath(), "The second request should have been for " + "/_all_dbs");
    String cookieHeader = request.getHeader("Cookie");
    assertNotNull(cookieHeader, "There should be a cookie on the request");
    assertTrue(request.getHeader("Cookie").contains(EXPECTED_OK_COOKIE), "The cookie header " + cookieHeader + " should contain the expected value.");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) URL(java.net.URL) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 35 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class HttpTest method test429BackoffMaxMoreThanRetriesAllowed.

/**
 * Test that the outer number of configured retries takes precedence.
 *
 * @throws Exception
 */
@TestTemplate
public void test429BackoffMaxMoreThanRetriesAllowed() throws Exception {
    // Always respond 429 for this test
    mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
    try {
        CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(10, 1, true)).build();
        String response = c.executeRequest(Http.GET(c.getBaseUri()).setNumberOfRetries(3)).responseAsString();
        fail("There should be a TooManyRequestsException instead had response " + response);
    } catch (TooManyRequestsException e) {
        assertEquals(3, mockWebServer.getRequestCount(), "There should be 3 request attempts");
    }
}
Also used : Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) TooManyRequestsException(com.cloudant.client.org.lightcouch.TooManyRequestsException) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

CloudantClient (com.cloudant.client.api.CloudantClient)47 MockResponse (okhttp3.mockwebserver.MockResponse)32 TestTemplate (org.junit.jupiter.api.TestTemplate)23 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)21 Test (org.junit.jupiter.api.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 Database (com.cloudant.client.api.Database)8 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)7 TestTimer (com.cloudant.tests.util.TestTimer)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 HttpConnection (com.cloudant.http.HttpConnection)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 Dispatcher (okhttp3.mockwebserver.Dispatcher)3 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)2 BasicAuthInterceptor (com.cloudant.http.interceptors.BasicAuthInterceptor)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)2