Skip to content

jayzes/cucumber-api-steps

Repository files navigation

cucumber-api-steps

Build Status Gem Version

A set of Cucumber step definitions utilizing Rack-Test that ease basic testing of REST-style APIs using either XML or JSON formats.

Adapted from a blog post by Anthony Eden with a few additions based on my own needs. I found myself copying these step definitions around to multiple projects, and decided that it would be worthwhile to gem them up to keep things nice and DRY.

Dependencies

Requires Cucumber (obviously). Also makes use of JSONPath for setting criteria against JSON responses. See the gemspec for more info.

Installation

Add the following line to your Gemfile, preferably in the test or cucumber group:

gem 'cucumber-api-steps', :require => false

Then add the following line to your env.rb to make the step definitions available in your features:

require 'cucumber/api_steps'

Usage

Still a work in progress. For now, read the api_steps.rb file or check out the stashboard-rails project - its Cucumber features make extensive use of the steps in this gem.

Examples

Feature: API

  Scenario: List tweets in JSON
    When I send and accept JSON
    And I send a GET request to "/api/tweets"
    Then the response status should be "200"
    And the JSON response should be:
      """
      [{"tweet":"Hello World!"},{"tweet":"New Rails has been released"}]
      """
    And the JSON response should have "$..tweet" with the text "Hello World!"
    And the JSON response should have "$..tweet" with a length of 2

  Scenario: List tweets in XML
    When I send and accept XML
    And I send a GET request to "/api/tweets"
    Then the XML response should have "tweet" with the text "Hello World!"

  Scenario: Post tweet using POST-params
    When I send a POST request to "/api/tweets" with the following:
      | tweet | Hello World! |
      | lat   | 42.848282    |
      | lng   | 74.634933    |
    Then the response status should be "201"

  Scenario: Post tweet using json in POST body
    When I send a POST request to "/api/tweets" with the following:
      """
      {"tweet":"Hello World!","lat":"42.848282", "lng":"74.634933"}
      """
    Then the response status should be "201"

  Scenario: Basic authentication
    When I authenticate as the user "joe" with the password "password123"
    And I send a GET request to "/api/tweets"
    Then the response status should be "200"

  Scenario: Digest authentication
    When I digest-authenticate as the user "joe" with the password "password123"
    And I send a GET request to "/api/tweets"
    Then the response status should be "200"

Contributors

  • Jay Zeschin
  • Justin Smestad
  • Kevin Pfefferle
  • Kalys Osmonov
  • Mingding Han
  • Gabe Varela
  • Steven Heidel
  • Adam Elhardt
  • Gonzalo Bulnes Guilpain

Copyright

Copyright (c) 2011 Jay Zeschin. Distributed under the MIT License.

About

Cucumber steps to easily test REST-based XML and JSON APIs

Resources

License

Stars

Watchers

Forks

Packages

No packages published