Skip to content

Instantly share code, notes, and snippets.

@XuefengWu
Created January 2, 2014 05:42
Show Gist options
  • Save XuefengWu/8215497 to your computer and use it in GitHub Desktop.
Save XuefengWu/8215497 to your computer and use it in GitHub Desktop.
package com.dius.pact.runner
import spray.routing.SimpleRoutingApp
import akka.actor.ActorSystem
import com.dius.pact.model.{Interaction, Pact}
object PactMockService extends App with SimpleRoutingApp {
implicit val system = ActorSystem("pack-mock-system")
val basePath = "src/test/resources"
val testJson = s"$basePath/pacts"
def loadPacts(dir:String):Seq[Pact] = PactFileSource.loadFiles(dir)
val pacts = loadPacts(testJson)
startServer(interface = "localhost", port = 8080) {
path(Segments) { segs =>
val path = segs.mkString("/","/","")
val interactions: Seq[Interaction] = pacts.head.interactions.filter(_.request.path == path)
get {
val body: String = interactions.head.response.body.getOrElse("{}")
complete(body)
} ~
post {
entity(as[Option[String]]) { requestBody =>
val interaction = interactions.filter(_.request.body == requestBody).head
val body: String = interaction.response.body.getOrElse("{}")
complete(body)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment