Search in sources :

Example 36 with TestTemplate

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);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 37 with TestTemplate

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.");
}
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 38 with TestTemplate

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());
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URL(java.net.URL) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 39 with TestTemplate

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

Example 40 with TestTemplate

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);
}
Also used : CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

TestTemplate (org.junit.jupiter.api.TestTemplate)51 CloudantClient (com.cloudant.client.api.CloudantClient)23 URI (java.net.URI)18 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)18 MockResponse (okhttp3.mockwebserver.MockResponse)16 HttpConnection (com.cloudant.http.HttpConnection)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 TreeMap (java.util.TreeMap)5 TestTimer (com.cloudant.tests.util.TestTimer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)2 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2