简体   繁体   中英

Verifying an XML attribute using Jayway's rest-assured

I am trying to verify that xmlns:ns1="http://thomas-bayer.com/blz/" in the following XML using Jayway's rest-assured's XML support .

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ns1:getBankResponse xmlns:ns1="http://thomas-bayer.com/blz/">
      <ns1:details>
        <ns1:bezeichnung>ABK-Kreditbank</ns1:bezeichnung>
        <ns1:bic>ABKBDEB1XXX</ns1:bic>
        <ns1:ort>Berlin</ns1:ort>
        <ns1:plz>10789</ns1:plz>
      </ns1:details>
    </ns1:getBankResponse>
  </soapenv:Body>
</soapenv:Envelope>

As I understand it,

xmlns:ns1="http://thomas-bayer.com/blz/"

is an attribute of

<ns1:getBankResponse

The @ prefix should return an attribute matching the stem following the @ .

Assume that xmlString is equal to the above SOAP response XML, all of the following have returned null:

String nameSpace1 = given(xmlString).getString("Envelope.Body.getBankResponse.@xmlns:ns1");
String nameSpace2 = given(xmlString).getString("Envelope.Body.getBankResponse.@ns1");
String nameSpace3 = given(xmlString).getString("Envelope.Body.getBankResponse.@xmlns");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@xmlns:ns1");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@ns1");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@xmlns");

Any help would be greatly appreciated.

PS. I know that rest-assured is primarily concerned with RESTful APIs but it can be used to test SOAP.

In the upcoming version of REST Assured (probably 2.9.1) you will be able to configure XmlPath to be unaware of namespaces. So this means you can do like this:

XmlPath xmlPath = new XmlPath(xmlResponse).using(xmlPathConfig().namespaceAware(false));
assertThat(xmlPath.getString("soapenv:Envelope.soapenv:Body.ns1:getBankResponse.@xmlns:ns1"), equalTo("http://thomas-bayer.com/blz/"));

You can try this out today by depending on version 2.9.1-SNAPSHOT after having added the following Maven repository:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

Partially solved it this way:

 XmlPath xmlPath = new XmlPath(xmlResponse).using(xmlPathConfig().declaredNamespace("ns1", "http://thomas-bayer.com/blz/"));

// Then

 assertThat(xmlPath.getString("Envelope.Body.getBankResponse.ns1:details.ns1:bezeichnung.text()"), equalTo("ABK-Kreditbank"));

I say partially because while it does prove that name space ns1 is used and therefore must be defined to be valid it doesn't show that:

xmlns:ns1="http://thomas-bayer.com/blz/"

because the test still passes if I change the URI.

Anybody have a better answer?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM