@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); } }
@BeforeMethod public void setUp() throws Exception { MockitoAnnotations.initMocks(this); underTest = new LocalhostRequestChecker(); }
/** * Determines if the given request tries to access a localhost address. * @param request the given request * @return true if it targets localhost, false otherwise */ public boolean checkIfRequestTargetsLocalhost(final WilmaHttpRequest request) { URI requestUri = request.getUri(); return isLocalhost(requestUri); }
@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); }