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);
}
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());
}
Aggregations