use of com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory in project java-cloudant by cloudant.
the class SessionInterceptorExpiryTests method executeTest.
private void executeTest(boolean okUsable, String sessionPath, Long expiryTime, String cookieValue) throws Exception {
queueResponses(okUsable, sessionPath, expiryTime, cookieValue);
HttpConnection conn = Http.GET(mockWebServer.url("/").url());
conn.connectionFactory = (isOkUsable) ? new OkHttpClientHttpUrlConnectionFactory() : new DefaultHttpUrlConnectionFactory();
conn.requestInterceptors.add(rqInterceptor);
conn.responseInterceptors.add(rpInterceptor);
conn = conn.execute();
// Consume response stream and assert ok: true
String responseStr = conn.responseAsString();
String okPattern = ".*\"ok\"\\s*:\\s*true.*";
assertTrue(Pattern.compile(okPattern, Pattern.DOTALL).matcher(responseStr).matches(), "There should be an ok response: " + responseStr);
// Assert the _session request
RecordedRequest sessionRequest = mockWebServer.takeRequest(MockWebServerResources.TIMEOUT, MockWebServerResources.TIMEOUT_UNIT);
assertEquals(sessionPath, sessionRequest.getPath(), "The interceptor should make a " + "session request");
assertNull(sessionRequest.getHeader("Cookie"), "There should be no existing cookie on the" + " session request");
// Assert the GET request
RecordedRequest getRequest = mockWebServer.takeRequest(MockWebServerResources.TIMEOUT, MockWebServerResources.TIMEOUT_UNIT);
assertEquals("/", getRequest.getPath(), "The request path should be correct");
assertNotNull(getRequest.getHeader("Cookie"), "There should be a cookie on the request");
String expectedCookie = ((sessionPath.equals("/_session")) ? MockWebServerResources.AUTH_COOKIE_NAME : MockWebServerResources.IAM_COOKIE_NAME) + "=" + cookieValue;
assertEquals(expectedCookie, getRequest.getHeader("Cookie"), "The cookie should be the " + "correct session type");
}
Aggregations