Search in sources :

Example 1 with DefaultDispatch

use of org.apache.knox.gateway.dispatch.DefaultDispatch in project knox by apache.

the class AuditLoggingTest method testHttpClientOutboundException.

@Test
public /**
 * Dispatching outbound request. Remote host is unreachable. Two log events is expected:
 *
 * action=dispatch request_type=uri outcome=FAILED
 * action=dispatch request_type=uri outcome=unavailable
 */
void testHttpClientOutboundException() throws IOException, URISyntaxException {
    String uri = "http://outbound-host:port/path";
    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getHeaderNames()).andReturn(Collections.enumeration(new ArrayList<String>())).anyTimes();
    EasyMock.replay(inboundRequest);
    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(outboundResponse);
    DefaultDispatch dispatch = new DefaultDispatch();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    dispatch.setHttpClient(client);
    try {
        dispatch.doGet(new URI(uri), inboundRequest, outboundResponse);
        fail("Expected exception while accessing to unreachable host");
    } catch (IOException e) {
        Iterator<LoggingEvent> iterator = CollectAppender.queue.iterator();
        LoggingEvent unavailableEvent = iterator.next();
        verifyValue((String) unavailableEvent.getMDC(AuditConstants.MDC_RESOURCE_NAME_KEY), uri);
        verifyValue((String) unavailableEvent.getMDC(AuditConstants.MDC_RESOURCE_TYPE_KEY), ResourceType.URI);
        verifyValue((String) unavailableEvent.getMDC(AuditConstants.MDC_ACTION_KEY), Action.DISPATCH);
        verifyValue((String) unavailableEvent.getMDC(AuditConstants.MDC_OUTCOME_KEY), ActionOutcome.UNAVAILABLE);
        LoggingEvent failureEvent = iterator.next();
        verifyValue((String) failureEvent.getMDC(AuditConstants.MDC_RESOURCE_NAME_KEY), uri);
        verifyValue((String) failureEvent.getMDC(AuditConstants.MDC_RESOURCE_TYPE_KEY), ResourceType.URI);
        verifyValue((String) failureEvent.getMDC(AuditConstants.MDC_ACTION_KEY), Action.DISPATCH);
        verifyValue((String) failureEvent.getMDC(AuditConstants.MDC_OUTCOME_KEY), ActionOutcome.FAILURE);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LoggingEvent(org.apache.log4j.spi.LoggingEvent) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) URI(java.net.URI) DefaultDispatch(org.apache.knox.gateway.dispatch.DefaultDispatch) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 URI (java.net.URI)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 DefaultDispatch (org.apache.knox.gateway.dispatch.DefaultDispatch)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 Test (org.junit.Test)1