2014-12-18 09:25:33 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.http.server
|
|
|
|
|
package directives
|
|
|
|
|
|
2015-04-24 18:03:07 +02:00
|
|
|
import akka.http.scaladsl.coding._
|
|
|
|
|
import akka.http.scaladsl.model.{ HttpResponse, StatusCodes }
|
|
|
|
|
import akka.http.scaladsl.model.headers.{ HttpEncodings, HttpEncoding, `Accept-Encoding`, `Content-Encoding` }
|
|
|
|
|
import akka.http.scaladsl.model.headers.HttpEncodings._
|
|
|
|
|
import akka.http.scaladsl.server._
|
2014-12-18 09:25:33 +01:00
|
|
|
import akka.util.ByteString
|
|
|
|
|
import org.scalatest.matchers.Matcher
|
|
|
|
|
|
|
|
|
|
class CodingDirectivesExamplesSpec extends RoutingSpec {
|
2014-12-22 15:18:19 +01:00
|
|
|
"encodeResponse" in {
|
|
|
|
|
val route = encodeResponse { complete("content") }
|
2014-12-18 09:25:33 +01:00
|
|
|
|
|
|
|
|
Get("/") ~> route ~> check {
|
2014-12-22 15:18:19 +01:00
|
|
|
response should haveContentEncoding(identity)
|
2014-12-18 09:25:33 +01:00
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(gzip, deflate) ~> route ~> check {
|
|
|
|
|
response should haveContentEncoding(gzip)
|
|
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check {
|
|
|
|
|
response should haveContentEncoding(deflate)
|
|
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(identity) ~> route ~> check {
|
|
|
|
|
response should haveContentEncoding(identity)
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
"encodeResponseWith" in {
|
|
|
|
|
val route = encodeResponseWith(Gzip) { complete("content") }
|
2014-12-18 09:25:33 +01:00
|
|
|
|
|
|
|
|
Get("/") ~> route ~> check {
|
|
|
|
|
response should haveContentEncoding(gzip)
|
|
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
Get("/") ~> `Accept-Encoding`() ~> route ~> check {
|
2014-12-18 09:25:33 +01:00
|
|
|
rejection shouldEqual UnacceptedResponseEncodingRejection(gzip)
|
|
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(gzip, deflate) ~> route ~> check {
|
|
|
|
|
response should haveContentEncoding(gzip)
|
|
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check {
|
|
|
|
|
rejection shouldEqual UnacceptedResponseEncodingRejection(gzip)
|
|
|
|
|
}
|
|
|
|
|
Get("/") ~> `Accept-Encoding`(identity) ~> route ~> check {
|
|
|
|
|
rejection shouldEqual UnacceptedResponseEncodingRejection(gzip)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val helloGzipped = compress("Hello", Gzip)
|
|
|
|
|
val helloDeflated = compress("Hello", Deflate)
|
|
|
|
|
"decodeRequest" in {
|
|
|
|
|
val route =
|
2014-12-22 15:18:19 +01:00
|
|
|
decodeRequest {
|
2014-12-18 09:25:33 +01:00
|
|
|
entity(as[String]) { content: String =>
|
|
|
|
|
complete(s"Request content: '$content'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
|
|
|
|
|
responseAs[String] shouldEqual "Request content: 'Hello'"
|
|
|
|
|
}
|
|
|
|
|
Post("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
|
2014-12-22 15:18:19 +01:00
|
|
|
responseAs[String] shouldEqual "Request content: 'Hello'"
|
2014-12-18 09:25:33 +01:00
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
Post("/", "hello uncompressed") ~> `Content-Encoding`(identity) ~> route ~> check {
|
|
|
|
|
responseAs[String] shouldEqual "Request content: 'hello uncompressed'"
|
2014-12-18 09:25:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
"decodeRequestWith-0" in {
|
2014-12-18 09:25:33 +01:00
|
|
|
val route =
|
2014-12-22 15:18:19 +01:00
|
|
|
decodeRequestWith(Gzip) {
|
2014-12-18 09:25:33 +01:00
|
|
|
entity(as[String]) { content: String =>
|
|
|
|
|
complete(s"Request content: '$content'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
|
|
|
|
|
responseAs[String] shouldEqual "Request content: 'Hello'"
|
|
|
|
|
}
|
|
|
|
|
Post("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
|
2014-12-22 15:18:19 +01:00
|
|
|
rejection shouldEqual UnsupportedRequestEncodingRejection(gzip)
|
2014-12-18 09:25:33 +01:00
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
Post("/", "hello") ~> `Content-Encoding`(identity) ~> route ~> check {
|
|
|
|
|
rejection shouldEqual UnsupportedRequestEncodingRejection(gzip)
|
2014-12-18 09:25:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-22 15:18:19 +01:00
|
|
|
"decodeRequestWith-1" in {
|
2014-12-18 09:25:33 +01:00
|
|
|
val route =
|
2014-12-22 15:18:19 +01:00
|
|
|
decodeRequestWith(Gzip, NoCoding) {
|
2014-12-18 09:25:33 +01:00
|
|
|
entity(as[String]) { content: String =>
|
|
|
|
|
complete(s"Request content: '$content'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
|
|
|
|
|
responseAs[String] shouldEqual "Request content: 'Hello'"
|
|
|
|
|
}
|
|
|
|
|
Post("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
|
|
|
|
|
rejections shouldEqual List(UnsupportedRequestEncodingRejection(gzip), UnsupportedRequestEncodingRejection(identity))
|
|
|
|
|
}
|
|
|
|
|
Post("/", "hello uncompressed") ~> `Content-Encoding`(identity) ~> route ~> check {
|
|
|
|
|
responseAs[String] shouldEqual "Request content: 'hello uncompressed'"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def haveContentEncoding(encoding: HttpEncoding): Matcher[HttpResponse] =
|
|
|
|
|
be(encoding) compose { (_: HttpResponse).header[`Content-Encoding`].map(_.encodings.head).getOrElse(HttpEncodings.identity) }
|
|
|
|
|
|
|
|
|
|
def compress(input: String, encoder: Encoder): ByteString = encoder.encode(ByteString(input))
|
|
|
|
|
}
|