LeaderRedirect implements Closeable { Optional<String> getRedirectTarget(HttpServletRequest req) { Optional<HostAndPort> redirectTarget = getRedirect(); if (redirectTarget.isPresent()) { HostAndPort target = redirectTarget.get(); StringBuilder redirect = new StringBuilder() .append(req.getScheme()) .append(": .append(target.getHostText()) .append(':') .append(target.getPort()) .append( Optional.fromNullable( req.getAttribute(JettyServerModule.ORIGINAL_PATH_ATTRIBUTE_NAME)) .or(req.getRequestURI())); String queryString = req.getQueryString(); if (queryString != null) { redirect.append('?').append(queryString); } return Optional.of(redirect.toString()); } else { return Optional.absent(); } } @Inject  LeaderRedirect(HttpService httpService, ServiceGroupMonitor serviceGroupMonitor); void monitor(); @Override void close();  }
@Test public void testRedirectTargetNoAttribute() throws Exception { HttpServletRequest mockRequest = mockRequest(null, null); replayAndMonitor(1); HostAndPort remote = HostAndPort.fromParts("foobar", HTTP_PORT); publishSchedulers(remote); assertEquals( Optional.of("http: leaderRedirector.getRedirectTarget(mockRequest)); }
@Test public void testRedirectTargetWithAttribute() throws Exception { HttpServletRequest mockRequest = mockRequest("/the/original/path", null); replayAndMonitor(1); HostAndPort remote = HostAndPort.fromParts("foobar", HTTP_PORT); publishSchedulers(remote); assertEquals( Optional.of("http: leaderRedirector.getRedirectTarget(mockRequest)); }
@Test public void testRedirectTargetQueryString() throws Exception { HttpServletRequest mockRequest = mockRequest(null, "bar=baz"); replayAndMonitor(1); HostAndPort remote = HostAndPort.fromParts("foobar", HTTP_PORT); publishSchedulers(remote); assertEquals( Optional.of("http: leaderRedirector.getRedirectTarget(mockRequest)); }