Search in sources :

Example 1 with ClientBuilder

use of com.cloudant.client.api.ClientBuilder in project java-cloudant by cloudant.

the class HttpProxyTest method proxiedRequest.

/**
 * This test validates that a request can successfully traverse a proxy to our mock server.
 */
@TestTemplate
public void proxiedRequest(final boolean okUsable, final boolean useSecureProxy, final boolean useHttpsServer, final boolean useProxyAuth) throws Exception {
    // mock a 200 OK
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            return new MockResponse();
        }
    });
    InetSocketAddress address = proxy.getListenAddress();
    URL proxyUrl = new URL((useSecureProxy) ? "https" : "http", address.getHostName(), address.getPort(), "/");
    ClientBuilder builder = CloudantClientHelper.newMockWebServerClientBuilder(server).proxyURL(proxyUrl);
    if (useProxyAuth) {
        builder.proxyUser(mockProxyUser).proxyPassword(mockProxyPass);
    }
    // We don't use SSL authentication for this test
    CloudantClient client = builder.disableSSLAuthentication().build();
    String response = client.executeRequest(Http.GET(client.getBaseUri())).responseAsString();
    assertTrue(response.isEmpty(), "There should be no response body on the mock response");
    // if it wasn't a 20x then an exception should have been thrown by now
    RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
    assertNotNull(request);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) InetSocketAddress(java.net.InetSocketAddress) Dispatcher(okhttp3.mockwebserver.Dispatcher) URL(java.net.URL) ClientBuilder(com.cloudant.client.api.ClientBuilder) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 2 with ClientBuilder

use of com.cloudant.client.api.ClientBuilder in project java-cloudant by cloudant.

the class CloudantClientTests method urlCheck.

private void urlCheck(String encodedUser, String encodedPassword) throws Exception {
    ClientBuilder b = ClientBuilder.url(server.url("").newBuilder().encodedUsername(encodedUser).encodedPassword(encodedPassword).build().url());
    credentialsCheck(b, encodedUser, encodedPassword);
}
Also used : ClientBuilder(com.cloudant.client.api.ClientBuilder)

Aggregations

ClientBuilder (com.cloudant.client.api.ClientBuilder)2 CloudantClient (com.cloudant.client.api.CloudantClient)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 Dispatcher (okhttp3.mockwebserver.Dispatcher)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 TestTemplate (org.junit.jupiter.api.TestTemplate)1