Skip to content

Commit

Permalink
Add spec and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vikraman committed Feb 26, 2015
1 parent f9b19d5 commit 0ea34c7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

cache:
directories:
- $HOME/.ivy2/cache

language: scala

scala:
- 2.11.5
- 2.10.4
jdk:
- oraclejdk8
- oraclejdk7
- openjdk7

script:
- sbt clean coverage test

after_success:
- sbt coveralls
5 changes: 5 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resolvers += Classpaths.sbtPluginReleases

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.0.4")

addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.0.0.BETA1")
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package akka.http.marshallers.argonaut

import argonaut._, Argonaut._
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.http.model.{ ContentTypes, HttpEntity, HttpRequest }
import akka.http.model.HttpCharsets.`UTF-8`
import akka.http.model.MediaTypes.`application/json`
import akka.http.marshalling.ToResponseMarshallable
import akka.http.unmarshalling.FromEntityUnmarshaller

import org.scalatest.{ FunSpec, BeforeAndAfterAll, ShouldMatchers }
import org.scalatest.concurrent.ScalaFutures

case class Person(name: String, age: Int)

object Person {

implicit def PersonCodecJson: CodecJson[Person] =
casecodec2(Person.apply, Person.unapply)("name", "age")

val person = Person("Foo Bar", 42)
val goodJson = person.asJson.nospaces
val badJson = """{ "name" := "Foo Bar", "age" := 42 }"""

}

class ArgonautSupportSpec
extends FunSpec with BeforeAndAfterAll with ArgonautSupport
with ShouldMatchers with ScalaFutures {

val system = ActorSystem("akka-argonaut")
implicit val ec = system.dispatcher
implicit val mat = ActorFlowMaterializer()(system)

describe("when marshalling a http request") {

val marshallable: ToResponseMarshallable = Person.person

whenReady(marshallable(HttpRequest())) { request =>

val entity = request.entity

it("should have correct media-type") {
entity.contentType.mediaType shouldBe `application/json`
}

it("should have correct charset") {
entity.contentType.charset shouldBe `UTF-8`
}

it("should have correct body") {
whenReady(request.entity.dataBytes
.map(_.utf8String).runFold("")(_ + _)) {
_ shouldBe Person.goodJson
}
}
}
}

describe("when unmarshalling a string") {

val unmarshaller = implicitly[FromEntityUnmarshaller[Person]]

it("should successfully unmarshal a valid entity") {
val goodEntity = HttpEntity(ContentTypes.`application/json`, Person.goodJson)
whenReady(unmarshaller(goodEntity)) {
_ shouldBe Person.person
}
}

it("should fail to unmarshal an invalid entity") {
val badEntity = HttpEntity(ContentTypes.`application/json`, Person.badJson)
whenReady(unmarshaller(badEntity).failed) {
_ shouldBe a[IllegalArgumentException]
}
}
}

override protected def afterAll(): Unit = {
super.afterAll()
system.shutdown()
}
}

0 comments on commit 0ea34c7

Please sign in to comment.