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