use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method inputStreamRetryGenerator.
@TestTemplate
public void inputStreamRetryGenerator() throws Exception {
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
byte[] content = "abcde".getBytes("UTF-8");
request.setRequestBody(new TestInputStreamGenerator(content));
testInputStreamRetry(request, content);
}
use of org.junit.jupiter.api.TestTemplate 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 org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testReadBeforeExecute.
/*
* Basic test to check that an IOException is thrown when we attempt to get the response
* without first calling execute()
*/
@TestTemplate
public void testReadBeforeExecute() throws Exception {
HttpConnection conn = new HttpConnection("POST", new URL(dbResource.getDbURIWithUserInfo()), "application/json");
ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes());
// nothing read from stream
assertEquals(data.getBytes().length, bis.available());
conn.setRequestBody(bis);
try {
String response = conn.responseAsString();
fail("IOException not thrown as expected instead had response " + response);
} catch (IOException ioe) {
// "Attempted to read response from server before calling execute()"
;
}
// stream was not read because execute() was not called
assertEquals(data.getBytes().length, bis.available());
}
use of org.junit.jupiter.api.TestTemplate 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");
}
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class SslAuthenticationTest method localSslAuthenticationEnabled.
/**
* Connect to the local simple https server with SSL authentication enabled explicitly.
* This should throw an exception because the SSL authentication fails.
*/
@TestTemplate
public void localSslAuthenticationEnabled() throws Exception {
CouchDbException thrownException = null;
try {
CloudantClient dbClient = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
// Queue a 200 OK response
server.enqueue(new MockResponse());
// Make an arbitrary connection to the DB.
dbClient.getAllDbs();
} catch (CouchDbException e) {
thrownException = e;
}
validateClientAuthenticationException(thrownException);
}
Aggregations