From aaa50415b8706520f68467e58f49ed48e69696c5 Mon Sep 17 00:00:00 2001 From: pezholio Date: Tue, 10 Dec 2013 12:49:22 +0000 Subject: [PATCH 01/23] Test for content type --- lib/cucumber/api_steps.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index db26226..15e46dc 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -76,6 +76,14 @@ def to_str end end +Then(/^the response content type should be (XML|JSON)$/) do |type| + if self.respond_to? :should + last_response.content_type.should include("application/#{type.downcase}") + else + assert last_response.content_type.include?("application/#{type.downcase}") + end +end + Then /^the JSON response should (not)?\s?have "([^"]*)"$/ do |negative, json_path| json = JSON.parse(last_response.body) results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) From f8f9254614c64adb76fac3b6065cc9a10dc4f51b Mon Sep 17 00:00:00 2001 From: pezholio Date: Tue, 10 Dec 2013 12:50:25 +0000 Subject: [PATCH 02/23] Attempt to test the test --- features/response.feature | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/features/response.feature b/features/response.feature index ea73612..716d44a 100644 --- a/features/response.feature +++ b/features/response.feature @@ -9,6 +9,15 @@ Feature: """ Then the response status should be "200" + + Scenario: Test response content type + And I perform the following step: + """ + I send and accept XML + I send a GET request to "/api/books" + """ + + Then the response content type should be XML Scenario: Test that JSON response contains a node When I perform the following step: From 1e5f70d0ce08c9d02dae6a47e6517886037cbf0a Mon Sep 17 00:00:00 2001 From: Hans Lemuet Date: Mon, 29 Sep 2014 02:40:53 +0200 Subject: [PATCH 03/23] Update README.md I updated the URL of the blog post by Anthony Eden --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e9acaf..d30da86 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A set of [Cucumber](https://github.com/aslakhellesoy/cucumber) step definitions [Rack-Test](https://github.com/brynary/rack-test) that ease basic testing of REST-style APIs using either XML or JSON formats. -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. +Adapted from [a blog post by Anthony Eden](http://anthonyeden.com/2013/07/10/testing-rest-apis-with-cucumber-and-rack.html) 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 From e52286143f323bc905117db5c601561037ed7fee Mon Sep 17 00:00:00 2001 From: Hans Lemuet Date: Sun, 19 Oct 2014 10:10:05 +0200 Subject: [PATCH 04/23] Fix link to Cucumber Cucumber project is now hosted at https://github.com/cucumber/cucumber --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d30da86..01049a3 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/jayzes/cucumber-api-steps.png)](https://travis-ci.org/jayzes/cucumber-api-steps) [![Gem Version](https://badge.fury.io/rb/cucumber-api-steps.png)](http://badge.fury.io/rb/cucumber-api-steps) -A set of [Cucumber](https://github.com/aslakhellesoy/cucumber) step definitions utilizing +A set of [Cucumber](https://github.com/cucumber/cucumber) step definitions utilizing [Rack-Test](https://github.com/brynary/rack-test) that ease basic testing of REST-style APIs using either XML or JSON formats. From 27efce141f96813d292d307fc4e73a5cea480ec5 Mon Sep 17 00:00:00 2001 From: Dale Date: Mon, 13 Jul 2015 11:14:12 -0700 Subject: [PATCH 05/23] update rspec should syntax to v3 expect syntax --- lib/cucumber/api_steps.rb | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index db26226..0c1487c 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -69,8 +69,8 @@ def to_str end Then /^the response status should be "([^"]*)"$/ do |status| - if self.respond_to? :should - last_response.status.should == status.to_i + if self.respond_to?(:expect) + expect(last_response.status).to eq(status.to_i) else assert_equal status.to_i, last_response.status end @@ -79,11 +79,11 @@ def to_str Then /^the JSON response should (not)?\s?have "([^"]*)"$/ do |negative, json_path| json = JSON.parse(last_response.body) results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) - if self.respond_to?(:should) + if self.respond_to?(:expect) if negative.present? - results.should be_empty + expect(results).to be_empty else - results.should_not be_empty + expect(results).not_to be_empty end else if negative.present? @@ -98,11 +98,11 @@ def to_str Then /^the JSON response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text| json = JSON.parse(last_response.body) results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) - if self.respond_to?(:should) + if self.respond_to?(:expect) if negative.present? - results.should_not include(text) + expect(results).not_to include(text) else - results.should include(text) + expect(results).to include(text) end else if negative.present? @@ -116,11 +116,11 @@ def to_str Then /^the XML response should (not)?\s?have "([^"]*)"$/ do |negative, xpath| parsed_response = Nokogiri::XML(last_response.body) elements = parsed_response.xpath(xpath) - if self.respond_to?(:should) + if self.respond_to?(:expect) if negative.present? - elements.should be_empty + expect(elements).to be_empty else - elements.should_not be_empty + expect(elements).not_to be_empty end else if negative.present? @@ -134,9 +134,9 @@ def to_str Then /^the XML response should have "([^"]*)" with the text "([^"]*)"$/ do |xpath, text| parsed_response = Nokogiri::XML(last_response.body) elements = parsed_response.xpath(xpath) - if self.respond_to?(:should) - elements.should_not be_empty, "could not find #{xpath} in:\n#{last_response.body}" - elements.find { |e| e.text == text }.should_not be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}" + if self.respond_to?(:expect) + expect(elements).not_to be_empty, "could not find #{xpath} in:\n#{last_response.body}" + expect(elements.find { |e| e.text == text }).not_to be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}" else assert !elements.empty?, "could not find #{xpath} in:\n#{last_response.body}" assert elements.find { |e| e.text == text }, "found elements but could not find #{text} in:\n#{elements.inspect}" @@ -147,8 +147,8 @@ def to_str expected = JSON.parse(json) actual = JSON.parse(last_response.body) - if self.respond_to?(:should) - actual.should == expected + if self.respond_to?(:expect) + expect(actual).to eq(expected) else assert_equal actual, response end @@ -157,8 +157,8 @@ def to_str Then /^the JSON response should have "([^"]*)" with a length of (\d+)$/ do |json_path, length| json = JSON.parse(last_response.body) results = JsonPath.new(json_path).on(json) - if self.respond_to?(:should) - results.length.should == length.to_i + if self.respond_to?(:expect) + expect(results.length).to eq(length.to_i) else assert_equal length.to_i, results.length end From 1b53fcfe6e29134ccb031b0f4a098eb9d403e87f Mon Sep 17 00:00:00 2001 From: Kalys Osmonov Date: Mon, 3 Aug 2015 22:22:22 +0600 Subject: [PATCH 06/23] Pass StringIO instead of String when docstring is given as parameters --- lib/cucumber/api_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index 0c1487c..2df7978 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -47,7 +47,7 @@ def to_str if input.class == Cucumber::Ast::Table request_opts[:params] = input.rows_hash else - request_opts[:input] = input + request_opts[:input] = StringIO.new input end end From 49872d63db6b8e6e81c5fa9155375e234641a0a9 Mon Sep 17 00:00:00 2001 From: Nick Maher Date: Mon, 8 Jun 2015 10:23:16 +0100 Subject: [PATCH 07/23] Add support for PATCH requests --- features/fixtures/fake_app.rb | 6 +++++- features/request.feature | 9 +++++++++ lib/cucumber/api_steps.rb | 2 +- lib/cucumber/api_steps/version.rb | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/features/fixtures/fake_app.rb b/features/fixtures/fake_app.rb index e27f59c..3773de8 100644 --- a/features/fixtures/fake_app.rb +++ b/features/fixtures/fake_app.rb @@ -15,7 +15,7 @@ class FakeApp < Sinatra::Base if request.accept.empty? || request.accept?('application/json') content_type :json books.to_json - elsif request.accept?('application/xml') + elsif request.accept?('application/xml') content_type :xml books.to_xml end @@ -25,6 +25,10 @@ class FakeApp < Sinatra::Base status 201 if params.values == ["Metaprograming ruby", "Pragprog"] end + patch '/api/books' do + status 200 if params.values == ["Metaprograming ruby", "Pragprog"] + end + post '/api/publishers' do input_data = JSON.parse request.env["rack.input"].read, symbolize_names: true status 201 if input_data == {publisher: 'Pragprog'} diff --git a/features/request.feature b/features/request.feature index 866e7a4..1231412 100644 --- a/features/request.feature +++ b/features/request.feature @@ -16,6 +16,15 @@ Feature: """ Then the response status should be "201" + Scenario: POST request with params + When I perform the following step with table: + """ + I send a PATCH request to "/api/books" with the following: + | title | Metaprograming ruby | + | publisher | Pragprog | + """ + Then the response status should be "200" + Scenario: POST request with string When I perform the following step with string: """ diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index 2df7978..fee75b6 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -36,7 +36,7 @@ def to_str digest_authorize user, pass end -When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| +When /^I send a (GET|PATCH|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| request_type = args.shift path = args.shift input = args.shift diff --git a/lib/cucumber/api_steps/version.rb b/lib/cucumber/api_steps/version.rb index e0c3aee..796ee76 100644 --- a/lib/cucumber/api_steps/version.rb +++ b/lib/cucumber/api_steps/version.rb @@ -1,5 +1,5 @@ module Cucumber module ApiSteps - VERSION = "0.13" + VERSION = "0.14" end end From d3951929c3d3f5c01bb9a146afc7bce6bc905193 Mon Sep 17 00:00:00 2001 From: Kalys Osmonov Date: Wed, 5 Aug 2015 09:15:51 +0600 Subject: [PATCH 08/23] Revert version Fix cucumber feature title --- features/request.feature | 2 +- lib/cucumber/api_steps/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/features/request.feature b/features/request.feature index 1231412..2495eb8 100644 --- a/features/request.feature +++ b/features/request.feature @@ -16,7 +16,7 @@ Feature: """ Then the response status should be "201" - Scenario: POST request with params + Scenario: PATCH request with params When I perform the following step with table: """ I send a PATCH request to "/api/books" with the following: diff --git a/lib/cucumber/api_steps/version.rb b/lib/cucumber/api_steps/version.rb index 796ee76..e0c3aee 100644 --- a/lib/cucumber/api_steps/version.rb +++ b/lib/cucumber/api_steps/version.rb @@ -1,5 +1,5 @@ module Cucumber module ApiSteps - VERSION = "0.14" + VERSION = "0.13" end end From 9b5d393d03e8c021bad34beda14c5cbb3cdd0a71 Mon Sep 17 00:00:00 2001 From: Eden Vicary Date: Fri, 28 Aug 2015 15:36:25 +1000 Subject: [PATCH 09/23] Update to the latest classes This removes a deprivation warning that makes the running of tests look messy. And we want our cucumber warnings to run slick and professionally. --- lib/cucumber/api_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index fee75b6..376f489 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -44,7 +44,7 @@ def to_str request_opts = {method: request_type.downcase.to_sym} unless input.nil? - if input.class == Cucumber::Ast::Table + if input.class == Cucumber::MultilineArgument::DataTable request_opts[:params] = input.rows_hash else request_opts[:input] = StringIO.new input From a0f1269c8ba06b178590dcf98fd1136a095ed473 Mon Sep 17 00:00:00 2001 From: Eden Vicary Date: Fri, 28 Aug 2015 15:52:17 +1000 Subject: [PATCH 10/23] Update Cucumber and Rspec This also requires some updates on the tests, to remove deprecations and clean up the cucumber results. --- cucumber-api-steps.gemspec | 4 ++-- features/step_definitions/api_test_steps.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cucumber-api-steps.gemspec b/cucumber-api-steps.gemspec index 26cdcef..3b69033 100644 --- a/cucumber-api-steps.gemspec +++ b/cucumber-api-steps.gemspec @@ -15,9 +15,9 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9.3' s.add_dependency 'jsonpath', '>= 0.1.2' - s.add_dependency 'cucumber', '>= 1.2.1' + s.add_dependency 'cucumber', '>= 2.0.2' s.add_development_dependency 'activesupport', '>= 3.0.0' - s.add_development_dependency 'rspec', '~> 2.12.0' + s.add_development_dependency 'rspec', '~> 3.3.0' s.add_development_dependency 'sinatra', '~> 1.4.3' s.files = `git ls-files`.split("\n") diff --git a/features/step_definitions/api_test_steps.rb b/features/step_definitions/api_test_steps.rb index 31bb6e4..0ad4ee3 100644 --- a/features/step_definitions/api_test_steps.rb +++ b/features/step_definitions/api_test_steps.rb @@ -7,7 +7,7 @@ end Then /^the response should equal:$/ do |response_body| - last_response.body.should eq(response_body) + expect(last_response.body).to eq(response_body) end When /^I perform the following step with table:$/ do |step_definition| @@ -33,13 +33,13 @@ Then /^the request headers should be:$/ do |headers| headers_hash = headers.rows_hash request '/' - last_request.env.slice(*headers_hash.keys).values.should eq(headers_hash.values) + expect(last_request.env.slice(*headers_hash.keys).values).to eq(headers_hash.values) end Then /^I should be authenticated$/ do - last_request.env["HTTP_AUTHORIZATION"].should eq("Basic #{Base64.encode64("joe:god")}") + expect(last_request.env["HTTP_AUTHORIZATION"]).to eq("Basic #{Base64.encode64("joe:god")}") end Then /^I should be digest authenticated$/ do - last_request.env["HTTP_AUTHORIZATION"].starts_with?("Digest ").should be_true + expect(last_request.env["HTTP_AUTHORIZATION"].starts_with?("Digest ")).to be true end From 194f57ae2679346ab11eb8bd3e206a049fcf71bb Mon Sep 17 00:00:00 2001 From: Kirill Zhukov Date: Thu, 24 Sep 2015 13:27:59 -0700 Subject: [PATCH 11/23] Highlight Ruby and Gherkin syntax in README.md. --- README.md | 94 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 01049a3..a8e7f47 100644 --- a/README.md +++ b/README.md @@ -17,60 +17,64 @@ Requires [Cucumber](https://github.com/aslakhellesoy/cucumber) (obviously). Als Add the following line to your Gemfile, preferably in the test or cucumber group: - gem 'cucumber-api-steps', :require => false +```ruby +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' +```ruby +require 'cucumber/api_steps' +``` # Usage 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. # 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 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" - +```cucumber +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 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 From 794c8c1f9655dbb2c48a49304f4732e9467fe153 Mon Sep 17 00:00:00 2001 From: Jay Zeschin Date: Wed, 25 Oct 2017 12:53:10 -0600 Subject: [PATCH 12/23] Bump version to 0.2.0 --- cucumber-api-steps.gemspec | 2 +- lib/cucumber/api_steps/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cucumber-api-steps.gemspec b/cucumber-api-steps.gemspec index 3b69033..621f9e2 100644 --- a/cucumber-api-steps.gemspec +++ b/cucumber-api-steps.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.version = Cucumber::ApiSteps::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Jay Zeschin"] - s.email = ["jay.zeschin@modeset.com"] + s.email = ["jay@zeschin.org"] s.homepage = "http://github.com/jayzes/cucumber-api-steps" s.summary = %q{Cucumber steps to easily test REST-based XML and JSON APIs} s.description = %q{Cucumber steps to easily test REST-based XML and JSON APIs} diff --git a/lib/cucumber/api_steps/version.rb b/lib/cucumber/api_steps/version.rb index e0c3aee..a190cc4 100644 --- a/lib/cucumber/api_steps/version.rb +++ b/lib/cucumber/api_steps/version.rb @@ -1,5 +1,5 @@ module Cucumber module ApiSteps - VERSION = "0.13" + VERSION = "0.2.0" end end From eb119a624b14018275dbfb92c9b586d7a6ac3a30 Mon Sep 17 00:00:00 2001 From: Jay Zeschin Date: Wed, 25 Oct 2017 12:53:47 -0600 Subject: [PATCH 13/23] Remove stub RVM configuration --- .rvmrc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .rvmrc diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index 965ca62..0000000 --- a/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm use 1.9.3@cucumber-api-steps From 854a4badfa09db6da67a0e9adbb733c2a4435c35 Mon Sep 17 00:00:00 2001 From: Jay Zeschin Date: Wed, 25 Oct 2017 12:56:21 -0600 Subject: [PATCH 14/23] Bump version to 0.14.0 --- lib/cucumber/api_steps/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cucumber/api_steps/version.rb b/lib/cucumber/api_steps/version.rb index a190cc4..15701e6 100644 --- a/lib/cucumber/api_steps/version.rb +++ b/lib/cucumber/api_steps/version.rb @@ -1,5 +1,5 @@ module Cucumber module ApiSteps - VERSION = "0.2.0" + VERSION = "0.14.0" end end From 431da773bf3a89e8152984c09089bdcd53aac719 Mon Sep 17 00:00:00 2001 From: Jay Zeschin Date: Wed, 25 Oct 2017 13:02:09 -0600 Subject: [PATCH 15/23] Modernize the list of Ruby versions to test against --- .travis.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index a5b8f0f..defaed1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: ruby rvm: - - "1.9.3" - - "2.0.0" - - "2.1.2" - - jruby-19mode # JRuby in 1.9 mode + - "2.2.8" + - "2.3.5" + - "2.4.2" - jruby-20mode # JRuby in 1.9 mode From f40aef767c90966929415971567cf573794f5eda Mon Sep 17 00:00:00 2001 From: Jay Zeschin Date: Wed, 25 Oct 2017 13:02:27 -0600 Subject: [PATCH 16/23] Add LICENSE file --- LICENSE.TXT | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE.TXT diff --git a/LICENSE.TXT b/LICENSE.TXT new file mode 100644 index 0000000..779f8b4 --- /dev/null +++ b/LICENSE.TXT @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Ello PBC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 05113bb80477c8b072c44e7d81d3df3b6bd30965 Mon Sep 17 00:00:00 2001 From: glaszig Date: Wed, 18 Apr 2018 21:21:21 +0200 Subject: [PATCH 17/23] fix "the JSON response should be" basically just a typo. usage of wrong variable made assertion fail always. --- lib/cucumber/api_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index 376f489..ef3e2df 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -150,7 +150,7 @@ def to_str if self.respond_to?(:expect) expect(actual).to eq(expected) else - assert_equal actual, response + assert_equal actual, expected end end From 3cad2a187f596ea884f08532f574d02ee2aad030 Mon Sep 17 00:00:00 2001 From: glaszig Date: Thu, 19 Apr 2018 13:19:31 +0200 Subject: [PATCH 18/23] fix assertion argument order minitest assertions take the expected value as first, the actual value as second argument. --- lib/cucumber/api_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index ef3e2df..c677ba7 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -150,7 +150,7 @@ def to_str if self.respond_to?(:expect) expect(actual).to eq(expected) else - assert_equal actual, expected + assert_equal expected, actual end end From da0c270f50eaadcabaa875cf641676dc32035441 Mon Sep 17 00:00:00 2001 From: glaszig Date: Thu, 19 Apr 2018 13:46:03 +0200 Subject: [PATCH 19/23] use Base64.strict_encode64 which does not add line feeds every few characters. see https://ruby-doc.org/stdlib-2.2.0/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64 ::strict_encode64 also uses the same #pack logic as rack-test does inside its #basic_authorize method. see https://github.com/rack-test/rack-test/blob/72fc72dc15bfd33f45bc13cd134fb5e2c04dbe1e/lib/rack/test.rb#L166 more: https://glaucocustodio.github.io/2014/09/27/a-reminder-about-base64encode64-in-ruby/ --- features/step_definitions/api_test_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/step_definitions/api_test_steps.rb b/features/step_definitions/api_test_steps.rb index 0ad4ee3..fc94e75 100644 --- a/features/step_definitions/api_test_steps.rb +++ b/features/step_definitions/api_test_steps.rb @@ -37,7 +37,7 @@ end Then /^I should be authenticated$/ do - expect(last_request.env["HTTP_AUTHORIZATION"]).to eq("Basic #{Base64.encode64("joe:god")}") + expect(last_request.env["HTTP_AUTHORIZATION"]).to eq("Basic #{Base64.strict_encode64("joe:god")}") end Then /^I should be digest authenticated$/ do From 85a680141930a4a2dad29c8ea5762aa3b6028d11 Mon Sep 17 00:00:00 2001 From: glaszig Date: Thu, 19 Apr 2018 13:53:04 +0200 Subject: [PATCH 20/23] test against ruby 2.5 and ruby-head as well --- .travis.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index defaed1..fc757bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: ruby rvm: - - "2.2.8" - - "2.3.5" - - "2.4.2" + - 2.2.8 + - 2.3.5 + - 2.4.2 + - 2.5.1 - jruby-20mode # JRuby in 1.9 mode + - ruby-head From 0e4f5861a0167010b9e9bc78dc2bf576de26b4e4 Mon Sep 17 00:00:00 2001 From: Ralph Rooding Date: Tue, 16 Oct 2018 08:41:15 +0200 Subject: [PATCH 21/23] Fixed a wrong example The step is implemented as: `/^the XML response should have "([^"]*)" with the text "([^"]*)"$/` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a8e7f47..61e2e72 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Feature: API 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 text "Hello World!" + 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: From e425f34fe2f056e8f510165ab45a4509738fe14a Mon Sep 17 00:00:00 2001 From: pezholio Date: Tue, 10 Dec 2013 12:49:22 +0000 Subject: [PATCH 22/23] Test for content type --- lib/cucumber/api_steps.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/cucumber/api_steps.rb b/lib/cucumber/api_steps.rb index c677ba7..d2a0236 100644 --- a/lib/cucumber/api_steps.rb +++ b/lib/cucumber/api_steps.rb @@ -76,6 +76,14 @@ def to_str end end +Then(/^the response content type should be (XML|JSON)$/) do |type| + if self.respond_to? :should + last_response.content_type.should include("application/#{type.downcase}") + else + assert last_response.content_type.include?("application/#{type.downcase}") + end +end + Then /^the JSON response should (not)?\s?have "([^"]*)"$/ do |negative, json_path| json = JSON.parse(last_response.body) results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) From 0e5a4e9ef0232fc615cabee9ba4682c5b6137344 Mon Sep 17 00:00:00 2001 From: pezholio Date: Tue, 10 Dec 2013 12:50:25 +0000 Subject: [PATCH 23/23] Attempt to test the test --- features/response.feature | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/features/response.feature b/features/response.feature index ea73612..716d44a 100644 --- a/features/response.feature +++ b/features/response.feature @@ -9,6 +9,15 @@ Feature: """ Then the response status should be "200" + + Scenario: Test response content type + And I perform the following step: + """ + I send and accept XML + I send a GET request to "/api/books" + """ + + Then the response content type should be XML Scenario: Test that JSON response contains a node When I perform the following step: