The green shadow we see is a good sign! It means that the java code of the fixture is correctly referenced, so it does perform the test. (If a yellow shadow appears the reason could be that we are running tests against an endpoint of a web service (localhost:8765) which doesn't exist.)
This is a test case where we want to perform a GET request in order to query a WS and retrieve the information related to an organization 322 present in a db. Since the WS requires authentication, we need to set the header of the GET operation, specifically to pass the cookie value (the authentication is based on a cookie). Thanks to the cookie we get access to the information which are retrieved through a json communication
In order to get the value of the cookie so we can set the header of the GET request appropiately, we need to manually login into the web service and retrieve the value of the authentication cookie by having a look to the GET and POST requests through firebug.
setHeader is a method able to modify the GET request before it takes place and https://<web server address> together with /organizations/322 is the url which appears in the browser when we select a specific organization among the ones available in the db and shown by the WS on request
| !-Table:smartrics.rest.fitnesse.fixture.RestFixture-! |https://fis.absolutesw.com|
| setHeader | Cookie: sessionid=245cafd3020628359200573c2ff14610 |
|GET | /organizations/322 | 200 |Content-Type : application/json |!- //uri -!|
Anyway in order to retrieve an authentication cookie automatically, the fixture allows to perform the let method.
You can learn about let in the file C:\Users\andrea.ferraresi\Downloads\smartrics-RestFixture-fb35bc2\src\main\java\smartrics\rest\fitnesse\fixture\RestFixture.java
Anyway there should be a small error, since String loc = row.getCell(2).text(); means that we need to specify in the second column after the LET method the location of the value we want to retrieve: in this case we want to retrieve the authentication cookie value so we specify it to be retrieved inside the header of the HTTP response and, contrary to the comment in the RestFixture.java file, we specify the location in the 3rd column
The regex Set-Cookie:sessionid=[0-9a-z]* is able to retrieve the cookie value. [The regular expressions are used to specify a precise element in the header, an xpath expression is used
to specify a precise element in the body.]
Set-Cookie: is a regex to extract the value of the http response field Set-Cookie, then we need to specify the string sessionid=[0-9a-z]* because the field Set-Cookie contains more than one value. If we wanted to retrieve a value from the body of the response (we would have written | let | mycookie | body | ...)
This line
setHeader
Content-Type : application/x-www-form-urlencoded;charset=UTF-8
is optional
In few details, we set a body for the POST request, we perform the POST request and we get the authentication cookie, we store it in the mycookie variable and use it for the GET operation to retrieve the information about the organization number 322.
Here we have a complete example of a perform a test on the actual body retrieved: the HTTP response is written in json, anyway the fixture we are using is not able to understand json: at the moment what we can do is to write xpath expressions as expected results (like //uri or //name[text()='Incorporation']) and the fixture is able to parse the body of the HTTP response and assert if the actual result is equal to the expected one.
The implicit assertion is that the expected result should be contained in the actual result, which in the example is much longer than the expected one
| !-Table:smartrics.rest.fitnesse.fixture.RestFixture-! |https://<web server address> |
|setHeader|Content-Type : application/x-www-form-urlencoded;charset=UTF-8|
|setBody|!-email=<username>%40<domain>.com&password=<write here the password>-!|
|POST | /me?authentication | 201 | | no-body |
| let | mycookie | header | sessionid=[0-9a-z]* | sessionid= |
| !-Table:smartrics.rest.fitnesse.fixture.RestFixture-! |https://<web server address> |
| setHeader | Cookie: %mycookie% |
|GET | /organizations/322 | 200 |Content-Type : application/json |!- //name[text()='Incorporation'] -!|