Cucumber::Http
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.
Installation
Add this line to your application's Gemfile:
gem 'cucumber-http'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cucumber-http
Usage
Bundled steps
Examples
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 "application/json" and accept "application/ld+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"
@api @images
Feature: Test the UDB3 image API
Background:
Given I am using the UDB3 development environment
And I am authorized as user "centraal_beheerder"
And I accept "application/json"
@imagecreate
Scenario: Create image
Given I set the form data properties to:
| description | logo |
| copyrightHolder | me |
| language | nl |
When I upload "file" from path "images/UDB.jpg" to "/images/"
Then the response status should be "201"
Benchmarking
Given /^I am benchmarking$/
Then 'the elapsed time should be less than {float} second(s)'
HTTP
Given /^I set headers?:$/
Given /^I send "(.*?)"$/
Given /^I accept "(.*?)"$/
Given /^I send and accept "(.*?)"$/
Given /^I send "(.*?)" and accept "(.*?)"$/
Given /^I send "(.*?)" and accept JSON$/
Given /^I send and accept JSON$/
Given 'I set the form data properties to:'
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?:)?$/
When 'I upload {string} from path {string} to {string}"'
Then 'the response status should be "{int}"'
Then 'the response status should not be "{int}"'
Then /^the response body should be valid JSON$/
Debugging
Then /^show me the( unparsed)? response$/
Then /^show me the kept values?$/
JSON (through JsonSpec)
These steps are documented in the json_spec README
Making larger, more descriptive tests
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 create an organizer with a random name of {integer} character(s)' 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 "application/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 create a role with a random name of {integer} character(s)' 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 "application/json"
When I set the JSON request payload to:
"""
{ "name": "#{name}" }
"""
And I send a POST request to "/roles/"
}
end
Development
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.
Contributing
- 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