Created
August 18, 2014 01:19
-
-
Save bosskovic/98efa3c2a30a104869fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/jayzes/cucumber-api-steps | |
# https://gist.github.com/gonzalo-bulnes/7069339 | |
# http://anthonyeden.com/2013/07/10/testing-rest-apis-with-cucumber-and-rack.html | |
# http://vimeo.com/30586709 | |
Given /^I send and accept HTML$/ do | |
header 'Accept', "text/html" | |
header 'Content-Type', "application/x-www-form-urlencoded" | |
end | |
Given /^I send and accept (XML|JSON)$/ do |type| | |
header 'Accept', "application/#{type.downcase}" | |
header 'Content-Type', "application/#{type.downcase}" | |
end | |
Given /^I send and accept JSON using version (\d+) of the (\w+) API$/ do |version, model| | |
header 'Accept', "application/vnd.#{model}+json; version=#{version}" | |
header 'Content-Type', 'application/json' | |
end | |
# AUTHENTICATION | |
When /^I authenticate as the user "([^"]*)" with the password "([^"]*)"$/ do |user, pass| | |
authorize user, pass | |
end | |
When /^I digest\-authenticate as the user "(.*?)" with the password "(.*?)"$/ do |user, pass| | |
digest_authorize user, pass | |
end | |
Given /^I have a valid authentication token$/ do | |
@current_user ||= User.find_by(email: "[email protected]") | |
@current_user ||= FactoryGirl.create(:me) | |
end | |
# REQUEST / RESPONSE | |
When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| | |
request_type = args.shift | |
path = args.shift | |
input = args.shift | |
request_opts = {method: request_type.downcase.to_sym} | |
unless input.nil? | |
if input.class == Cucumber::Ast::Table | |
request_opts[:params] = input.rows_hash | |
else | |
request_opts[:input] = input | |
end | |
end | |
request path, request_opts | |
end | |
Then /^the response status should be "([^"]*)"$/ do |status| | |
begin | |
if self.respond_to? :should | |
last_response.status.should == status.to_i | |
else | |
assert_equal status.to_i, last_response.status | |
end | |
rescue RSpec::Expectations::ExpectationNotMetError => e | |
puts 'Response body:' | |
puts last_response.body | |
raise e | |
end | |
end | |
Then /^show me the (unparsed)?\s?response$/ do |unparsed| | |
if unparsed == 'unparsed' | |
puts last_response.body | |
elsif last_response.headers['Content-Type'] =~ /json/ | |
json_response = JSON.parse(last_response.body) | |
puts JSON.pretty_generate(json_response) | |
elsif last_response.headers['Content-Type'] =~ /xml/ | |
puts Nokogiri::XML(last_response.body) | |
else | |
puts last_response.headers | |
puts last_response.body | |
end | |
end | |
# CHECKING IF THE RESPONSE IS REPRESENTATION OF THE MODEL | |
Then /^the response body should be a JSON representation of the (\w+)$/ do |model| | |
last_response.body.should eq(model.constantize.last.to_json) | |
end | |
Then /^the response body should be a JSON representation of the (\w+) list$/ do |model| | |
last_response.body.should eq(model.constantize.all.to_json) | |
end | |
Then /^the response body should be a XML representation of the (\w+)$/ do |model| | |
last_response.body.should eq(model.constantize.last.to_xml) | |
end | |
Then /^the response body should be a XML representation of the (\w+) list$/ do |model| | |
last_response.body.should eq(model.constantize.all.to_xml) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment