@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); } }
@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 testCheckConditionShouldReturnTrueWhenRequestHasLocalhostNameUri() throws URISyntaxException { //GIVEN uri = new URI("http://localhost:1234/blabla"); given(request.getUri()).willReturn(uri); //WHEN boolean actualResult = underTest.checkIfRequestTargetsLocalhost(request); //THEN assertTrue(actualResult); }
@Test public void testCheckConditionShouldReturnFalseWhenRequestHasNonLocalhostUri() throws URISyntaxException { //GIVEN uri = new URI("http://somehostwhichisnotlocal:1234/blabla"); given(request.getUri()).willReturn(uri); //WHEN boolean actualResult = underTest.checkIfRequestTargetsLocalhost(request); //THEN assertFalse(actualResult); }
@Test public void testCheckConditionShouldReturnTrueWhenRequestHasLocalhostIpUri() throws URISyntaxException { //GIVEN uri = new URI("http://127.0.0.1:1234/blabla"); given(request.getUri()).willReturn(uri); //WHEN boolean actualResult = underTest.checkIfRequestTargetsLocalhost(request); //THEN assertTrue(actualResult); }
@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); }