Skip to content

Instantly share code, notes, and snippets.

@XuefengWu
Created January 1, 2014 04:32
Show Gist options
  • Save XuefengWu/8205097 to your computer and use it in GitHub Desktop.
Save XuefengWu/8205097 to your computer and use it in GitHub Desktop.
package myaccount.rest
import com.dius.pact.model.{Request, Pact}
import com.dius.pact.runner.PactFileSource
import org.specs2.mutable.Specification
import play.api.test.WithServer
import play.api.libs.ws.WS
import play.api.test.Helpers._
import scala.concurrent.Await
import scala.concurrent.duration._
import org.specs2.specification.{FormattingFragments => FF}
class RegisterPactSpec extends Specification {
val basePath = "test/resources"
val testJson = s"$basePath/pacts"
def loadPacts(dir:String):Seq[Pact] = PactFileSource.loadFiles(dir)
def fullUrl(path: String) = WS.url("http://localhost:19001" + path)
def fullUrlJson(path: String) = fullUrl(path).withHeaders(CONTENT_TYPE -> "application/json")
def chooseRequest(path: String,input: String, method: String) = method.toLowerCase() match {
case "get" => fullUrl(path).get()
case "post" => fullUrlJson(path).post(input)
case "put" => fullUrlJson(path).put(input)
}
private val pacts: Seq[Pact] = loadPacts(testJson)
pacts.map { j =>
addFragments(j.provider.name + " should")
j.interactions.map{ i =>
i.description >> new WithServer {
val result = {
val request: Request = i.request
Await.result(chooseRequest(request.path,request.body.getOrElse(""),request.method.toString()), Duration(3, SECONDS))
}
result.status === i.response.status
}
}
}
addFragments(FF.p)
pacts.map { j =>
addFragments(j.provider.name + " should")
j.interactions.map{ i =>
i.description >> new WithServer {
val result = {
val request: Request = i.request
Await.result(chooseRequest(request.path,request.body.getOrElse(""),request.method.toString()), Duration(3, SECONDS))
}
result.status === i.response.status
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment