在Scala中存根SOAP请求

时间:2015-03-10 14:26:34

标签: scala soap netty scalaxb betamax

我使用scalaxb生成SOAP接口的模型和客户端部分。对于测试,我使用Betamax,也可以在Scala中使用。但是,scalaxb使用Netty作为传输,ignores proxy settings由Betamax建立。你会如何应对这种情况?

scalaxb使用蛋糕模式,因此该服务由以下example中的3个部分构建:

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._

val service = (new stockquote.StockQuoteSoap12Bindings with
  scalaxb.SoapClientsAsync with
  scalaxb.DispatchHttpClientsAsync {}).service
val fresponse = service.getQuote(Some("GOOG"))
val response = Await.result(fresponse, 5 seconds)
println(response)

测试:

import co.freeside.betamax.{TapeMode, Recorder}
import co.freeside.betamax.proxy.jetty.ProxyServer
import dispatch._
import org.scalatest.{Tag, FunSuite}
import scala.concurrent.duration._

import scala.concurrent.{Await, Future}

class StockquoteSpec extends FunSuite with Betamax {

  testWithBetamax("stockquote", Some(TapeMode.READ_WRITE))("stockquote") { 
    val fresponse = service.getQuote(Some("GOOG"))
    val response = Await.result(fresponse, 5 seconds)
    println(response)
  }
}

trait Betamax {

  protected def test(testName: String, testTags: Tag*)(testFun: => Unit)

  def testWithBetamax(tape: String, mode: Option[TapeMode] = None)(testName: String, testTags: Tag*)(testFun: => Unit) = {
    test(testName, testTags: _*) {
      val recorder = new Recorder
      val proxyServer = new ProxyServer(recorder)
      recorder.insertTape(tape)
      recorder.getTape.setMode(mode.getOrElse(recorder.getDefaultMode()))
      proxyServer.start()
      try {
        testFun
      } finally {
        recorder.ejectTape()
        proxyServer.stop()
      }
    }
  }
}

版本:

  • net.databinder.dispatch 0.11.2
  • co.freeside.betamax 1.1.2
  • com.ning.async-http-client 1.8.10
  • io.netty.netty 3.9.2.Final

1 个答案:

答案 0 :(得分:1)

确实可以将代理与Netty一起使用。虽然Netty不读取代理设置的系统属性,但可以使用ProxyServerSelector注入设置。它是使用build的{​​{1}}方法创建的:

AsyncHttpClientConfig

唯一的障碍是if (proxyServerSelector == null && useProxySelector) { proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector(); } if (proxyServerSelector == null && useProxyProperties) { proxyServerSelector = ProxyUtils.createProxyServerSelector(System.getProperties()); } if (proxyServerSelector == null) { proxyServerSelector = ProxyServerSelector.NO_PROXY_SELECTOR; } 使用默认配置scalaxb。您可以使用创建服务时可以使用的自定义useProxyProperties=false覆盖它:

MyDispatchHttpClientsAsync

val service = (new stockquote.StockQuoteSoap12Bindings with scalaxb.SoapClientsAsync with MyDispatchHttpClientsAsync {}).service 的源代码(关键点是调用MyDispatchHttpClientsAsync):

setUseProxyProperties(true)