3 Handling XML Payloads - essenius/FitNesseFitSharpRest GitHub Wiki
XML payloads are handled similarly to Json payloads. We show that with an OData example service. However, there are a few things we need to configure, because this is a special kind of XML, namely Atom.
For changing the default configuration, we use the Rest Config table. This table should be the first table on a page. It configures the REST fixture for use of Atom payloads, and sets a namespace key as well. With this, we make implicit namespaces explicit, so they can be found in XPath queries.
!|table:Rest Config |
|DefaultAccept |application/atom+xml |
|DefaultContentType |application/atom+xml |
|DefaultXmlNameSpaceKey|a |
|XmlValueTypeAttribute |m:type |
|ContentTypeMapping |application/atom+xml: Xml|
Here is the call to the OData service:
!|script |RestTester |http://services.odata.org |
|$serviceBase=|echo |/V3/OData/OData.svc |
|Send |Get |to|$serviceBase/Products|expect code|200|
|$title= |value from response matching|//a:entry[1]/atom:title |
|$id= |value from response matching|//a:entry[1]//d:ID |
|$price= |value from response matching|//a:entry[1]//d:Price |
|$response= |Response Object |
As you can see, the fixture figured out that we are now using an XML object and not a Json object.
Changing XML is also possible:
!|script |
|$title=|echo |/a:feed/a:entry[2]/a:title |
|check |property value |$title|of|$response|Milk |
|ensure |set property value|$title|of|$response|to|Melk|
We can even merge an XML document, if we pay attention to the namespace:
!|script |
|$xml= |echo |<?xml version="1.0" encoding="utf-8"?> |
|$namespace=|echo |xmlns="http://www.w3.org/2005/Atom" |
|$content= |echo |$xml<contributor $namespace><name>John Doe</name></contributor>|
|$xpath= |echo |/a:feed[1]/a:entry[2]/a:content[1]/m:properties[1] |
|Add |$content|to |$response |at |$xpath |
We can validate that it’s there via a query:
!|query: properties for object |$response |$xpath/a:contributor[1]//*|
|Property |Type |Value |
|$xpath/a:contributor[1]/a:name[1]|System.String|John Doe |
Now we have a feel for both Json and XML payloads, let's have a look at the last one, text payloads.