helpers: Avoid adding language prefix if already present
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Sep 2016 19:37:27 +0000 (21:37 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Sep 2016 19:37:27 +0000 (21:37 +0200)
Fixes #2444

helpers/url.go
helpers/url_test.go

index 1d87cf89f4d02138f79fb0e90c05467bfb9584fb..1bc6042e6179c1750dbc6e5bdf2710126108c8ee 100644 (file)
@@ -169,8 +169,15 @@ func AbsURL(in string, addLanguage bool) string {
 
        if addLanguage {
                prefix := getLanguagePrefix()
+               hasPrefix := false
+               // avoid adding language prefix if already present
+               if strings.HasPrefix(in, "/") {
+                       hasPrefix = strings.HasPrefix(in[1:], prefix)
+               } else {
+                       hasPrefix = strings.HasPrefix(in, prefix)
+               }
 
-               if prefix != "" {
+               if prefix != "" && !hasPrefix {
                        addSlash := in == "" || strings.HasSuffix(in, "/")
                        in = path.Join(prefix, in)
 
@@ -224,7 +231,15 @@ func RelURL(in string, addLanguage bool) string {
 
        if addLanguage {
                prefix := getLanguagePrefix()
-               if prefix != "" {
+               hasPrefix := false
+               // avoid adding language prefix if already present
+               if strings.HasPrefix(in, "/") {
+                       hasPrefix = strings.HasPrefix(in[1:], prefix)
+               } else {
+                       hasPrefix = strings.HasPrefix(in, prefix)
+               }
+
+               if prefix != "" && !hasPrefix {
                        hadSlash := strings.HasSuffix(u, "/")
 
                        u = path.Join(prefix, u)
index d1fadb25299db1e7a4fb4ca146f8f4adbe541b12..2cf93b859dedf5acd5d7caeb28200465a46fe503 100644 (file)
@@ -69,12 +69,14 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
                expected string
        }{
                {"/test/foo", "http://base/", "http://base/MULTItest/foo"},
+               {"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
                {"", "http://base/ace/", "http://base/ace/MULTI"},
                {"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
                {"http://abs", "http://base/", "http://abs"},
                {"schema://abs", "http://base/", "schema://abs"},
                {"//schemaless", "http://base/", "//schemaless"},
                {"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
+               {lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
                {"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
                {"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
        }
@@ -141,6 +143,8 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
                expected string
        }{
                {"/test/foo", "http://base/", false, "MULTI/test/foo"},
+               {"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
+               {lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
                {"test.css", "http://base/sub", false, "/subMULTI/test.css"},
                {"test.css", "http://base/sub", true, "MULTI/test.css"},
                {"/test/", "http://base/", false, "MULTI/test/"},