I am working on passing a SOAP envelop for the pact. As per my understanding PACT supports XML so I wanted to give a try with SOAP message.I have created a sample change student CXF service in Java. Following is my pact test
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());
}
Now my Service correctly returns the values in RunTest and Junit is successful.I verified this by removing @PactVerification annotation.But I am getting the response from pact mocking service in application/Json format.Where as I was expecting the response in text/xml format. That is the format I am sending to pact.
Any idea why it is returning me the JSON response instead of XML response. Below is the exact error log:
[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: POST\n\tpath: \/web\/ChangeStudent\n\tquery: [:]\n\theaders: [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]\n\tmatchers: [:]\n\tbody: 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>)" })
Sadly, that's not the case; Pact does not support XML. You could potentially have a simple string matcher, but that's going to cause some issues when you have a single thing out of place, like a space.