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