Created
January 2, 2014 05:42
-
-
Save XuefengWu/8215497 to your computer and use it in GitHub Desktop.
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
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