encodeResponseWith

encodeResponseWith

§Signature

§Description

Encodes the response with the encoding that is requested by the client via the Accept-Encoding if it is among the provided encoders or rejects the request with an UnacceptedResponseEncodingRejection(supportedEncodings).

The response encoding is determined by the rules specified in RFC7231.

If the Accept-Encoding header is missing then the response is encoded using the first encoder.

If the Accept-Encoding header is empty and NoCoding is part of the encoders then no response encoding is used. Otherwise the request is rejected.

§Example

  1. val route = encodeResponseWith(Gzip) { complete("content") }
  2.  
  3. // tests:
  4. Get("/") ~> route ~> check {
  5. response should haveContentEncoding(gzip)
  6. }
  7. Get("/") ~> `Accept-Encoding`(gzip, deflate) ~> route ~> check {
  8. response should haveContentEncoding(gzip)
  9. }
  10. Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check {
  11. rejection shouldEqual UnacceptedResponseEncodingRejection(gzip)
  12. }
  13. Get("/") ~> `Accept-Encoding`(identity) ~> route ~> check {
  14. rejection shouldEqual UnacceptedResponseEncodingRejection(gzip)
  15. }