@Override public Optional<HttpUrl> redirectTo(HttpUrl current, String redirectBaseUrl) { return redirectTo(current, HttpUrl.parse(redirectBaseUrl)); }
@Test public void testRedirectTo_updatesCurrentPointer() throws Exception { UrlSelectorImpl selector = UrlSelectorImpl.create(list("http://foo/a", "http://bar/a"), false); HttpUrl current = HttpUrl.parse("http://baz/a/b/path"); String redirectTo = "http://bar/a"; assertThat(selector.redirectTo(current, redirectTo)).contains(HttpUrl.parse("http://bar/a/b/path")); assertThat(selector.redirectToCurrent(current)).contains(HttpUrl.parse("http://bar/a/b/path")); }
@Test public void testRedirectToNextRoundRobin() { UrlSelectorImpl selector = UrlSelectorImpl.create(list("http://foo/a", "http://bar/a"), false); HttpUrl current = HttpUrl.parse("http://baz/a/b/path"); String redirectTo = "http://bar/a"; assertThat(selector.redirectTo(current, redirectTo)).contains(HttpUrl.parse("http://bar/a/b/path")); assertThat(selector.redirectToNextRoundRobin(current)).contains(HttpUrl.parse("http://foo/a/b/path")); assertThat(selector.redirectToNextRoundRobin(current)).contains(HttpUrl.parse("http://bar/a/b/path")); }
@Override public Optional<HttpUrl> redirectTo(HttpUrl current, String redirectBaseUrl) { return redirectTo(current, HttpUrl.parse(redirectBaseUrl)); }
@Override public Optional<HttpUrl> redirectToCurrent(HttpUrl current) { return redirectTo(current, baseUrls.get(currentUrl.get())); }
@Override public Optional<HttpUrl> redirectToCurrent(HttpUrl current) { return redirectTo(current, baseUrls.get(currentUrl.get())); }
@Override public Optional<HttpUrl> redirectToNextRoundRobin(HttpUrl current) { Optional<HttpUrl> nextUrl = getNext(currentUrl.get()); if (nextUrl.isPresent()) { return redirectTo(current, nextUrl.get()); } return redirectTo(current, baseUrls.get((currentUrl.get() + 1) % baseUrls.size())); }
@Override public Optional<HttpUrl> redirectToNextRoundRobin(HttpUrl current) { Optional<HttpUrl> nextUrl = getNext(currentUrl.get()); if (nextUrl.isPresent()) { return redirectTo(current, nextUrl.get()); } return redirectTo(current, baseUrls.get((currentUrl.get() + 1) % baseUrls.size())); }
@Override public Optional<HttpUrl> redirectToNext(HttpUrl existingUrl) { // if possible, determine the index of the passed in url (so we can be sure to return a url which is different) Optional<Integer> existingUrlIndex = indexFor(existingUrl); int potentialNextIndex = existingUrlIndex.orElse(currentUrl.get()); Optional<HttpUrl> nextUrl = getNext(potentialNextIndex); if (nextUrl.isPresent()) { return redirectTo(existingUrl, nextUrl.get()); } // No healthy URLs remain; re-balance across any specified nodes return redirectTo(existingUrl, baseUrls.get((existingUrlIndex.orElse(currentUrl.get()) + 1) % baseUrls.size())); }
@Override public Optional<HttpUrl> redirectToNext(HttpUrl existingUrl) { // if possible, determine the index of the passed in url (so we can be sure to return a url which is different) Optional<Integer> existingUrlIndex = indexFor(existingUrl); int potentialNextIndex = existingUrlIndex.orElse(currentUrl.get()); Optional<HttpUrl> nextUrl = getNext(potentialNextIndex); if (nextUrl.isPresent()) { return redirectTo(existingUrl, nextUrl.get()); } // No healthy URLs remain; re-balance across any specified nodes return redirectTo(existingUrl, baseUrls.get((existingUrlIndex.orElse(currentUrl.get()) + 1) % baseUrls.size())); }
@Test public void testRedirectTo_succeedsWhenRequestedBaseUrlPathIsPrefixOfCurrentPath() throws Exception { String url1 = "http://foo/a"; String url2 = "https://bar:8080/a/b/c"; List<String> baseUrls = list(url1, url2); UrlSelectorImpl selector = UrlSelectorImpl.create(baseUrls, false); // Redirect to self is OK. assertThat(selector.redirectTo(parse(url1), url1)).contains(parse(url1)); assertThat(selector.redirectTo(parse(url1 + "/123?abc"), url1)).contains(parse(url1 + "/123?abc")); // Redirect to other is OK when current path is a prefix of the requested base URL. // Note: url2 -->url1 redirects across schema+host+port assertThat(selector.redirectTo(parse(url2), url1)).contains(parse(url1 + "/b/c")); assertThat(selector.redirectTo(parse(url2 + "/123?abc"), url1)).contains(parse(url1 + "/b/c/123?abc")); }
@Test public void testRedirectTo_doesNotFindMatchesForCaseSentitivePaths() throws Exception { String baseUrl = "http://foo/a"; UrlSelectorImpl selector = UrlSelectorImpl.create(list(baseUrl), false); assertThat(selector.redirectTo(parse(baseUrl), "http://foo/A")).isEmpty(); }
@Test public void testRedirectTo_failsWhenRequestedBaseUrlPathIsNotPrefixOfCurrentPath() throws Exception { String url1 = "http://foo/a"; String url2 = "https://bar:8080/a/b/c"; UrlSelectorImpl selector = UrlSelectorImpl.create(list(url1, url2), false); assertThat(selector.redirectTo(parse(url1), url2)).isEmpty(); }
@Test public void testRedirectTo_findsMatchesWithCaseInsensitiveHostNames() throws Exception { String baseUrl = "http://foo/a"; UrlSelectorImpl selector = UrlSelectorImpl.create(list(baseUrl), false); assertThat(selector.redirectTo(parse(baseUrl), "http://FOO/a")).contains(parse(baseUrl)); }