Included page: .RestFixtureTests.SetUp (edit)
| script | smartrics.rest.test.fitnesse.fixture.HttpServerFixture | |
| check | is started | true |
| 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.
| Table: Rest Fixture | http://localhost:9876 | |||
| setBody | { "resource" : { "name" : "test post", "data" : "some data" } } | |||
| POST | 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.
| Table: Rest Fixture | http://localhost:9876 | |||
| GET | 200 |
Content-Type : application/json expected Set-Cookie : JID="ABC.${a.y}"; Content-Type : application/json;charset=ISO-8859-1 Content-Length : 65 Server : Jetty(6.0.2) actual |
jsonbody.resource.name=="test post" && jsonbody.resource.data=="some data" expected { "resource" : { "name" : "test post", "data" : "some data" } } actual |
|
It is possible to specify multiple assertions, one for each line.
The test passes if each line evaluates to true.
| Table: Rest Fixture | http://localhost:9876 | |||
| GET | 200 |
Content-Type : application/json expected Set-Cookie : JID="ABC.${a.y}"; Content-Type : application/json;charset=ISO-8859-1 Content-Length : 65 Server : Jetty(6.0.2) actual |
jsonbody.resource.name.length >= 0 jsonbody.resource.name.length < 1000 jsonbody.resource.name.length < jsonbody.resource.name.length+1 expected { "resource" : { "name" : "test post", "data" : "some data" } } actual |
|
| let | content | js | response.body |
{ "resource" : { "name" : "test post", "data" : "some data" } } |
| Table: Rest Fixture | http://localhost:9876 | |||
| setBody | { "resource" : { "name" : "test post", "data" : "some data" } } |
|||
| POST | 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.
| Table: Rest Fixture | http://localhost:9876 | |||
| GET | 200 |
Content-Type : application/json expected Set-Cookie : JID="ABC.${a.y}"; Content-Type : application/json;charset=ISO-8859-1 Content-Length : 65 Server : Jetty(6.0.2) actual |
/* javascript */ var concat = jsonbody.resource.name + " and " + jsonbody.resource.data concat=="test post and some data" expected { "resource" : { "name" : "test post", "data" : "some data" } } actual |
|
| let | content | js | response.body |
{ "resource" : { "name" : "test post", "data" : "some data" } } |
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 | <br/>sampleJs=src/main/resources/FitNesseRoot/files/javascript/sample.js<br/>jsonpath=src/main/resources/FitNesseRoot/files/javascript/jsonpath-0.8.0.js |
| Table: Rest Fixture | http://localhost:9876 | imp | ||
| GET | 200 |
Content-Type : application/json expected Set-Cookie : JID="ABC.${a.y}"; Content-Type : application/json;charset=ISO-8859-1 Content-Length : 65 Server : Jetty(6.0.2) actual |
JSONPath.eval(jsonbody, "$..resource.name") == "test post" expected { "resource" : { "name" : "test post", "data" : "some data" } } actual |
|
| let | sum | js | var sum = sampleAdd(1, 2); sum; |
3.0 |