This library provides Cucumber steps to test JSON HTTP API's. The steps are rather verbose (technical) in nature, somewhat contrary to the Cucumber philosophy of high-level, descriptive tests. This is intentional as it allows these steps to be bundled in larger, more high-level tests by non-developers.
Add this line to your application's Gemfile:
gem 'cucumber-http'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cucumber-http
Examples for testing the REST API of UiTDatabank:
@api @labels
Feature: Test the UDB3 labels API
Background:
Given I am using the UDB3 development environment
And I am authorized as user "centraal_beheerder"
And I send and accept JSON
@labelcreate
Scenario: Create label
When I create a label with a random name of 10 characters
And I keep the value of the JSON response at "uuid" as "uuid"
And I send a GET request to "/labels/%{uuid}"
Then the response status should be "200"
And the JSON response at "visibility" should be "visible"
And the JSON response at "privacy" should be "public"
Given /^I am benchmarking$/
Then /^the elapsed time should be less than (#{CAPTURE_FLOAT}) seconds?$/
Given /^I set headers?:$/
Given /^I send "(.*?)" and accept (XML|JSON)$/
Given /^I send and accept (XML|JSON)$/
Given /^I set the JSON request payload to:$/
Given /^I set the JSON request payload from "(.*?)"$/
When /^I send a (GET|POST|PATCH|PUT|DELETE) request to "([^"]*)"(?: with parameters?:)?$/
Then /^the response status should( not)? be "(#{CAPTURE_INTEGER})"$/
Then /^the response body should be valid (XML|JSON)$/
Then /^show me the( unparsed)? response$/
Then /^show me the kept values?$/
These steps are documented in the json_spec README
The steps in this library are overly technical for use in Cucumber as a communication
tool between developers and business people. They are meant to be encapsulated in
larger, more general steps through the use of the steps
method in Cucumber.
This allows easier reuse of code and the ability for non-developers to write requirements.
Examples for testing the REST API of UiTDatabank:
Creating an organizer with a random name:
When /^I(?: have)? created? an organizer with a random name of (#{CAPTURE_INTEGER}) characters?$/ do |characters|
name = Faker::Lorem.characters(characters)
steps %Q{
Given I am using the UDB3 development environment
And I am authorized as user "centraal_beheerder"
And I send and accept JSON
When I set the JSON request payload to:
"""
{"mainLanguage":"nl","website":"https://www.#{name}.be","name":"#{name}","contact":[]}
"""
And I send a POST request to "/organizers/"
}
end
Creating a role with a random name:
When /^I(?: have)? created? a role with a random name of (#{CAPTURE_INTEGER}) characters?$/ do |characters|
name = Faker::Lorem.characters(characters)
steps %Q{
Given I am using the UDB3 development environment
And I am authorized as user "centraal_beheerder"
And I send and accept JSON
When I set the JSON request payload to:
"""
{ "name": "#{name}" }
"""
And I send a POST request to "/roles/"
}
end
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
- Fork it ( https://github.com/[my-github-username]/cucumber-http/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request