Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
! httpx: move unmarshal and unmarshalUnsafe to Unmarshaller and add u…
Browse files Browse the repository at this point in the history
…nmarshaller 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`.
  • Loading branch information
agemooij committed Jul 21, 2013
1 parent 7222a6f commit adf9170
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Expand Up @@ -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)
}
}
Expand Up @@ -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)
Expand Down
Expand Up @@ -67,4 +67,33 @@ class BasicUnmarshallersSpec extends Specification {
EmptyEntity.as[String] === Left(ContentExpected)
}
}
}

"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]
}
}
}

0 comments on commit adf9170

Please sign in to comment.