responseEncodingAccepted
Description
Passes the request to the inner route if the request accepts the argument encoding. Otherwise, rejects the request with an UnacceptedResponseEncodingRejection(encoding)
.
Example
- Scala
-
source
val route = responseEncodingAccepted(gzip) { complete("content") } Get("/") ~> route ~> check { responseAs[String] shouldEqual "content" } Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check { rejection shouldEqual UnacceptedResponseEncodingRejection(gzip) }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.complete; import static org.apache.pekko.http.javadsl.server.Directives.responseEncodingAccepted; final Route route = responseEncodingAccepted(HttpEncodings.GZIP, () -> complete("content")); // tests: testRoute(route).run(HttpRequest.GET("/")).assertEntity("content"); runRouteUnSealed( route, HttpRequest.GET("/").addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE))) .assertRejections(Rejections.unacceptedResponseEncoding(HttpEncodings.GZIP));
1.0.1