From adf9170b8974324a88e721557b0ef0611f6ef341 Mon Sep 17 00:00:00 2001 From: Age Mooij Date: Fri, 19 Jul 2013 01:37:07 +0200 Subject: [PATCH] ! httpx: move unmarshal and unmarshalUnsafe to Unmarshaller and add unmarshaller method This fixes #373 by moving the `unmarshal` and `unmarshalUnsafe` methods from the spray.httpx package object to the Unmarshaller object. It also renames `unmarshal` to `unmarshaller` and adds an improved version of `unmarshal`that takes an `HttpEntity` as a parameter, making it consistent with `unmarshalUnsafe` Added the necessary unit tests to `BasicUnmarshallers`. --- .../httpx/unmarshalling/Unmarshaller.scala | 8 +++++ .../spray/httpx/unmarshalling/package.scala | 6 ---- .../BasicUnmarshallersSpec.scala | 31 ++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Unmarshaller.scala b/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Unmarshaller.scala index 51aedf9bb2..640ddcb524 100644 --- a/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Unmarshaller.scala +++ b/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Unmarshaller.scala @@ -39,4 +39,12 @@ object Unmarshaller { new Unmarshaller[T] { def apply(entity: HttpEntity) = if (entity.isEmpty) Left(ContentExpected) else um(entity) } + + def unmarshaller[T](implicit um: Unmarshaller[T]) = um + + def unmarshal[T: Unmarshaller](entity: HttpEntity): Deserialized[T] = unmarshaller.apply(entity) + def unmarshalUnsafe[T: Unmarshaller](entity: HttpEntity): T = unmarshaller.apply(entity) match { + case Right(value) ⇒ value + case Left(error) ⇒ sys.error(error.toString) + } } diff --git a/spray-httpx/src/main/scala/spray/httpx/unmarshalling/package.scala b/spray-httpx/src/main/scala/spray/httpx/unmarshalling/package.scala index e5ce62da46..6e0d19d756 100644 --- a/spray-httpx/src/main/scala/spray/httpx/unmarshalling/package.scala +++ b/spray-httpx/src/main/scala/spray/httpx/unmarshalling/package.scala @@ -26,12 +26,6 @@ package object unmarshalling { type Unmarshaller[T] = Deserializer[HttpEntity, T] type FromEntityOptionUnmarshaller[T] = Deserializer[Option[HttpEntity], T] - def unmarshal[T](implicit um: Unmarshaller[T]) = um - def unmarshalUnsafe[T: Unmarshaller](entity: HttpEntity): T = unmarshal.apply(entity) match { - case Right(value) ⇒ value - case Left(error) ⇒ sys.error(error.toString) - } - implicit def formFieldExtractor(form: HttpForm) = FormFieldExtractor(form) implicit def pimpHttpEntity(entity: HttpEntity) = new PimpedHttpEntity(entity) implicit def pimpHttpBodyPart(bodyPart: BodyPart) = new PimpedHttpEntity(bodyPart.entity) diff --git a/spray-httpx/src/test/scala/spray/httpx/unmarshalling/BasicUnmarshallersSpec.scala b/spray-httpx/src/test/scala/spray/httpx/unmarshalling/BasicUnmarshallersSpec.scala index 0cc56e5446..501f1a03ce 100644 --- a/spray-httpx/src/test/scala/spray/httpx/unmarshalling/BasicUnmarshallersSpec.scala +++ b/spray-httpx/src/test/scala/spray/httpx/unmarshalling/BasicUnmarshallersSpec.scala @@ -67,4 +67,33 @@ class BasicUnmarshallersSpec extends Specification { EmptyEntity.as[String] === Left(ContentExpected) } } -} \ No newline at end of file + + "Unmarshaller.unmarshaller" should { + "produce the correct unmarshaller" in { + val unmarshaller = Unmarshaller.unmarshaller[String] + + unmarshaller(HttpEntity("Hällö")) === Right("Hällö") + } + } + + "Unmarshaller.unmarshal" should { + "succeed when unmarshalling valid entities" in { + Unmarshaller.unmarshal[String](HttpEntity("Hällö")) === Right("Hällö") + } + + "fail when unmarshalling invalid entities" in { + val Left(UnsupportedContentType(msg)) = Unmarshaller.unmarshal[FormData](HttpEntity("Hällö")) + msg === "Expected 'application/x-www-form-urlencoded'" + } + } + + "Unmarshaller.unmarshalUnsafe" should { + "correctly unmarshal valid content" in { + Unmarshaller.unmarshalUnsafe[String](HttpEntity("Hällö")) === "Hällö" + } + + "throw an exception for invalid entities" in { + Unmarshaller.unmarshalUnsafe[NodeSeq](HttpEntity("Hällö")) must throwA[RuntimeException] + } + } +}