0

我正在为该协议传递一个 SOAP 信封。根据我的理解,PACT 支持 XML,所以我想尝试使用 SOAP 消息。我用 Java 创建了一个示例 change student CXF 服务。以下是我的契约测试

公共 PactFragment createFragment(PactDslWithProvider 构建器){

        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());
                     }

现在我的服务正确返回 RunTest 中的值并且 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: 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>)" })
4

1 回答 1

1

可悲的是,事实并非如此。Pact 不支持 XML。你可能有一个简单的字符串匹配器,但是当你有一个不合适的地方时,这会导致一些问题,比如一个空格。

于 2017-07-26T00:05:18.033 回答