From 931e8f9b18e0c170cd968b35e4bf9e6f5df37593 Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 12 Nov 2014 14:33:10 +0100 Subject: [PATCH] !htk rename `RouteTest::entity` to `RouteTest::responseEntity`, smaller test cleanups Also upgrade to latest (un)marshalling changes. --- .../scala/akka/http/testkit/RouteTest.scala | 6 +- .../http/testkit/ScalatestRouteTestSpec.scala | 4 +- .../marshallers/xml/ScalaXmlSupportSpec.scala | 20 ++---- .../directives/CodingDirectivesSpec.scala | 65 +++++++------------ .../directives/ParameterDirectivesSpec.scala | 8 +-- 5 files changed, 38 insertions(+), 65 deletions(-) diff --git a/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala b/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala index 8a2fe0f385..9d95fd9be7 100644 --- a/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala +++ b/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala @@ -54,17 +54,17 @@ trait RouteTest extends RequestBuilding with RouteTestResultComponent with Marsh def handled: Boolean = result.handled def response: HttpResponse = result.response - def entity: HttpEntity = result.entity + def responseEntity: HttpEntity = result.entity def chunks: immutable.Seq[HttpEntity.ChunkStreamPart] = result.chunks def entityAs[T: FromEntityUnmarshaller: ClassTag](implicit timeout: Duration = 1.second): T = { def msg(e: Throwable) = s"Could not unmarshal entity to type '${implicitly[ClassTag[T]]}' for `entityAs` assertion: $e\n\nResponse was: $response" - Await.result(Unmarshal(entity).to[T].fast.recover[T] { case error ⇒ failTest(msg(error)) }, timeout) + Await.result(Unmarshal(responseEntity).to[T].fast.recover[T] { case error ⇒ failTest(msg(error)) }, timeout) } def responseAs[T: FromResponseUnmarshaller: ClassTag](implicit timeout: Duration = 1.second): T = { def msg(e: Throwable) = s"Could not unmarshal response to type '${implicitly[ClassTag[T]]}' for `responseAs` assertion: $e\n\nResponse was: $response" Await.result(Unmarshal(response).to[T].fast.recover[T] { case error ⇒ failTest(msg(error)) }, timeout) } - def contentType: ContentType = entity.contentType + def contentType: ContentType = responseEntity.contentType def mediaType: MediaType = contentType.mediaType def charset: HttpCharset = contentType.charset def definedCharset: Option[HttpCharset] = contentType.definedCharset diff --git a/akka-http-testkit/src/test/scala/akka/http/testkit/ScalatestRouteTestSpec.scala b/akka-http-testkit/src/test/scala/akka/http/testkit/ScalatestRouteTestSpec.scala index 35ea537134..3f8ea8d999 100644 --- a/akka-http-testkit/src/test/scala/akka/http/testkit/ScalatestRouteTestSpec.scala +++ b/akka-http-testkit/src/test/scala/akka/http/testkit/ScalatestRouteTestSpec.scala @@ -33,7 +33,7 @@ class ScalatestRouteTestSpec extends FreeSpec with Matchers with ScalatestRouteT } } ~> check { status shouldEqual OK - entity shouldEqual HttpEntity(ContentTypes.`text/plain(UTF-8)`, "abc") + responseEntity shouldEqual HttpEntity(ContentTypes.`text/plain(UTF-8)`, "abc") header("Fancy") shouldEqual Some(pinkHeader) } } @@ -69,7 +69,7 @@ class ScalatestRouteTestSpec extends FreeSpec with Matchers with ScalatestRouteT check { status shouldEqual OK - entity shouldEqual HttpEntity(ContentTypes.`text/plain(UTF-8)`, "abc") + responseEntity shouldEqual HttpEntity(ContentTypes.`text/plain(UTF-8)`, "abc") header("Fancy") shouldEqual Some(pinkHeader) }(result) } diff --git a/akka-http-tests/src/test/scala/akka/http/marshallers/xml/ScalaXmlSupportSpec.scala b/akka-http-tests/src/test/scala/akka/http/marshallers/xml/ScalaXmlSupportSpec.scala index 9d60849d47..27d02c4e9c 100644 --- a/akka-http-tests/src/test/scala/akka/http/marshallers/xml/ScalaXmlSupportSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/marshallers/xml/ScalaXmlSupportSpec.scala @@ -4,19 +4,13 @@ package akka.http.marshallers.xml -import akka.http.unmarshalling.Unmarshal - -import akka.http.testkit.ScalatestRouteTest -import akka.http.unmarshalling.UnmarshallingError.UnsupportedContentType -import org.scalatest.{ Matchers, WordSpec } - -import akka.http.model.HttpCharsets._ -import akka.http.model.MediaTypes._ -import akka.http.model.{ ContentTypeRange, ContentType, HttpEntity } - -import akka.http.marshalling.{ ToEntityMarshallers, Marshal } - import scala.xml.NodeSeq +import org.scalatest.{ Matchers, WordSpec } +import akka.http.testkit.ScalatestRouteTest +import akka.http.unmarshalling.{ Unmarshaller, Unmarshal } +import akka.http.model._ +import HttpCharsets._ +import MediaTypes._ class ScalaXmlSupportSpec extends WordSpec with Matchers with ScalatestRouteTest { import ScalaXmlSupport._ @@ -31,7 +25,7 @@ class ScalaXmlSupportSpec extends WordSpec with Matchers with ScalatestRouteTest } "nodeSeqUnmarshaller should reject `application/octet-stream`" in { Unmarshal(HttpEntity(`application/octet-stream`, "Hällö")).to[NodeSeq].map(_.text) should - haveFailedWith(UnsupportedContentType(ScalaXmlSupport.nodeSeqMediaTypes map (ContentTypeRange(_)))) + haveFailedWith(Unmarshaller.UnsupportedContentTypeException(nodeSeqContentTypeRanges: _*)) } } } diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/CodingDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/CodingDirectivesSpec.scala index 234d14fa41..6ce7d6e952 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/CodingDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/CodingDirectivesSpec.scala @@ -5,24 +5,19 @@ package akka.http.server package directives -import org.scalatest.Tag import org.scalatest.matchers.Matcher - import akka.util.ByteString import akka.stream.scaladsl.Source - import akka.http.util._ - import akka.http.model._ -import HttpEntity.{ ChunkStreamPart, Chunk } +import akka.http.coding._ import headers._ +import HttpEntity.{ ChunkStreamPart, Chunk } import HttpCharsets._ import HttpEncodings._ import MediaTypes._ import StatusCodes._ -import akka.http.coding._ - class CodingDirectivesSpec extends RoutingSpec { val echoRequestContent: Route = { ctx ⇒ ctx.complete(ctx.request.entity.dataBytes.utf8String) } @@ -93,7 +88,7 @@ class CodingDirectivesSpec extends RoutingSpec { } "a (decodeRequest(Gzip) | decodeRequest(NoEncoding)) compound directive" should { - lazy val decodeWithGzipOrNoEncoding = (decodeRequest(Gzip) | decodeRequest(NoCoding)) + lazy val decodeWithGzipOrNoEncoding = decodeRequest(Gzip) | decodeRequest(NoCoding) "decode the request content if it has encoding 'gzip'" in { Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> { decodeWithGzipOrNoEncoding { echoRequestContent } @@ -124,13 +119,13 @@ class CodingDirectivesSpec extends RoutingSpec { encodeResponse(Gzip) { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "encode the response content with GZIP if the request has no Accept-Encoding header" in { Post() ~> { encodeResponse(Gzip) { yeah } - } ~> check { entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } + } ~> check { responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "reject the request if the client does not accept GZIP encoding" in { Post() ~> `Accept-Encoding`(identity) ~> { @@ -202,13 +197,13 @@ class CodingDirectivesSpec extends RoutingSpec { } "a (encodeResponse(Gzip) | encodeResponse(NoEncoding)) compound directive" should { - lazy val encodeGzipOrIdentity = (encodeResponse(Gzip) | encodeResponse(NoCoding)) + lazy val encodeGzipOrIdentity = encodeResponse(Gzip) | encodeResponse(NoCoding) "produce a GZIP encoded response if the request has no Accept-Encoding header" in { Post() ~> { encodeGzipOrIdentity { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a GZIP encoded response if the request has an `Accept-Encoding: deflate, gzip` header" in { @@ -216,7 +211,7 @@ class CodingDirectivesSpec extends RoutingSpec { encodeGzipOrIdentity { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a non-encoded response if the request has an `Accept-Encoding: identity` header" in { @@ -261,7 +256,7 @@ class CodingDirectivesSpec extends RoutingSpec { encodeIdentityOrGzip { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "reject the request if it has an `Accept-Encoding: deflate` header" in { @@ -275,14 +270,13 @@ class CodingDirectivesSpec extends RoutingSpec { } } - //# compressResponse-example "the compressResponse directive" should { "produce a GZIP compressed response if the request has no Accept-Encoding header" in { Post("/") ~> { compressResponse() { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a GZIP compressed response if the request has an `Accept-Encoding: gzip, deflate` header" in { @@ -290,7 +284,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponse() { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a Deflate compressed response if the request has an `Accept-Encoding: deflate` header" in { @@ -298,7 +292,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponse() { yeah } } ~> check { response should haveContentEncoding(deflate) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) } } "produce an uncompressed response if the request has an `Accept-Encoding: identity` header" in { @@ -310,16 +304,14 @@ class CodingDirectivesSpec extends RoutingSpec { } } } - //# - //# compressResponseIfRequested-example "the compressResponseIfRequested directive" should { "produce an uncompressed response if the request has no Accept-Encoding header" in { Post("/") ~> { compressResponseIfRequested() { yeah } } ~> check { response should haveNoContentEncoding - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), "Yeah!") + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), "Yeah!") } } "produce a GZIP compressed response if the request has an `Accept-Encoding: deflate, gzip` header" in { @@ -327,7 +319,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponseIfRequested() { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a Deflate encoded response if the request has an `Accept-Encoding: deflate` header" in { @@ -335,7 +327,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponseIfRequested() { yeah } } ~> check { response should haveContentEncoding(deflate) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) } } "produce an uncompressed response if the request has an `Accept-Encoding: identity` header" in { @@ -347,16 +339,14 @@ class CodingDirectivesSpec extends RoutingSpec { } } } - //# - //# compressResponseWith-example "the compressResponseWith directive" should { "produce a response compressed with the specified Encoder if the request has a matching Accept-Encoding header" in { Post("/") ~> `Accept-Encoding`(gzip) ~> { compressResponse(Gzip) { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "produce a response compressed with one of the specified Encoders if the request has a matching Accept-Encoding header" in { @@ -364,7 +354,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponse(Gzip, Deflate) { yeah } } ~> check { response should haveContentEncoding(deflate) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahDeflated) } } "produce a response compressed with the first of the specified Encoders if the request has no Accept-Encoding header" in { @@ -372,7 +362,7 @@ class CodingDirectivesSpec extends RoutingSpec { compressResponse(Gzip, Deflate) { yeah } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), yeahGzipped) } } "reject the request if it has an Accept-Encoding header with an encoding that doesn't match" in { @@ -383,9 +373,7 @@ class CodingDirectivesSpec extends RoutingSpec { } } } - //# - //# decompressRequest-example "the decompressRequest directive" should { "decompress the request content if it has a `Content-Encoding: gzip` header and the content is gzip encoded" in { Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> { @@ -413,9 +401,7 @@ class CodingDirectivesSpec extends RoutingSpec { } } } - //# - //# decompressRequestWith-example "the decompressRequestWith directive" should { "decompress the request content if its `Content-Encoding` header matches the specified encoder" in { Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> { @@ -435,9 +421,7 @@ class CodingDirectivesSpec extends RoutingSpec { } } } - //# - //# decompress-compress-combination-example "the (decompressRequest & compressResponse) compound directive" should { lazy val decompressCompress = (decompressRequest() & compressResponse()) "decompress a GZIP compressed request and produce a GZIP compressed response if the request has no Accept-Encoding header" in { @@ -445,7 +429,7 @@ class CodingDirectivesSpec extends RoutingSpec { decompressCompress { echoRequestContent } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) } } "decompress a GZIP compressed request and produce a Deflate compressed response if the request has an `Accept-Encoding: deflate` header" in { @@ -453,7 +437,7 @@ class CodingDirectivesSpec extends RoutingSpec { decompressCompress { echoRequestContent } } ~> check { response should haveContentEncoding(deflate) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloDeflated) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloDeflated) } } "decompress an uncompressed request and produce a GZIP compressed response if the request has an `Accept-Encoding: gzip` header" in { @@ -461,14 +445,13 @@ class CodingDirectivesSpec extends RoutingSpec { decompressCompress { echoRequestContent } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) } } } - //# "the (decompressRequest & compressResponseIfRequested) compound directive" should { - lazy val decompressCompressIfRequested = (decompressRequest() & compressResponseIfRequested()) + lazy val decompressCompressIfRequested = decompressRequest() & compressResponseIfRequested() "decode a GZIP encoded request and produce a non-encoded response if the request has no Accept-Encoding header" in { Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> { decompressCompressIfRequested { echoRequestContent } @@ -481,7 +464,7 @@ class CodingDirectivesSpec extends RoutingSpec { decompressCompressIfRequested { echoRequestContent } } ~> check { response should haveContentEncoding(deflate) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloDeflated) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloDeflated) } } "decode a non-encoded request and produce a GZIP encoded response if the request has an `Accept-Encoding: gzip` header" in { @@ -489,7 +472,7 @@ class CodingDirectivesSpec extends RoutingSpec { decompressCompressIfRequested { echoRequestContent } } ~> check { response should haveContentEncoding(gzip) - entity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) + responseEntity shouldEqual HttpEntity(ContentType(`text/plain`, `UTF-8`), helloGzipped) } } } diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/ParameterDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/ParameterDirectivesSpec.scala index 1e70bac789..670c3bc027 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/ParameterDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/ParameterDirectivesSpec.scala @@ -5,7 +5,6 @@ package akka.http.server package directives -import akka.http.unmarshalling.{ FromStringOptionUnmarshaller, UnmarshallingError } import org.scalatest.{ FreeSpec, Inside } import akka.http.unmarshalling.Unmarshaller.HexInt @@ -22,8 +21,7 @@ class ParameterDirectivesSpec extends FreeSpec with GenericRoutingSpec with Insi parameter('amount.as[Int]) { echoComplete } } ~> check { inside(rejection) { - case MalformedQueryParamRejection("amount", "'1x3' is not a valid 32-bit signed integer value", - Some(UnmarshallingError.InvalidContent(_, Some(_: NumberFormatException)))) ⇒ + case MalformedQueryParamRejection("amount", "'1x3' is not a valid 32-bit signed integer value", Some(_)) ⇒ } } } @@ -117,8 +115,7 @@ class ParameterDirectivesSpec extends FreeSpec with GenericRoutingSpec with Insi parameter('really.as[Boolean]) { echoComplete } } ~> check { inside(rejection) { - case MalformedQueryParamRejection("really", "'absolutely' is not a valid Boolean value", - Some(UnmarshallingError.InvalidContent(_, None))) ⇒ + case MalformedQueryParamRejection("really", "'absolutely' is not a valid Boolean value", None) ⇒ } } } @@ -162,7 +159,6 @@ class ParameterDirectivesSpec extends FreeSpec with GenericRoutingSpec with Insi "The 'parameter' requirement directive should" - { "block requests that do not contain the required parameter" in { Get("/person?age=19") ~> { - implicitly[FromStringOptionUnmarshaller[String]] parameter('nose ! "large") { completeOk } } ~> check { handled shouldEqual false } }