Chris Kessel
Chris Kessel

Reputation: 5875

Fitnesse simple http request/response?

I have some SOAP requests and I'd like a Fitnesse page that'll execute the request and validate I got a response (either just checking it was a 200 OK or checking the SOAP response that comes back). In a nutshell, I'm using it to replay a series of SOAP requests and validate they worked, like a smoke test after deployment.

It seems like this should be something built in, sending a http request and checking the response code or bulk comparison of the body. I don't need to dig into the XML response itself. It's something I could actually script (bash/curl), but I'd rather use Fitnesse since we use that for a bunch of other stuff.

I'm using the Java version of fitnesse, not the C# (FitSharp) version.

I've hunted around without success, but I'm hoping I somehow missed it either in Fitnesse itself or a common 3rd party jar I can drop in to support this.

Upvotes: 1

Views: 3979

Answers (3)

Simon Elms
Simon Elms

Reputation: 19688

I recommend RestFixture. Despite the name it is a very simple HTTP client - you can craft any HTTP request then check the response that comes back: The status code, the headers (using regular expressions to check just the headers you're interested in) and the body (using XPath or JavaScript expectations to check just the nodes you're interested in). Handles either XML or JSON based on the value of the Content-Type header. Handles almost all the HTTP verbs: GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE. Don't think it handles PATCH though.

I've just started playing with Fried Hoeben's HttpTest fixtures, XmlHttpTest and JsonHttpTest (see Fried's answer elsewhere in this thread). They appear to have slightly more features than RestFixture. For example, they support templates which allow you to send repeated requests that are identical apart from certain values embedded in the body. You specify the template once then just list the values to pass into it for each call, avoiding having to repeatedly specify the whole request body each time.

On the other hand, for simple calls to web services I think RestFixture is easier to use.

RestFixture Overview

RestFixture is easy to remember as it effectively has only three types of commands:

1) set: setHeader and setBody. For specifying headers or the body of an HTTP request;

2) let: For assigning a value to a variable;

3) HTTP Verbs: Sends an HTTP request.

This is why I like RestFixture - the syntax for all HTTP verbs are the same:

| verb | uri | ?responseStatusCode | ?responseHeaders | ?responseBody |

You specify the HTTP verb, the URI to send the request to, the response status code you expect, the response headers you want to check (leave out any headers you don't want to test; regular expressions can be used) and the nodes in the response body to check.

Using the same syntax for every HTTP verb, I think, makes for very clean tests.

Example

| Import |
| smartrics.rest.fitnesse.fixture |

|Table: Rest Fixture | http://localhost:9876 |
| setBody | <resource><name>a name</name><data>data</data></resource> |
| POST | /resources/ | 201    |                          | no-body    |
| let  | id          | header | Location:/resources/(.+) |            |

| Table: Rest Fixture | http://localhost:9876 |
| GET | /resources/%id% | 200 | Content-Type : application/xml | !- /resource/name[text()='a name']
                                                                    /resource/data[text()='data'] -! |

This test generates a new item via a POST, reads the new item's ID back and assigns it to a variable, then uses that ID value with a GET to verify the new item exists and contains the right data.

The POST request expects the response to have a 201 status code and no body. The GET request expects the response to have a 200 status code and a Content-Type header set to application/xml. It expects the body to have a name node set to "a name" and a data node set to "data".

This example is for a RESTful web service but RestFixture would work just as well for SOAP. The body you specify for the POST would just be bigger.

Installation and Use

RestFixture can be found at http://github.com/smartrics/RestFixture. It includes links to documentation with extensive examples.

Here are instructions for building and installing RestFixture using Maven: workflow of creating tests using RestFixture

Coming from a .NET background without any experience of Maven, however, I found the easiest way to install it was to add it to an existing FitNesse installation:

1) Download the zipped binary from Maven Central, at http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22smartrics-RestFixture%22

2) Unpack the zip file to a directory alongside the FitrNesseRoot directory (the directory should NOT be under FitNesseRoot). For the sake of example let's call the directory RestFixtureLib (in reality you can name the directory anything you like);

3) In the RestFixtureLib directory delete the FitNesse-{version}.jar file, as we already have FitNesse installed;

4) In the test page that will include RestFixture tests add the RestFixtureLib directory to the class path:

!path RestFixtureLib/*.jar

(or whatever the path is to the directory you unpacked RestFixture to)

This class path definition could be included in a suite page or the root page if you'll be using RestFixture on more than one page.

5) RestFixture can be used with either the FIT or the SLIM test systems. If using SLIM include the following definition in the test page:

!define TEST_SYSTEM {slim}

6) It makes tests a lot less long-winded if you import the RestFixture namespace at the top of your test page:

| Import |
| smartrics.rest.fitnesse.fixture |

Then you can just define a test with

|Table: Rest Fixture | ... |
| ...                      |

Upvotes: 3

Fried Hoeben
Fried Hoeben

Reputation: 3272

My fixture library has support for SOAP calls, using its XmlHttpTest fixture.

Sample test using this approach where the request is completely in the wiki (scenarios and templates can also be used to configure the requests to be sent):

!define POST_BODY { {{{
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
  <s11:Body>
    <ns1:GetCityWeatherByZIP xmlns:ns1="http://ws.cdyne.com/WeatherWS/">
      <ns1:ZIP>90210</ns1:ZIP>
    </ns1:GetCityWeatherByZIP>
  </s11:Body>
</s11:Envelope>
}}} }

|script         |xml http test                                                       |
|post           |${POST_BODY}   |to                   |${URL}                        |
|check          |response status|200                                                 |
|show           |response                                                            |
|register prefix|weather        |for namespace        |http://ws.cdyne.com/WeatherWS/|
|check          |xPath          |//weather:City/text()|Beverly Hills                 |

Actual test output can be seen in the example report.

Upvotes: 1

Dan Woodward
Dan Woodward

Reputation: 2569

FitNesse itself is very agnostic about what you would want to test. So it doesn't come with a SOAP library out of the box. We implemented our own by wrapping the javax.xml.soap classes. Unfortunately, I can't share that code. I can tell you that it isn't terribly complicated.

You can find the basics of a simple SOAP call with these classes here: http://www.java2s.com/Code/Java/J2EE/ThisexampledemonstratesahelloworldexampleforusingJAXMAPI.htm

Upvotes: 0

Related Questions