djangid / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

Add support for XML namespaces #39

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
E.g. 

expect().body("rss.channel.dc:creator", equalTo(""))...

Original issue reported on code.google.com by johan.ha...@gmail.com on 3 Mar 2011 at 7:16

GoogleCodeExporter commented 8 years ago
In addition,  this doesn't work. Assuming that namespaceBindings includes a 
binding for "p", this

  expect().body("/p:foo", namespaceBindings)

should work, but it doesn't. At least if 'foo' is in the default namespace in 
the document it doesn't work.

Original comment by normanwalsh on 14 Nov 2013 at 11:49

GoogleCodeExporter commented 8 years ago
Also, this is a valid expression, with the right bindings:

hasXPath("node-name(/*) = xs:QName('db:package-database')", packageNamespaces)

Original comment by normanwalsh on 15 Nov 2013 at 12:04

GoogleCodeExporter commented 8 years ago
The XPath implementation is provided by Hamcrest, perhaps you could try 
updating the Hamcrest lib and see if that works any better?

Original comment by johan.ha...@gmail.com on 15 Nov 2013 at 11:05

GoogleCodeExporter commented 8 years ago
No change. But I'll see if I can report it back to the Hamcrest folks.

Original comment by normanwalsh on 15 Nov 2013 at 6:11

GoogleCodeExporter commented 8 years ago
Hmmm. In the  Hamcrest unit tests, this works:

        assertThat(nsxml, hasXPath("/test:root", bindings));

In rest-assured, this goes bang:

                body(hasXPath("/db:package-database", packageNamespaces)).

java.lang.AssertionError: 1 expectation failed.
Expected: an XML document with XPath /db:package-database 
  Actual: <?xml version="1.0" encoding="UTF-8"?>
<package-database xmlns="http://marklogic.com/manage/package/databases">
  <metadata xmlns:db="http://marklogic.com/manage/package/databases">
    <package-version>2.0</package-version>

I wonder if there's something different about the way you setup the context?

Original comment by normanwalsh on 15 Nov 2013 at 6:24

GoogleCodeExporter commented 8 years ago
I've tried implementation support for XPath namespaces in master now. Depend on 
version 1.8.2-SNAPSHOT after having added the following repo:

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

To use namespaces in XPath you must for configure REST Assured to use 
namespaces, for example:

 given().
                config(newConfig().xmlConfig(xmlConfig().with().namespaceAware(true))).
 expect().
                body(hasXPath("/db:package-database", namespaceContext)).
when().
                get("/package-db-xml");

I'm going to continue working on namespace support for GPath expressions next.

Original comment by johan.ha...@gmail.com on 20 Nov 2013 at 4:50

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I've now also added support for expecting on namespace aware XML, for example 
given the resource "/namespace-examplet" returns:

<foo xmlns:ns="http://localhost/
         <bar>sudo </bar>
         <ns:bar>make me a sandwich!</ns:bar>
      </foo>

you can verify it like this:

     given().
              config(newConfig().xmlConfig(xmlConfig().declaredNamespace("ns", "http://localhost/"))
      expect().
              body("bar.text()", equalTo("sudo make me a sandwich!")).
              body(":bar.text()", equalTo("sudo ")).
              body("ns:bar.text()", equalTo("make me a sandwich!")).
      when().
              get("/namespace-example");

Original comment by johan.ha...@gmail.com on 21 Nov 2013 at 12:04

GoogleCodeExporter commented 8 years ago

Original comment by johan.ha...@gmail.com on 21 Nov 2013 at 12:04