-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
84 changes: 84 additions & 0 deletions
84
src/test/scala/akka/http/marshallers/argonaut/ArgonautSupportSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |