Skip to content

Instantly share code, notes, and snippets.

@acwright
Created February 14, 2012 18:30
Show Gist options
  • Select an option

  • Save acwright/1828870 to your computer and use it in GitHub Desktop.

Select an option

Save acwright/1828870 to your computer and use it in GitHub Desktop.
API step definitions for Cucumber
Given /^I have a valid API account$/ do
@account = Account.create(:token => UUID.generate(:compact))
@token = @account.token
authorize 'x', @account.secret
end
Given /^I send and accept XML$/ do
header 'Accept', 'application/xml'
header 'Content-Type', 'application/xml'
end
Given /^I send and accept JSON$/ do
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
end
Given /^I have (\d+) "([^\"]*)"$/ do |count, factory|
count.to_i.times do
record = Kernel.const_get(factory.singularize.capitalize).new
if record.respond_to?(:account_id)
record.account_id = @account.id
record.save
end
end
end
Given /^I have this "([^\"]*)":$/ do |factory, parameters|
record = Kernel.const_get(factory.capitalize).new(eval(parameters))
if [email protected]? && record.respond_to?(:account_id)
record.account_id = @account.id
end
self.instance_variable_set("@#{factory}".to_sym, record) if record.save
end
Given /^I have an? "([^\"]*)" with these attributes:$/ do |factory, attributes|
attributes.hashes.each do |hash|
hash.delete('id')
record = Kernel.const_get(factory.capitalize).new(hash)
if [email protected]? && record.respond_to?(:account_id)
record.account_id = @account.id
end
self.instance_variable_set("@#{factory}".to_sym, record) if record.save
end
end
When /^I send a GET request to "([^\"]*)"$/ do |path|
get path.gsub!(/\[([^\]]*)\]/) {|m| eval("#{m}") }
end
When /^I send a (POST|PUT) request to "([^\"]*)"$/ do |method, path|
send method.downcase, path.gsub!(/\[([^\]]*)\]/) {|m| eval("#{m}") }, @body
end
When /^I send a DELETE request to "([^\"]*)"$/ do |path|
delete path.gsub!(/\[([^\]]*)\]/) {|m| eval("#{m}") }
end
When /^I send the following body:$/ do |body|
@body = body.strip
end
Then /^the response status should be "([^\"]*)"$/ do |status|
last_response.status.should == status.to_i
end
Then /^the response should be the JSON:$/ do |json|
self.instance_variables.each {|v| self.instance_variable_get(v).reload if self.instance_variable_get(v).respond_to?(:reload) }
response = ActiveSupport::JSON.decode(last_response.body)
expected = ActiveSupport::JSON.decode(json.gsub(/\|([^\|]*)\|/) {|m| eval("#{m.gsub('|', '')}") })
expected.diff(response).should == {}
end
Then /^the response should be the XML:$/ do |xml|
self.instance_variables.each {|v| self.instance_variable_get(v).reload if self.instance_variable_get(v).respond_to?(:reload) }
response = Hash.from_xml(last_response.body)
expected = Hash.from_xml(xml.gsub(/\|([^\|]*)\|/) {|m| eval("#{m.gsub('|', '')}") })
expected.diff(response).should == {}
end
Then /^the XML response should be an? "([^\"]*)" array with (\d+) "([^\"]*)" elements?$/ do |tag, number_of_children, child_tag|
data = Nokogiri::XML(last_response.body)
data.xpath("//#{tag}/#{child_tag}").length.should == number_of_children.to_i
end
Then /^the JSON response should be an? "([^\"]*)" array with (\d+) "([^\"]*)" elements?$/ do |tag, number_of_children, name|
data = JSON.parse(last_response.body)
data.map { |d| d[name] }.length.should == number_of_children.to_i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment