RestFixtureFitTests.PostTests

Included page: .RestFixtureFitTests.SetUp (edit)

Action Fixture
start smartrics.rest.test.fitnesse.fixture.HttpServerFixture
check is started true
press reset resources database


POST


Posting an XML should create a resource

Note

You set the body for the POST operation with the setBody.
Here you can see how let is used. The first cell is the label of the variable.
The follwoing cell indicates where to get the data from. That is either the header or the _body_ of the last successful HTTP response
Then the expression to extract the value to be assigned to he variable. For header it has to be a regular expression, for _body_ it
has to be an XPath whose execution must return a String node.
Fit Rest Fixture http://localhost:7654
setBody <resource><name>test post</name><data>some data</data></resource>
POST /resources 201 Location : /resources/2
Content-Length : 0
Server : Jetty(6.0.2)
no-body
let id header Location:/resources/(.+) 2

Note

Variables are global: here the GET op is executed on an URI containing %id% where the label 'id' is defined in the test above.
The test succeedes only if the resulting XPath match the XML used for the creation of the resource.
You can also put variables in the expected result
The subsequent let operation shows how to use XPaths in the body.

Label values can be accessed from code using new Variables().get("labelname");

Note on fit.Fixture symbol map

Labels are implemented on top of the Fitnesse symbols map. So every label set via .RestFixture is accessible via fit.Fixture.getSymbol().

If a label starts with $, the $ is ignored.

Fit Rest Fixture http://localhost:7654
GET /resources/2 200 Set-Cookie : JID="ABC.${a.y}";
Content-Type : application/xml;charset=ISO-8859-1
Content-Length : 65
Server : Jetty(6.0.2)

 /resource/name[text()='test post']<br/>
 /resource/data[text()='some data']
let name body /resource/name/text() test post
let $symName body /resource/name/text() test post

Labels that are set to null are rendered using the default value for null, e.g. the string "null"

Fit Rest Fixture http://localhost:7654
GET /resources/2 200 Set-Cookie : JID="ABC.${a.y}";
Content-Type : application/xml;charset=ISO-8859-1
Content-Length : 65
Server : Jetty(6.0.2)
<resource><name>test post</name><data>some data</data></resource>
let thisIsNull body /resource/somewhereelse/text() null
comment this is null: 'null'

Albeit, the value can be overridden

smartrics.rest.fitnesse.fixture.RestFixtureConfig withBespokeNullRepresentation
restfixture.null.value.representation

Fit Rest Fixture http://localhost:7654 withBespokeNullRepresentation
GET /resources/2 200 Set-Cookie : JID="ABC.${a.y}";
Content-Type : application/xml;charset=ISO-8859-1
Content-Length : 65
Server : Jetty(6.0.2)
<resource><name>test post</name><data>some data</data></resource>
let thisIsNull body /resource/somewhereelse/text() null
comment this is null: ''

smartrics.rest.fitnesse.fixture.RestFixtureConfig withBespokeNullRepresentation
restfixture.null.value.representation NIL

Fit Rest Fixture http://localhost:7654 withBespokeNullRepresentation
GET /resources/2 200 Set-Cookie : JID="ABC.${a.y}";
Content-Type : application/xml;charset=ISO-8859-1
Content-Length : 65
Server : Jetty(6.0.2)
<resource><name>test post</name><data>some data</data></resource>
let thisIsNull body /resource/somewhereelse/text() null
comment this is null: 'NIL'


Json

Expectations can be asserted also in json format for responses whose content type is 'application/json'
Variables may be substitued in the expected result
Fit Rest Fixture http://localhost:7654
setBody { "resource" : { "name" : "test post", "data" : "some data" } }
POST /resources/ 201 Location : /resources/3
Content-Length : 0
Server : Jetty(6.0.2)
no-body
let id header Location:/resources/(.+) 3

Fit Rest Fixture http://localhost:7654
GET /resources/3.json 200 Content-Type : application/json
jsonbody.resource.name=='test post' <br/>
jsonbody.resource.data=='some data'

Other than the aforementioned content types you can emulate form POSTs.
Remember to escape the setBody content cell with ! - -!

Fit Rest Fixture http://localhost:7654
setHeader Content-Type : application/x-www-form-urlencoded;charset=UTF-8
setBody name=test%20post&data=some%20data
POST /resources 201 Location : /resources/4
Content-Length : 0
Server : Jetty(6.0.2)
no-body
let id header Location:/resources/(.+) 4

Fit Rest Fixture http://localhost:7654
GET /resources/4 200 Set-Cookie : JID="ABC.${a.y}";
Content-Type : application/xml;charset=ISO-8859-1
Content-Length : 68
Server : Jetty(6.0.2)

 /resource/name[text()='test post']
 /resource/data[text()='some data']