Skip to content

Commit

Permalink
#3438 - Fix bug where duplicate scheme is added to rewritten url value (
Browse files Browse the repository at this point in the history
#3439)

Co-authored-by: Bart Thierens <bart.thierens@kbc.be>
  • Loading branch information
senn and Bart Thierens authored Oct 1, 2024
1 parent a059ed5 commit 4f4f18c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ private String handleMatchingPatternAttribute(Pattern pattern, String attrValue)
url = prependHostName(url);
// Added check to determine whether the existing host has to be replaced
if (this.replaceHost) {
int index = attrValue.indexOf("://");
sb.setLength(0);
sb.append(attrValue, 0, index + 1);
if (!url.contains("://")) {
String reuseScheme = attrValue.substring(0, attrValue.indexOf("://") + 1);
sb.append(reuseScheme);
}
sb.append(url);
} else {
m.appendReplacement(sb, Matcher.quoteReplacement(url));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

import java.util.List;

Expand Down Expand Up @@ -124,28 +122,40 @@ public void test_with_prefix_and_matching_pattern_and_single_host() throws Excep

@Test
public void test_with_prefix_and_matching_pattern_and_single_host_and_replace_host() throws Exception {
test_with_prefix_and_matching_pattern_and_single_host_and_replace_host_custom_scheme(null);
test_with_prefix_and_matching_pattern_and_single_host_and_replace_host_custom_scheme("https");
test_with_prefix_and_matching_pattern_and_single_host_and_replace_host_custom_scheme("http");
}

private void test_with_prefix_and_matching_pattern_and_single_host_and_replace_host_custom_scheme(String scheme) throws Exception {
MockBundle bundle = new MockBundle(-1);
MockComponentContext ctx = new MockComponentContext(bundle);
ctx.setProperty("prefixes", new String[] { "/content/dam" });
ctx.setProperty("attributes", new String[] { "img:src" });
ctx.setProperty("host.pattern", "static.host.com");
ctx.setProperty("matchingPatterns", "img:src;(\\/content\\/dam\\/.+?\\.(png|jpg))");
ctx.setProperty("replaceHost", true);
if (scheme != null) {
ctx.setProperty("host.scheme", scheme);
}

StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
factory.activate(ctx);

reset(handler);
Transformer transformer = factory.createTransformer();
transformer.setContentHandler(handler);

AttributesImpl imageWithJustSrc = new AttributesImpl();
imageWithJustSrc.addAttribute(null, "src", null, "CDATA", "https://www.host.com/content/dam/flower.jpg");
String inputUrl = "https://www.host.com/content/dam/flower.jpg";
imageWithJustSrc.addAttribute(null, "src", null, "CDATA", inputUrl);
transformer.startElement(null, "img", null, imageWithJustSrc);

verify(handler, only()).startElement(isNull(), eq("img"), isNull(),
attributesCaptor.capture());
List<Attributes> values = attributesCaptor.getAllValues();
assertEquals("https://static.host.com/content/dam/flower.jpg", values.get(0).getValue(0));
String expectedScheme = scheme == null ? inputUrl.substring(0, inputUrl.indexOf("://")) : scheme;
assertEquals(expectedScheme + "://static.host.com/content/dam/flower.jpg", values.get(values.size() - 1).getValue(0));
}

@Test
Expand Down

0 comments on commit 4f4f18c

Please sign in to comment.