### Question:
GetHtmlFromCurrentHostPredicate implements Predicate<HttpRequest> { @Override public boolean test(HttpRequest httpRequest) { try { boolean methodIsGet = httpRequest.getMethod().equals(HttpMethod.GET); String acceptHeader = httpRequest.headers().get(ACCEPT); if (acceptHeader == null) { logger.warn("No accept header for http request " + httpRequest.getUri()); return false; } boolean expectsHtmlContent = acceptHeader.contains(Constants.HTML); boolean isFromCurrentBaseUrl = httpRequest.getUri().startsWith("/") || httpRequest.getUri().contains(baseURI.getHost()); return expectsHtmlContent && isFromCurrentBaseUrl && methodIsGet; } catch (Exception e) { logger.error("Exception while checking predicate", e); return false; } } @Inject  GetHtmlFromCurrentHostPredicate(@Named(BASE_URI) URI baseURI); @Override boolean test(HttpRequest httpRequest);  }### Answer:
@Test public void whenTheHostIsTheSameThenNotChangingURLRequest() throws URISyntaxException { URI uri = new URI("http: HttpHeaders httpHeaders = mock(HttpHeaders.class); when(httpHeaders.get("Accept")).thenReturn("application/json"); HttpRequest httpRequest = mock(HttpRequest.class); when(httpRequest.getUri()).thenReturn("http: when(httpRequest.headers()).thenReturn(httpHeaders); when(httpRequest.getMethod()).thenReturn(HttpMethod.GET); GetHtmlFromCurrentHostPredicate getHtmlFromCurrentHostPredicate = new GetHtmlFromCurrentHostPredicate(uri); assertFalse(getHtmlFromCurrentHostPredicate.test(httpRequest)); }
@Test public void whenHostIsDifferentAndContentTypeIsNotHTPNotChangingURLRequest() throws URISyntaxException { URI uri = new URI("http: HttpHeaders httpHeaders = mock(HttpHeaders.class); when(httpHeaders.get("Accept")).thenReturn("application/html"); HttpRequest httpRequest = mock(HttpRequest.class); when(httpRequest.getUri()).thenReturn("http: when(httpRequest.headers()).thenReturn(httpHeaders); when(httpRequest.getMethod()).thenReturn(HttpMethod.GET); GetHtmlFromCurrentHostPredicate getHtmlFromCurrentHostPredicate = new GetHtmlFromCurrentHostPredicate(uri); assertTrue(getHtmlFromCurrentHostPredicate.test(httpRequest)); }
@Test public void ifRequestIsNotGetThenItIsFalse() throws URISyntaxException { URI uri = new URI("http: HttpHeaders httpHeaders = mock(HttpHeaders.class); when(httpHeaders.get("Accept")).thenReturn("application/html"); HttpRequest httpRequest = mock(HttpRequest.class); when(httpRequest.getUri()).thenReturn("http: when(httpRequest.headers()).thenReturn(httpHeaders); when(httpRequest.getMethod()).thenReturn(HttpMethod.POST); GetHtmlFromCurrentHostPredicate getHtmlFromCurrentHostPredicate = new GetHtmlFromCurrentHostPredicate(uri); assertFalse(getHtmlFromCurrentHostPredicate.test(httpRequest)); }
@Test public void isChangingRequest() throws URISyntaxException { URI uri = new URI("http: HttpHeaders httpHeaders = mock(HttpHeaders.class); when(httpHeaders.get("Accept")).thenReturn("application/html"); HttpRequest httpRequest = mock(HttpRequest.class); when(httpRequest.getUri()).thenReturn("http: when(httpRequest.headers()).thenReturn(httpHeaders); when(httpRequest.getMethod()).thenReturn(HttpMethod.GET); GetHtmlFromCurrentHostPredicate getHtmlFromCurrentHostPredicate = new GetHtmlFromCurrentHostPredicate(uri); assertFalse(getHtmlFromCurrentHostPredicate.test(httpRequest)); }
@Test public void whenRequestedTypeIsNotHTMLThenNotChangingURLRequest() throws URISyntaxException { URI uri = new URI("http: HttpHeaders httpHeaders = mock(HttpHeaders.class); when(httpHeaders.get("Accept")).thenReturn("application/json"); HttpRequest httpRequest = mock(HttpRequest.class); when(httpRequest.getUri()).thenReturn("http: when(httpRequest.headers()).thenReturn(httpHeaders); when(httpRequest.getMethod()).thenReturn(HttpMethod.GET); GetHtmlFromCurrentHostPredicate getHtmlFromCurrentHostPredicate = new GetHtmlFromCurrentHostPredicate(uri); assertFalse(getHtmlFromCurrentHostPredicate.test(httpRequest)); }