Created
November 30, 2010 12:47
-
-
Save aeden/721632 to your computer and use it in GitHub Desktop.
Steps for testing API endpoints with Cucumber
This file contains hidden or 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
# Requires nokogiri and jsonpath | |
# Only works with rack test | |
World(Rack::Test::Methods) | |
# Feel free to customize this for whatever API auth scheme you use | |
Given /^I am a valid API user$/ do | |
user = Factory(:user) | |
authorize(user.email, user.password) | |
end | |
Given /^I send and accept XML$/ do | |
header 'Accept', 'text/xml' | |
header 'Content-Type', 'text/xml' | |
end | |
Given /^I send and accept JSON$/ do | |
header 'Accept', 'application/json' | |
header 'Content-Type', 'application/json' | |
end | |
When /^I send a GET request (?:for|to) "([^\"]*)"$/ do |path| | |
get path | |
end | |
When /^I send a POST request to "([^\"]*)"$/ do |path| | |
post path | |
end | |
When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, body| | |
post path, body | |
end | |
When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, body| | |
put path, body | |
end | |
When /^I send a DELETE request to "([^\"]*)"$/ do |path| | |
delete path | |
end | |
Then /^show me the response$/ do | |
p last_response | |
end | |
Then /^the response status should be "([^\"]*)"$/ do |status| | |
last_response.status.should eq(status.to_i), "response status code did not match: #{status} \n#{last_response.body}" | |
end | |
Then /^the JSON response should have "([^\"]*)" with the text "([^\"]*)"$/ do |json_path, text| | |
json = JSON.parse(last_response.body) | |
JsonPath.new(json_path).on(json).to_a.map(&:to_s).should include(text) | |
end | |
Then /^the XML response should have "([^\"]*)" with the text "([^\"]*)"$/ do |xpath, text| | |
parsed_response = Nokogiri::XML(last_response.body) | |
elements = parsed_response.xpath(xpath) | |
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}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment