Search in sources :

Example 46 with CloudantClient

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

the class CloudantClientTests method testUserAgentHeaderIsAddedToRequest.

/**
 * Assert that requests have the User-Agent header added. This test runs a local HTTP server
 * process that can handle a single request to receive the request and validate the header.
 */
@Test
public void testUserAgentHeaderIsAddedToRequest() throws Exception {
    // send back an OK 200
    server.enqueue(new MockResponse());
    // instantiating the client performs a single post request
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
    String response = client.executeRequest(createPost(client.getBaseUri(), null, "application/json")).responseAsString();
    assertTrue(response.isEmpty(), "There should be no response body on the mock response");
    // assert that the request had the expected header
    String userAgentHeader = server.takeRequest(10, TimeUnit.SECONDS).getHeader("User-Agent");
    assertNotNull(userAgentHeader, "The User-Agent header should be present on the request");
    assertTrue(userAgentHeader.matches(userAgentUnknownRegex), "The value of the User-Agent " + "header " + userAgentHeader + " on the request" + " should match the format " + userAgentFormat);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test)

Example 47 with CloudantClient

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

the class CloudantClientTests method credentialsCheck.

private void credentialsCheck(ClientBuilder b, String encodedUser, String encodedPassword) throws Exception {
    CloudantClient c = b.build();
    server.enqueue(MockWebServerResources.OK_COOKIE);
    server.enqueue(MockWebServerResources.JSON_OK);
    HttpConnection conn = c.executeRequest(Http.GET(c.getBaseUri()));
    // Consume response stream and assert ok: true
    String responseStr = conn.responseAsString();
    assertNotNull(responseStr);
    // One request to _session then one to get info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
    // Get the _session request
    RecordedRequest request = server.takeRequest();
    String body = request.getBody().readUtf8();
    // body should be of form:
    // name=YourUserName&password=YourPassword
    Matcher m = CREDENTIALS.matcher(body);
    assertTrue(m.matches(), "The _session request should match the regex");
    assertEquals(2, m.groupCount(), "There should be a username group and a password group in" + " the creds");
    assertEquals(encodedUser, m.group(1), "The username should match");
    assertEquals(encodedPassword, m.group(2), "The password should match");
    // ensure that building a URL from it does not throw any exceptions
    new URL(c.getBaseUri().toString());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CloudantClient(com.cloudant.client.api.CloudantClient) HttpConnection(com.cloudant.http.HttpConnection) Matcher(java.util.regex.Matcher) URL(java.net.URL)

Aggregations

CloudantClient (com.cloudant.client.api.CloudantClient)47 MockResponse (okhttp3.mockwebserver.MockResponse)32 TestTemplate (org.junit.jupiter.api.TestTemplate)23 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)21 Test (org.junit.jupiter.api.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 Database (com.cloudant.client.api.Database)8 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)7 TestTimer (com.cloudant.tests.util.TestTimer)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 HttpConnection (com.cloudant.http.HttpConnection)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 Dispatcher (okhttp3.mockwebserver.Dispatcher)3 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)2 BasicAuthInterceptor (com.cloudant.http.interceptors.BasicAuthInterceptor)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)2