@Override public void process(final WilmaHttpEntity entity) throws ApplicationException { WilmaHttpRequest request = (WilmaHttpRequest) entity; /** * If this method was called then localhost blocking is turned on, * so we only need to check if the request targets localhost in order to determine if it needs to be rerouted. */ boolean requestNeedsRerouting = localhostRequestChecker.checkIfRequestTargetsLocalhost(request); if (requestNeedsRerouting) { localhostRequestRouter.reroute(request); request.setRerouted(true); } }
/** * Changes the requests destination URI to the servlet that will answer back that the request was blocked. * @param request the request to reroute */ public void reroute(final WilmaHttpRequest request) { try { URI uri; uri = createUri(); request.setUri(uri); } catch (URISyntaxException e) { logger.error(e.getMessage()); } }
@Test public void testWhenOnApplicationEventReceivedShouldSetInternalPort() { //GIVEN given(routeEngineConfigurationAccess.getProperties()).willReturn(propertyDTO); given(propertyDTO.getProxyPort()).willReturn(9876); //WHEN underTest.onApplicationEvent(null); //THEN assertEquals(9876, Whitebox.getInternalState(underTest, "internalPort")); }
@Test public void testThatRerouteShouldSetRequestUriToServletAddress() throws URISyntaxException { //GIVEN URI expected = new URI("http://127.0.0.1:0/local/"); //WHEN underTest.reroute(request); //THEN verify(request).setUri(expected); }
@Test public void testIfEnabledShouldNotRerouteNonLocalhostRequest() throws ApplicationException { //GIVEN underTest.setEnabled(true); given(localhostRequestChecker.checkIfRequestTargetsLocalhost(Mockito.any(WilmaHttpRequest.class))).willReturn(false); //WHEN underTest.process(request); //THEN verify(localhostRequestRouter, never()).reroute(request); verify(request, never()).setRerouted(true); }
@Test public void testIfEnabledShouldRerouteLocalhostRequest() throws ApplicationException { //GIVEN underTest.setEnabled(true); given(localhostRequestChecker.checkIfRequestTargetsLocalhost(Mockito.any(WilmaHttpRequest.class))).willReturn(true); //WHEN underTest.process(request); //THEN verify(localhostRequestRouter).reroute(request); verify(request).setRerouted(true); }