Created
January 1, 2014 04:32
-
-
Save XuefengWu/8205097 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 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