FitNesseRoot.RestFixtureTests.JavascriptExpectations.content.txt Maven / Gradle / Ivy
!3 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.
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}|
|setBody|!- { "resource" : { "name" : "test post", "data" : "some data" } } -!|
|POST | /resources/ | 201 | | no-body |
|let | id | header | Location:/resources/(.+) | |
A variable jsonbody
is defined holding the JSON in the response.
The test passes if the result of the evaluation of the cell is a boolean.
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}|
|GET | /resources/%id%.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.
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}|
|GET | /resources/%id%.json | 200 |Content-Type : application/json |!-
jsonbody.resource.name.length >= 0
jsonbody.resource.name.length < 1000
jsonbody.resource.name.length < jsonbody.resource.name.length+1
-!|
| let | content | js | response.body | |
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}|
|setBody| %content% |
|POST | /resources/ | 201 | | no-body |
|let | id | header | Location:/resources/(.+) | |
In some cases may be useful to force evaluation of the expectation cell as a javascript block.
By embedding the /* javascript */
comment, the whole expectation cell will be parsed and evaluated entirely using the javascript interpreter.
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}|
|GET | /resources/%id%.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 | |
It is possible to inject external javascript scripts by setting the "restfixture.javascript.imports.map" property to a map value (script name=script path).
The path to the script can be to a file in the filesystem or to an racheable URL
| Table: Rest Fixture Config | imp |
| restfixture.javascript.imports.map | !-
sampleJs=src/main/resources/FitNesseRoot/files/javascript/sample.js
jsonpath=src/main/resources/FitNesseRoot/files/javascript/jsonpath-0.8.0.js-! |
| Table: Rest Fixture |http://${jettyHost}:${jettyPort}| imp |
|GET | /resources/%id%.json | 200 |Content-Type : application/json |JSONPath.eval(jsonbody, "$..resource.name") == "test post"|
| let | sum | js | var sum = sampleAdd(1, 2); sum;| |