From dbc08fae9f4df26833caa748d6d4710e91cbb161 Mon Sep 17 00:00:00 2001 From: Chris Gresty Date: Mon, 2 Mar 2020 10:14:24 +0000 Subject: [PATCH 1/3] Fix rewrite config ending with two consecutive substitutions --- .../api/extension/service/RewriteConfig.java | 6 ++- .../extension/service/RewriteConfigTest.java | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 components/api/src/test/java/com/hotels/styx/api/extension/service/RewriteConfigTest.java diff --git a/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java b/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java index 0abc7cc288..9092998ef7 100644 --- a/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java +++ b/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.regex.MatchResult; @@ -120,8 +121,11 @@ public Replacement(String replacement) { private String substitute(MatchResult matcher) { StringBuilder rewrittenUrl = new StringBuilder(); + // There may not be enough literals to fully interleave with placeholders. + // This is just how String.split(REGEX) works. + // Any remaining literals are assumed to be empty strings. for (int i = 0; i < placeholderNumbers.size(); i++) { - if (!literals.isEmpty()) { + if (literals.size() > i) { rewrittenUrl.append(literals.get(i)); } rewrittenUrl.append(matcher.group(placeholderNumbers.get(i))); diff --git a/components/api/src/test/java/com/hotels/styx/api/extension/service/RewriteConfigTest.java b/components/api/src/test/java/com/hotels/styx/api/extension/service/RewriteConfigTest.java new file mode 100644 index 0000000000..1e8e5608de --- /dev/null +++ b/components/api/src/test/java/com/hotels/styx/api/extension/service/RewriteConfigTest.java @@ -0,0 +1,37 @@ +package com.hotels.styx.api.extension.service; + +import org.hamcrest.CoreMatchers; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Optional; +import java.util.regex.Pattern; + +public class RewriteConfigTest { + + @Test + public void testSubstitutions() { + String urlPattern = "\\/foo\\/(a|b|c)(\\/.*)?"; + + RewriteConfig config = new RewriteConfig(urlPattern, "/bar/$1$2"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("/bar/b/something")); + + config = new RewriteConfig(urlPattern, "/bar/$1/x$2"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("/bar/b/x/something")); + + config = new RewriteConfig(urlPattern, "/bar/$1/x$2/y"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("/bar/b/x/something/y")); + + config = new RewriteConfig(urlPattern, "$1/x$2/y"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("b/x/something/y")); + + config = new RewriteConfig(urlPattern, "$1$2/y"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("b/something/y")); + + config = new RewriteConfig(urlPattern, "$1$2"); + assertThat(config.rewrite("/foo/b/something").get(), CoreMatchers.equalTo("b/something")); + } +} From 5c47c69fc9b4178a9f38bb8b7b7b4cb8d0e5dfa4 Mon Sep 17 00:00:00 2001 From: Chris Gresty Date: Mon, 2 Mar 2020 10:20:03 +0000 Subject: [PATCH 2/3] Checkstyle --- .../com/hotels/styx/api/extension/service/RewriteConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java b/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java index 9092998ef7..d10d9a51c5 100644 --- a/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java +++ b/components/api/src/main/java/com/hotels/styx/api/extension/service/RewriteConfig.java @@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableList; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.regex.MatchResult; From 305b48a6927039e0fed48cb86d2175dca0d1646f Mon Sep 17 00:00:00 2001 From: Chris Gresty Date: Mon, 2 Mar 2020 11:00:17 +0000 Subject: [PATCH 3/3] Rebuild