LinkUtils { public static String getRelativeUri(String uri) { if (uri == null) { return null; } final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(uri); uriBuilder.scheme(null); uriBuilder.host(null); uriBuilder.port(-1); final String newUri = uriBuilder.toUriString(); return StringUtils.isEmpty(newUri) ? "/" : newUri; } private  LinkUtils(); static String getBaseUri(HttpServletRequest request); static String getNonBlankStringOrDefault(String string, String defaultString); static String getAbsoluteUrl(String relativeUri); static String getRelativeUri(String uri); static String getLocalUrl(String url); static String getLocalUrl(String url, String defaultRedirect); static boolean isLocalUrl(String url); static boolean isRelativeUrl(String url); static boolean isLoginPageLink(String url); static String getSafeRedirectUri(String uri); static String getSafeRedirectUri(String uri, String defaultUri); static final String COLAB_URL; }
@Test public void testGetRelativeUri() throws Exception { final String result = LinkUtils.getRelativeUri(BASE_URL + "/contests"); assertEquals("Relative URI not correct", "/contests", result); }
@Test public void testGetRelativeUri__queryParam() throws Exception { final String result = LinkUtils.getRelativeUri(BASE_URL + "/contests?test=1&test2=2"); assertEquals("Query params not handled correctly", "/contests?test=1&test2=2", result); }
@Test public void testGetRelativeUri__emptyPath__shouldReturnRoot() throws Exception { final String result = LinkUtils.getRelativeUri(BASE_URL); assertEquals("Empty path not handled correctly", "/", result); }
@Test public void testGetRelativeUri__relativeUri__shouldReturnItself() throws Exception { final String result = LinkUtils.getRelativeUri("/contests"); assertEquals("Relative uri not returned unchanged", "/contests", result); }
@Test public void testGetRelativeUri__empty__shouldReturnRoot() throws Exception { final String result = LinkUtils.getRelativeUri(""); assertEquals("Empty uri handled incorrectly", "/", result); }
@Test public void testGetRelativeUri__blank__shouldReturnRoot() throws Exception { final String result = LinkUtils.getRelativeUri(" "); assertEquals("Blank uri handled incorrectly", "/", result); }
@Test public void testGetRelativeUri__null__shouldReturnNull() throws Exception { final String result = LinkUtils.getRelativeUri(null); assertEquals("Null uri handled incorrectly", null, result); }