fix double slash for append and getCompleteUrl of HttpUtil

This commit is contained in:
Sebastian Sdorra
2014-01-07 08:08:32 +01:00
parent 9b850543cd
commit 2e3bd0ef8f
2 changed files with 37 additions and 13 deletions

View File

@@ -197,7 +197,11 @@ public final class HttpUtil
*/ */
public static String append(String uri, String suffix) public static String append(String uri, String suffix)
{ {
if (!uri.endsWith(SEPARATOR_PATH) &&!suffix.startsWith(SEPARATOR_PATH)) if ( uri.endsWith(SEPARATOR_PATH) && suffix.startsWith(SEPARATOR_PATH) )
{
uri = uri.substring( 0, uri.length() - 1 );
}
else if (!uri.endsWith(SEPARATOR_PATH) &&!suffix.startsWith(SEPARATOR_PATH))
{ {
uri = uri.concat(SEPARATOR_PATH); uri = uri.concat(SEPARATOR_PATH);
} }
@@ -490,18 +494,7 @@ public final class HttpUtil
public static String getCompleteUrl(ScmConfiguration configuration, public static String getCompleteUrl(ScmConfiguration configuration,
String path) String path)
{ {
String url = configuration.getBaseUrl(); return append(configuration.getBaseUrl(), path);
if (url.endsWith(SEPARATOR_PATH) && path.startsWith(SEPARATOR_PATH))
{
url = url.substring(0, url.length());
}
else if (!path.startsWith(SEPARATOR_PATH))
{
path = SEPARATOR_PATH.concat(path);
}
return url.concat(path);
} }
/** /**

View File

@@ -54,6 +54,33 @@ import javax.servlet.http.HttpServletRequest;
public class HttpUtilTest public class HttpUtilTest
{ {
/**
* Method description
*
*/
@Test
public void appendTest()
{
//J-
assertEquals(
"http://www.scm-manager/scm/test",
HttpUtil.append("http://www.scm-manager/scm/", "test")
);
assertEquals(
"http://www.scm-manager/scm/test",
HttpUtil.append("http://www.scm-manager/scm", "test")
);
assertEquals(
"http://www.scm-manager/scm/test",
HttpUtil.append("http://www.scm-manager/scm", "/test")
);
assertEquals(
"http://www.scm-manager/scm/test",
HttpUtil.append("http://www.scm-manager/scm/", "/test")
);
//J+
}
/** /**
* Method description * Method description
* *
@@ -173,6 +200,10 @@ public class HttpUtilTest
HttpUtil.getCompleteUrl(config, "test/path")); HttpUtil.getCompleteUrl(config, "test/path"));
assertEquals("http://www.scm-manager.org/scm/test/path", assertEquals("http://www.scm-manager.org/scm/test/path",
HttpUtil.getCompleteUrl(config, "/test/path")); HttpUtil.getCompleteUrl(config, "/test/path"));
config.setBaseUrl("http://www.scm-manager.org/scm/");
assertEquals("http://www.scm-manager.org/scm/test/path",
HttpUtil.getCompleteUrl(config, "/test/path"));
} }
/** /**