pathSuffixTest

Signature

def pathSuffixTest[L](pm: PathMatcher[L]): Directive[L] 

Description

Checks whether the unmatched path of the RequestContextRequestContext has a suffix matched by the given PathMatcher. Potentially extracts one or more values (depending on the type of the argument) but doesn’t consume its match from the unmatched path.

This directive is very similar to the pathSuffix directive with the one difference that the path suffix it matched (if it matched) is not consumed. The unmatched path of the RequestContextRequestContext is therefore left as is even in the case that the directive successfully matched and the request is passed on to its inner route.

As opposed to pathPrefixTest this directive matches and consumes the unmatched path from the right, i.e. the end.

Caution

For efficiency reasons, the given PathMatcher must match the desired suffix in reversed-segment order, i.e. pathSuffixTest("baz" / "bar") would match /foo/bar/baz! The order within a segment match is not reversed.

Depending on the type of its PathMatcher argument the pathSuffixTest directive extracts zero or more values from the URI. If the match fails the request is rejected with an empty rejection set.

Example

Scala
sourceval completeWithUnmatchedPath =
  extractUnmatchedPath { p =>
    complete(p.toString)
  }

val route =
  concat(
    pathSuffixTest(Slash) {
      complete("slashed")
    },
    complete("unslashed"))

// tests:
Get("/foo/") ~> route ~> check {
  responseAs[String] shouldEqual "slashed"
}
Get("/foo") ~> route ~> check {
  responseAs[String] shouldEqual "unslashed"
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.complete;
import static org.apache.pekko.http.javadsl.server.Directives.pathSuffixTest;
final Route route =
    concat(pathSuffixTest(slash(), () -> complete("slashed")), complete("unslashed"));
// tests:
testRoute(route).run(HttpRequest.GET("/foo/")).assertEntity("slashed");
testRoute(route).run(HttpRequest.GET("/foo")).assertEntity("unslashed");