如何在测试 Web 服务客户端时使用特定端口
How to use a specific port while Testing web service clients
我正在按照 play framework 2.5 文档使用 Web 服务客户端编写测试
https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
下面的代码是从上面的 link 中提取出来的。但是,如文档中所述,使用 implicit 端口时会分配一个随机可用端口。
是否可以提供特定端口而不是随机端口?
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
Server.withRouter() {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
如果您尝试进入 withRouter
的定义,您会看到需要 ServerConfig
,您可以在其中提供 port
和 run mode
。
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
//here 8888 is the port which you have defined.
Server.withRouter(ServerConfig(port = Some(8888), mode = Mode.Test)) {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
//The port will 8888 in this block
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
希望对您有所帮助,编码愉快:)
将此添加到 build.sbt 有效
PlayKeys.devSettings := Seq("play.server.http.port" -> "9100")
我正在按照 play framework 2.5 文档使用 Web 服务客户端编写测试
https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
下面的代码是从上面的 link 中提取出来的。但是,如文档中所述,使用 implicit 端口时会分配一个随机可用端口。
是否可以提供特定端口而不是随机端口?
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
Server.withRouter() {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
如果您尝试进入 withRouter
的定义,您会看到需要 ServerConfig
,您可以在其中提供 port
和 run mode
。
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
//here 8888 is the port which you have defined.
Server.withRouter(ServerConfig(port = Some(8888), mode = Mode.Test)) {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
//The port will 8888 in this block
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
希望对您有所帮助,编码愉快:)
将此添加到 build.sbt 有效
PlayKeys.devSettings := Seq("play.server.http.port" -> "9100")