Search in sources :

Example 91 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class CloudantClientTests method cookieTest.

@Test
@RequiresCloudant
public void cookieTest() {
    Membership membership = account.getMembership();
    assertNotNull(membership);
}
Also used : Membership(com.cloudant.client.api.model.Membership) Test(org.junit.jupiter.api.Test) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

Example 92 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class CloudantClientTests method gatewayStyleURL.

@Test
public void gatewayStyleURL() throws Exception {
    final String gatewayPath = "/gateway";
    // Set a dispatcher that returns 200 if the requests have the correct path /gateway/_all_dbs
    // Otherwise return 400.
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (request.getPath().equals(gatewayPath + "/_all_dbs")) {
                return new MockResponse();
            } else {
                return new MockResponse().setResponseCode(400);
            }
        }
    });
    // Build a client with a URL that includes a path
    CloudantClient c = ClientBuilder.url(new URL(server.url(gatewayPath).toString())).build();
    // If the request path is wrong this call will return 400 and throw an exception failing the
    // test.
    c.getAllDbs();
    // Build a client with a URL that includes a path with a trailing /
    c = ClientBuilder.url(new URL(server.url(gatewayPath + "/").toString())).build();
    // If the request path is wrong this call will return 400 and throw an exception failing the
    // test.
    c.getAllDbs();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Dispatcher(okhttp3.mockwebserver.Dispatcher) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 93 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class CloudantClientTests method testBasicAuthFromCredentials.

/**
 * Test that configuring the Basic Authentication interceptor from credentials and adding to
 * the CloudantClient works.
 */
@Test
public void testBasicAuthFromCredentials() throws Exception {
    BasicAuthInterceptor interceptor = BasicAuthInterceptor.createFromCredentials("username", "password");
    // send back a mock OK 200
    server.enqueue(new MockResponse());
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(server).interceptors(interceptor).build();
    client.getAllDbs();
    // expected 'username:password'
    assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", server.takeRequest().getHeader("Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) BasicAuthInterceptor(com.cloudant.http.interceptors.BasicAuthInterceptor) CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test)

Example 94 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class CloudantClientTests method testUserAgentHeaderStringFromFile.

@Test
public void testUserAgentHeaderStringFromFile() throws Exception {
    // This doesn't read the a properties file, since the tests do not run from the published
    // jars.
    // Point to the built classes, it's a bit awkward but we need to load the class cleanly
    File f = new File("../cloudant-http/build/classes/main/");
    String userAgentHeader = new UserAgentInterceptor(new URLClassLoader(new URL[] { f.toURI().toURL() }) {

        @Override
        public InputStream getResourceAsStream(String name) {
            if (name.equals("META-INF/com.cloudant.client.properties")) {
                try {
                    return new ByteArrayInputStream(("user.agent.name=java-cloudant\nuser" + ".agent.version=1.6.1").getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
            return super.getResourceAsStream(name);
        }
    }, "META-INF/com.cloudant.client.properties").getUserAgent();
    assertTrue(userAgentHeader.matches(userAgentRegex), "The value of the User-Agent header: " + "" + userAgentHeader + " should match the " + "format: " + userAgentFormat);
}
Also used : UserAgentInterceptor(com.cloudant.http.internal.interceptors.UserAgentInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) URLClassLoader(java.net.URLClassLoader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 95 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class CloudantClientTests method testDefaultPorts.

@Test
public void testDefaultPorts() throws Exception {
    CloudantClient c = null;
    c = CloudantClientHelper.newTestAddressClient().build();
    assertEquals(80, c.getBaseUri().getPort(), "The http port should be 80");
    c = CloudantClientHelper.newHttpsTestAddressClient().build();
    assertEquals(443, c.getBaseUri().getPort(), "The http port should be 443");
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)67450 lombok.val (lombok.val)3880 File (java.io.File)2228 HashMap (java.util.HashMap)2180 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2164 ArrayList (java.util.ArrayList)2137 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2027 SqlSession (org.apache.ibatis.session.SqlSession)1845 List (java.util.List)1799 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1484 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1184 Map (java.util.Map)1143 IOException (java.io.IOException)1048 Path (java.nio.file.Path)1006 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)935 Date (java.util.Date)914 Method (java.lang.reflect.Method)862 TestBean (org.springframework.beans.testfixture.beans.TestBean)822 Transaction (org.neo4j.graphdb.Transaction)752 BaseDataTest (org.apache.ibatis.BaseDataTest)740