我正在努力为协议传递一个 SOAP 信封。根据我的理解,PACT支持XML,所以我想尝试一下SOAP消息。我已经在 Java 中创建了一个示例更改学生 CXF 服务。以下是我的契约测试
public PactFragment createFragment(PactDslWithProvider builder( {
Map<String, String> headersXml = new HashMap<>();
headersXml.put("Content-Type","text/xml;charset=UTF-8" );
//headers.put("Content-Type", "application/json");
return builder
.given("welcomeTest")
.uponReceiving("a request to get the welcome test of a user")
.path("/web/ChangeStudent?wsdl")
//.query("wsdl")
.method("POST").body("<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.pactsoap.com/"><soapenv:Header/>"+
"<soapenv:Body><ser:changeName><arg0><name>Nandess</name></arg0></ser:changeName></soapenv:Body></soapenv:Envelope>")
.willRespondWith()
.headers(headersXml)
.status(200)
.body("<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:changeNameResponse xmlns:ns2="http://service.pactsoap.com/">".toLowerCase()+
"<return><name>HELLO myNamre</name></return></ns2:changeNameResponse></soap:Body></soap:Envelope>".toLowerCase())
.toFragment();
}
@Test
@PactVerification(value = "pactEmailsoap" , fragment = "createFragment")
public void runTest(){
String result = new EmailService(rule.getConfig().url()).ChangeStudentDetailsNew();
assertEquals(result.toLowerCase(),"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:changeNameResponse xmlns:ns2="http://service.pactsoap.com/">".toLowerCase()+
"<return><name>HELLO Nandess</name></return></ns2:changeNameResponse></soap:Body></soap:Envelope>".toLowerCase());
}
现在,我的服务正确返回了运行测试中的值,并且 Junit 成功了。我通过删除@PactVerification注释来验证这一点。但是我从应用程序/Json格式的协议模拟服务中得到了响应。我期待文本/xml格式的响应。 这就是我送交协议的形式。
知道为什么它向我返回 JSON 响应而不是 XML 响应。 以下是确切的错误日志:
[nioEventLoopGroup-3-1] DEBUG au.com.dius.pact.consumer.UnfilteredMockProvider - Generating response: status: 500
headers: [Access-Control-Allow-Origin:*, Content-Type:application/json, X-Pact-Unexpected-Request:1]
matchers: [:]
body: au.com.dius.pact.model.OptionalBody(PRESENT, { "error": "Unexpected request : tmethod: POSTntpath: /web/ChangeStudentntquery: [:]ntheaders: [SOAPAction:"", Connection:Keep-Alive, Content-Length:262, Content-Type:text/xml;charset=UTF-8, Accept-Encoding:gzip,deflate, User-Agent:Apache-HttpClient/4.1.1 (java 1.5), Host:localhost:22762]ntmatchers: [:]ntbody: au.com.dius.pact.model.OptionalBody(PRESENT, <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.pactsoap.com/"><SOAP-ENV:Header/><SOAP-ENV:Body><ser:changeName><arg0><name>myName</name></arg0></ser:changeName></SOAP-ENV:Body></SOAP-ENV:Envelope>)" })
可悲的是,事实并非如此;Pact 不支持 XML。您可能有一个简单的字符串匹配器,但是当您有一个不合适的东西(例如空格(时,这会导致一些问题。