Skip to content

Commit

Permalink
Added support for keeping trailing slashes in FkRegex.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Reiche committed Jun 24, 2019
1 parent 49a2355 commit 5a0fcad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/org/takes/facets/fork/FkRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public final class FkRegex implements Fork {
*/
private final Scalar<TkRegex> target;

/**
* Remove trailing slashes is optional
*/
private boolean removeTrailingSlash = true;

/**
* Ctor.
* @param ptn Pattern
Expand Down Expand Up @@ -195,10 +200,20 @@ public FkRegex(final Pattern ptn, final Scalar<TkRegex> tke) {
this.target = tke;
}

/**
* Allows disabling the standard way for handling trailing slashes.
* @param removeTrailingSlash
* @return
*/
public FkRegex setRemoveTrailingSlash(boolean removeTrailingSlash) {
this.removeTrailingSlash = removeTrailingSlash;
return this;
}

@Override
public Opt<Response> route(final Request req) throws Exception {
String path = new RqHref.Base(req).href().path();
if (path.length() > 1 && path.charAt(path.length() - 1) == '/') {
if (removeTrailingSlash && path.length() > 1 && path.charAt(path.length() - 1) == '/') {
path = path.substring(0, path.length() - 1);
}
final Matcher matcher = this.pattern.matcher(path);
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/takes/facets/fork/FkRegexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,20 @@ public void removesTrailingSlash() throws Exception {
);
}

/**
* FkRegex can remove trailing slash from URI.
* @throws Exception If some problem inside
*/
@Test
public void keepsTrailingSlash() throws Exception {
MatcherAssert.assertThat(
new FkRegex("/h/tail/", new TkEmpty())
.setRemoveTrailingSlash(false)
.route(
new RqFake("POST", "/h/tail/")
).has(),
Matchers.is(true)
);
}

}

0 comments on commit 5a0fcad

Please sign in to comment.