cucumber-api-steps 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .DS_Store
@@ -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 CHANGED
@@ -2,4 +2,10 @@ require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'bueller'
5
- Bueller::Tasks.new
5
+ Bueller::Tasks.new
6
+
7
+ require 'cucumber/rake/task'
8
+
9
+ Cucumber::Rake::Task.new do |t|
10
+ t.cucumber_opts = %w{--format pretty}
11
+ end
@@ -12,9 +12,10 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Cucumber steps to easily test REST-based XML and JSON APIs}
13
13
  s.description = %q{Cucumber steps to easily test REST-based XML and JSON APIs}
14
14
 
15
- s.add_dependency 'jsonpath', '>= 0.1.2'
16
- s.add_dependency 'cucumber', '>= 0.8.3'
17
- s.add_development_dependency 'bueller'
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'
18
19
 
19
20
  s.files = `git ls-files`.split("\n")
20
21
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -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 'cucumber/api_steps'
@@ -2,80 +2,71 @@ require 'jsonpath'
2
2
 
3
3
  World(Rack::Test::Methods)
4
4
 
5
- Given /^I send and accept XML$/ do
6
- page.driver.header 'Accept', 'text/xml'
7
- page.driver.header 'Content-Type', 'text/xml'
5
+ Given /^I send and accept (XML|JSON)$/ do |type|
6
+ page.driver.header 'Accept', "text/#{type.downcase}"
7
+ page.driver.header 'Content-Type', "text/#{type.downcase}"
8
8
  end
9
9
 
10
- Given /^I send and accept JSON$/ do
11
- page.driver.header 'Accept', 'application/json'
12
- page.driver.header 'Content-Type', 'application/json'
13
- end
14
-
15
- When /^I authenticate as the user "([^\"]*)" with the password "([^\"]*)"$/ do |user, pass|
16
- page.driver.authorize(user, pass)
17
- end
18
-
19
- When /^I send a GET request (?:for|to) "([^\"]*)"$/ do |path|
20
- page.driver.get path
21
- end
22
-
23
- When /^I send a POST request to "([^\"]*)"$/ do |path|
24
- page.driver.post path
25
- end
26
-
27
- When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, body|
28
- page.driver.post path, body
29
- end
30
-
31
- When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, body|
32
- page.driver.put path, body
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
33
22
  end
34
23
 
35
- When /^I send a DELETE request to "([^\"]*)"$/ do |path|
36
- page.driver.delete path
24
+ When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |request_type, path, body|
25
+ if body.present?
26
+ page.driver.send(request_type.downcase.to_sym, path, body)
27
+ else
28
+ page.driver.send(request_type.downcase.to_sym, path)
29
+ end
37
30
  end
38
31
 
39
32
  Then /^show me the response$/ do
40
- p page.driver.last_response
33
+ p page.driver.response
41
34
  end
42
35
 
43
- Then /^the response status should be "([^\"]*)"$/ do |status|
36
+ Then /^the response status should be "([^"]*)"$/ do |status|
44
37
  if page.respond_to? :should
45
- page.driver.last_response.status.should == status.to_i
38
+ page.driver.response.status.should == status.to_i
46
39
  else
47
- assert_equal status.to_i, page.driver.last_response.status
40
+ assert_equal status.to_i, page.driver.response.status
48
41
  end
49
42
  end
50
43
 
51
- Then /^the JSON response should have "([^\"]*)" with the text "([^\"]*)"$/ do |json_path, text|
52
- json = JSON.parse(page.driver.last_response.body)
44
+ Then /^the JSON response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text|
45
+ json = JSON.parse(page.driver.response.body)
53
46
  results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
54
- if page.respond_to? :should
55
- results.should include(text)
47
+ if page.respond_to?(:should)
48
+ if negative.present?
49
+ results.should_not include(text)
50
+ else
51
+ results.should include(text)
52
+ end
56
53
  else
57
- assert results.include?(text)
54
+ if negative.present?
55
+ assert !results.include?(text)
56
+ else
57
+ assert results.include?(text)
58
+ end
58
59
  end
59
60
  end
60
61
 
61
- Then /^the JSON response should not have "([^\"]*)" with the text "([^\"]*)"$/ do |json_path, text|
62
- json = JSON.parse(page.driver.last_response.body)
63
- results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
64
- if page.respond_to? :should
65
- results.should_not include(text)
66
- else
67
- assert !results.include?(text)
68
- end
69
- end
70
-
71
- Then /^the XML response should have "([^\"]*)" with the text "([^\"]*)"$/ do |xpath, text|
62
+ Then /^the XML response should have "([^"]*)" with the text "([^"]*)"$/ do |xpath, text|
72
63
  parsed_response = Nokogiri::XML(last_response.body)
73
64
  elements = parsed_response.xpath(xpath)
74
- if page.respond_to? :should
65
+ if page.respond_to?(:should)
75
66
  elements.should_not be_empty, "could not find #{xpath} in:\n#{last_response.body}"
76
67
  elements.find { |e| e.text == text }.should_not be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}"
77
68
  else
78
69
  assert !elements.empty?, "could not find #{xpath} in:\n#{last_response.body}"
79
70
  assert elements.find { |e| e.text == text }, "found elements but could not find #{text} in:\n#{elements.inspect}"
80
- end
81
- end
71
+ end
72
+ end
@@ -1,5 +1,5 @@
1
1
  module Cucumber
2
2
  module ApiSteps
3
- VERSION = "0.4"
3
+ VERSION = "0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cucumber-api-steps
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- version: "0.4"
8
+ - 5
9
+ version: "0.5"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jay Zeschin
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-12 00:00:00 -06:00
17
+ date: 2011-10-05 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -63,6 +63,20 @@ dependencies:
63
63
  version: "0"
64
64
  type: :development
65
65
  version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: capybara
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :development
79
+ version_requirements: *id004
66
80
  description: Cucumber steps to easily test REST-based XML and JSON APIs
67
81
  email:
68
82
  - jay.zeschin@factorylabs.com
@@ -76,8 +90,10 @@ files:
76
90
  - .gitignore
77
91
  - .rvmrc
78
92
  - Gemfile
93
+ - README.md
79
94
  - Rakefile
80
95
  - cucumber-api-steps.gemspec
96
+ - features/support/env.rb
81
97
  - lib/cucumber/api_steps.rb
82
98
  - lib/cucumber/api_steps/version.rb
83
99
  has_rdoc: true
@@ -110,9 +126,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
126
  requirements: []
111
127
 
112
128
  rubyforge_project:
113
- rubygems_version: 1.4.2
129
+ rubygems_version: 1.6.2
114
130
  signing_key:
115
131
  specification_version: 3
116
132
  summary: Cucumber steps to easily test REST-based XML and JSON APIs
117
- test_files: []
118
-
133
+ test_files:
134
+ - features/support/env.rb