Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH #636 Fix RewriteConfig ending with two consecutive placeholders #638

Merged
merged 3 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,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)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}