Java Code Examples for org.apache.http.client.methods.CloseableHttpResponse#setHeader()

The following examples show how to use org.apache.http.client.methods.CloseableHttpResponse#setHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CasAuthenticationHandlerTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
public void testCasAuthenticationKo() throws Exception {
    DriverRequest driverRequest = TestUtils.createDriverRequest(driver1);
    OutgoingRequest outgoingRequest =
            httpClientRequestExecutor.createOutgoingRequest(driverRequest, "http://localhost:8080", true);
    FragmentEvent event =
            new FragmentEvent(driverRequest.getOriginalRequest(), outgoingRequest, outgoingRequest.getContext());
    CloseableHttpResponse httpResponse = BasicCloseableHttpResponse.adapt(createMockResponse("0"));
    httpResponse.setHeader("Location", "http://localhost/loginurl?service=http");
    event.setHttpResponse(httpResponse);

    HttpResponse responseOnceAuthenticated = createMockResponse("1");
    mockConnectionManager.setResponse(responseOnceAuthenticated);

    handler.event(EventManager.EVENT_FRAGMENT_POST, event);

    // No extra request should be sent
    assertNull(mockConnectionManager.getSentRequest());
    // The response should be "unauthorized" as we cannot send the CAS ticket
    assertEquals(401, event.getHttpResponse().getStatusLine().getStatusCode());
}
 
Example 2
Source File: CasAuthenticationHandlerTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testCasAuthenticationOk() throws Exception {
    AttributePrincipal userPrincipal = new AttributePrincipal() {
        private static final long serialVersionUID = 1L;

        @Override
        public Map<String, Object> getAttributes() {
            return null;
        }

        @Override
        public String getName() {
            return "test";
        }

        @Override
        public String getProxyTicketFor(String arg0) {
            return "proxy_ticket";
        }
    };
    IncomingRequest incomingRequest = TestUtils.createIncomingRequest().setUserPrincipal(userPrincipal).build();
    DriverRequest driverRequest = new DriverRequest(incomingRequest, driver1, "/");
    OutgoingRequest outgoingRequest =
            httpClientRequestExecutor.createOutgoingRequest(driverRequest, "http://localhost:8080", true);
    FragmentEvent event =
            new FragmentEvent(driverRequest.getOriginalRequest(), outgoingRequest, outgoingRequest.getContext());
    CloseableHttpResponse httpResponse = BasicCloseableHttpResponse.adapt(createMockResponse("0"));
    httpResponse.setHeader("Location", "http://localhost/loginurl?service=http");
    event.setHttpResponse(httpResponse);

    HttpResponse responseOnceAuthenticated = createMockResponse("1");
    mockConnectionManager.setResponse(responseOnceAuthenticated);

    handler.event(EventManager.EVENT_FRAGMENT_POST, event);

    // A new request should have been sent with the proxy ticket
    assertNotNull(mockConnectionManager.getSentRequest());
    assertEquals("/?ticket=proxy_ticket", mockConnectionManager.getSentRequest().getRequestLine().getUri());
    assertEquals(200, event.getHttpResponse().getStatusLine().getStatusCode());
    assertEquals("1", EntityUtils.toString(event.getHttpResponse().getEntity()));
}