Jim Cakalic
Jim Cakalic

Reputation: 21

REST API Testing with Citrus/Cucumber

I'm piloting use of Cucumber for functional/integration testing within my development organization and have been using Citrus with the standard glue it provides for API testing. The hurdle I've encountered is how to dynamically change the REST URL given variables for a scenario. The capability seems to exist in the Java DSL but is not exposed in the Cucumber steps. I can configure the citrus-http:client with placeholders for system properties but these obviously need to be resolved when the application context is loaded by Spring. What I'd like to be able to do in my Background message definition is something like:

Given message todoListRequest
    And <todoListRequest> header Content-Type is "application/json"
    And <todoListRequest> header Accept is "application/json"
    And <todoListRequest> uri is "/todo/${item-number}"

and then in a Scenario:

Scenario: Gets expected item for specified item number
    Given variables 
        | item-number | 3 |
  When <todoListClient> sends message <todoListRequest>
  Then <todoListClient> should receive message <todoListResponse>

The service hostname and port could still be configured in the application context and the constructed URI appended to that value to create the target of the method (GET in this case, though I didn't specify and maybe that is something else that needs to be added?). Does that seem reasonable? Obviously, I could write my own glue for this but I wanted to see if there was an out of the box provided capability for what seems like a pretty obvious REST scenario before going that route. I understand the Cucumber integration is fairly recent (as of 2.6?) so it might still be maturing. This is an area where I would be interested in helping if that is welcome...

Thanks

Upvotes: 2

Views: 1558

Answers (1)

Christoph Deppisch
Christoph Deppisch

Reputation: 2216

You can use the Citrus internal message headers here:

And <todoListRequest> header citrus_http_method is "POST"
And <todoListRequest> header citrus_http_request_uri is "/todo/${item-number}"

The Citrus http client will read these special headers and remove those automatically before message is sent.

Edit: Since Citrus 2.7.1 there is a default REST Cucumber step API that provides brilliant access to sending and receiving messages over Http. So you can write

Given Content-Type: application/json
And Accept: application/json
When send POST /todo/${item-number}
Then receive status 200 OK

Read more about this here: http://www.citrusframework.org/reference/html/cucumber.html#http-steps

Upvotes: 3

Related Questions