Search in sources :

Example 1 with UserAgentInterceptor

use of com.cloudant.http.internal.interceptors.UserAgentInterceptor 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 2 with UserAgentInterceptor

use of com.cloudant.http.internal.interceptors.UserAgentInterceptor in project java-cloudant by cloudant.

the class CloudantClientTests method testUserAgentHeaderString.

/**
 * Assert that the User-Agent header is of the expected form.
 */
@Test
public void testUserAgentHeaderString() throws Exception {
    // This doesn't read the a properties file, since the tests do not run from the published
    // jars.
    String userAgentHeader = new UserAgentInterceptor(UserAgentInterceptor.class.getClassLoader(), "META-INF/com.cloudant.client.properties").getUserAgent();
    assertTrue(userAgentHeader.matches(userAgentUnknownRegex), "The value of the User-Agent " + "header: " + userAgentHeader + " should match the " + "format: " + userAgentFormat);
}
Also used : UserAgentInterceptor(com.cloudant.http.internal.interceptors.UserAgentInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

UserAgentInterceptor (com.cloudant.http.internal.interceptors.UserAgentInterceptor)2 Test (org.junit.jupiter.api.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLClassLoader (java.net.URLClassLoader)1