api_spec 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +11 -0
- data/api_spec.gemspec +24 -0
- data/features/support/env.rb +11 -0
- data/lib/api_spec/cucumber.rb +76 -0
- data/lib/api_spec/version.rb +3 -0
- metadata +101 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create ruby-1.9.3@api_spec
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Cucumber API Steps
|
2
|
+
|
3
|
+
A set of [Cucumber](https://github.com/aslakhellesoy/cucumber) step definitions utilizing
|
4
|
+
[Rack-Test](https://github.com/brynary/rack-test) that ease basic
|
5
|
+
testing of REST-style APIs using either XML or JSON formats.
|
6
|
+
|
7
|
+
Adapted from [a blog post by Anthony Eden](http://www.anthonyeden.com/2010/11/testing-rest-apis-with-cucumber-and-rack-test/) 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.
|
8
|
+
|
9
|
+
## Dependencies
|
10
|
+
|
11
|
+
Requires [Cucumber](https://github.com/aslakhellesoy/cucumber) (obviously). Also makes use of [JSONPath](https://github.com/joshbuddy/jsonpath) for setting criteria against JSON responses. See the gemspec for more info.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add the following line to your Gemfile, preferably in the test or cucumber group:
|
16
|
+
|
17
|
+
gem 'cucumber-api-steps', :require => false
|
18
|
+
|
19
|
+
Then add the following line to your env.rb to make the step definitions available in your features:
|
20
|
+
|
21
|
+
require 'cucumber/api_steps'
|
22
|
+
|
23
|
+
# Usage
|
24
|
+
|
25
|
+
Still a work in progress. For now, read the api_steps.rb file or check out the [stashboard-rails](https://github.com/jayzes/stashboard-rails) project - its Cucumber features make extensive use of the steps in this gem.
|
26
|
+
|
27
|
+
One major caveat is that the way the steps are currently built, the PUT and POST steps accept a heredoc-style string (demarcated with lines of three double quotes) as a body, instead of a hash as many people seem to expect. I found this way to be more natural/flexible for how I write API tests, but it seems like others do not, so I'll be changing the steps to accept either a hash or a string soon.
|
28
|
+
|
29
|
+
# Contributors
|
30
|
+
* Jay Zeschin
|
31
|
+
* Justin Smestad
|
32
|
+
|
33
|
+
## Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2011 Jay Zeschin. Distributed under the MIT License.
|
data/Rakefile
ADDED
data/api_spec.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "api_spec/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "api_spec"
|
7
|
+
s.version = ApiSpec::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jay Zeschin", "Joseph DelCioppio"]
|
10
|
+
s.email = ["jay.zeschin@factorylabs.com", "joe@rentingsmart.com"]
|
11
|
+
s.homepage = "http://github.com/rentingsmart/api_spec"
|
12
|
+
s.summary = %q{Cucumber steps to easily test REST-based XML and JSON APIs}
|
13
|
+
s.description = %q{Cucumber steps to easily test REST-based XML and JSON APIs}
|
14
|
+
|
15
|
+
s.add_dependency 'jsonpath', '>= 0.1.2'
|
16
|
+
s.add_dependency 'cucumber', '>= 0.8.3'
|
17
|
+
s.add_development_dependency 'bueller'
|
18
|
+
s.add_development_dependency 'capybara'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
|
2
|
+
require 'capybara/cucumber'
|
3
|
+
|
4
|
+
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
|
5
|
+
# order to ease the transition to Capybara we set the default here. If you'd
|
6
|
+
# prefer to use XPath just remove this line and adjust any selectors in your
|
7
|
+
# steps to use the XPath syntax.
|
8
|
+
Capybara.default_selector = :css
|
9
|
+
Capybara.default_driver = :rack_test
|
10
|
+
|
11
|
+
require 'api_spec/cucumber'
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'jsonpath'
|
2
|
+
|
3
|
+
World(Rack::Test::Methods)
|
4
|
+
|
5
|
+
Given /^I send and accept (XML|JSON)$/ do |type|
|
6
|
+
page.driver.header 'Accept', "application/#{type.downcase}"
|
7
|
+
page.driver.header 'Content-Type', "application/#{type.downcase}"
|
8
|
+
end
|
9
|
+
|
10
|
+
When /^I authenticate as the user "([^"]*)" with the password "([^"]*)"$/ do |user, pass|
|
11
|
+
if page.driver.respond_to?(:basic_auth)
|
12
|
+
page.driver.basic_auth(user, pass)
|
13
|
+
elsif page.driver.respond_to?(:basic_authorize)
|
14
|
+
page.driver.basic_authorize(user, pass)
|
15
|
+
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
|
16
|
+
page.driver.browser.basic_authorize(user, pass)
|
17
|
+
elsif page.driver.respond_to?(:authorize)
|
18
|
+
page.driver.authorize(user, pass)
|
19
|
+
else
|
20
|
+
raise "Can't figure out how to log in with the current driver!"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args|
|
25
|
+
request_type = args.shift
|
26
|
+
path = args.shift
|
27
|
+
body = args.shift
|
28
|
+
if body.present?
|
29
|
+
page.driver.send(request_type.downcase.to_sym, path, body)
|
30
|
+
else
|
31
|
+
page.driver.send(request_type.downcase.to_sym, path)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Then /^show me the response$/ do
|
36
|
+
p page.driver.response
|
37
|
+
end
|
38
|
+
|
39
|
+
Then /^the response status should be "([^"]*)"$/ do |status|
|
40
|
+
if page.respond_to? :should
|
41
|
+
page.driver.response.status.should == status.to_i
|
42
|
+
else
|
43
|
+
assert_equal status.to_i, page.driver.response.status
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Then /^the JSON response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text|
|
48
|
+
json = JSON.parse(page.driver.response.body)
|
49
|
+
results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
|
50
|
+
if page.respond_to?(:should)
|
51
|
+
if negative.present?
|
52
|
+
results.should_not include(text)
|
53
|
+
else
|
54
|
+
results.should include(text)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
if negative.present?
|
58
|
+
assert !results.include?(text)
|
59
|
+
else
|
60
|
+
assert results.include?(text)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
Then /^the XML response should have "([^"]*)" with the text "([^"]*)"$/ do |xpath, text|
|
66
|
+
parsed_response = Nokogiri::XML(last_response.body)
|
67
|
+
elements = parsed_response.xpath(xpath)
|
68
|
+
if page.respond_to?(:should)
|
69
|
+
elements.should_not be_empty, "could not find #{xpath} in:\n#{last_response.body}"
|
70
|
+
elements.find { |e| e.text == text }.should_not be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}"
|
71
|
+
else
|
72
|
+
assert !elements.empty?, "could not find #{xpath} in:\n#{last_response.body}"
|
73
|
+
assert elements.find { |e| e.text == text }, "found elements but could not find #{text} in:\n#{elements.inspect}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_spec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.9'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jay Zeschin
|
9
|
+
- Joseph DelCioppio
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-11-01 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: jsonpath
|
17
|
+
requirement: &70119405930560 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.1.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70119405930560
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: cucumber
|
28
|
+
requirement: &70119405929660 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70119405929660
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bueller
|
39
|
+
requirement: &70119405929120 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70119405929120
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: capybara
|
50
|
+
requirement: &70119405928400 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70119405928400
|
59
|
+
description: Cucumber steps to easily test REST-based XML and JSON APIs
|
60
|
+
email:
|
61
|
+
- jay.zeschin@factorylabs.com
|
62
|
+
- joe@rentingsmart.com
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- .gitignore
|
68
|
+
- .rvmrc
|
69
|
+
- Gemfile
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- api_spec.gemspec
|
73
|
+
- features/support/env.rb
|
74
|
+
- lib/api_spec/cucumber.rb
|
75
|
+
- lib/api_spec/version.rb
|
76
|
+
homepage: http://github.com/rentingsmart/api_spec
|
77
|
+
licenses: []
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.8.10
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Cucumber steps to easily test REST-based XML and JSON APIs
|
100
|
+
test_files:
|
101
|
+
- features/support/env.rb
|