RestFixtureFitTests.JavascriptExpectations

Included page: .RestFixtureFitTests.SetUp (edit)

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


Expectations in Javascript on responses with content type application/json


As of .RestFixture Version 2, a javascript engine is embedded to allow expectations in Javascript on response body contents in JSON format.

XPaths and JSON

For backward compatibility XPath expressions are maintained and executed

After a successful response is received with content type "application/json" the expectation cell in a .RestFixture row is
interpreted as a string with Javascript and executed within the context of the response body.

Fit Rest Fixture http://localhost:7654
setBody { "resource" : { "name" : "test post", "data" : "some data" } }
POST /resources/ 201 Location : /resources/2
Content-Length : 0
Server : Jetty(6.0.2)
no-body
let id header Location:/resources/(.+) 2

A variable <code>jsonbody</code> is defined holding the JSON in the response.

The test passes if the result of the evaluation of the cell is a boolean.

Fit Rest Fixture http://localhost:7654
GET /resources/2.json 200 Content-Type : application/json
jsonbody.resource.name=="test post" && jsonbody.resource.data=="some data" 

It is possible to specify multiple assertions, one for each line.
The test passes if each line evaluates to true.

Fit Rest Fixture http://localhost:7654
GET /resources/2.json 200 Content-Type : application/json
jsonbody.resource.name.length>=0 
jsonbody.resource.name.length<1000
let content js response.body { "resource" : { "name" : "test post", "data" : "some data" } }

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

In some cases may be useful to force evaluation of the expectation cell as a javascript block.
By embedding the <code>/* javascript */</code> comment, the whole expectation cell will be parsed and evaluated entirely using the javascript interpreter.

Fit Rest Fixture http://localhost:7654
GET /resources/3.json 200 Content-Type : application/json
/* javascript */
var concat = jsonbody.resource.name + " and " + jsonbody.resource.data
concat=="test post and some data" 
let content js response.body { "resource" : { "name" : "test post", "data" : "some data" } }