Search in sources :

Example 1 with DefaultHttpUrlConnectionFactory

use of com.cloudant.http.internal.DefaultHttpUrlConnectionFactory 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");
}
Also used : OkHttpClientHttpUrlConnectionFactory(com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) HttpConnection(com.cloudant.http.HttpConnection) DefaultHttpUrlConnectionFactory(com.cloudant.http.internal.DefaultHttpUrlConnectionFactory)

Aggregations

HttpConnection (com.cloudant.http.HttpConnection)1 DefaultHttpUrlConnectionFactory (com.cloudant.http.internal.DefaultHttpUrlConnectionFactory)1 OkHttpClientHttpUrlConnectionFactory (com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1