Created
September 9, 2010 20:13
-
-
Save moklett/572468 to your computer and use it in GitHub Desktop.
A sample of the step definitions used in the Chargify API documentation (i.e. http://docs.chargify.com/api-subscriptions)
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
Given /^I (?:send and )?accept (.*?)(?: responses)?$/ do |format| | |
@format = format.downcase.to_sym | |
header('Content-Type', content_type_for(@format)) | |
http_accept(@format) | |
end | |
Given /^the requester accepts (.*) responses$/ do |format| | |
Given "I accept #{format} responses" | |
end | |
Given /^my site is in (test|production) mode$/ do |mode| | |
case mode | |
when 'test' | |
@site.update_attribute(:test_mode, true) | |
else | |
@site.update_attribute(:test_mode, false) | |
end | |
end | |
When /^I send a GET request to (.*)$/ do |url| | |
visit url_for_test(url) | |
end | |
Then /^the response status should be "([^\"]*)"$/ do |status| | |
response.status.should == status | |
end | |
Then /^the response content type should be "([^\"]*)"$/ do |content_type| | |
response.content_type.should == content_type | |
end | |
Then /^the response should contain (?:a|an) "([^\"]*)" element$/ do |el| | |
response.should have_selector(el) | |
end | |
Then /^the response should be (?:a|an) "([^\"]*)" element with these sub\-elements$/ do |el,table| | |
response.should have_selector(el) | |
table.hashes.each do |hash| | |
sub_el = hash['Element'] | |
content = hash['Data'] | |
if content == '?' || content =~ /^<[^>]*>$/ | |
content = nil | |
end | |
options = {} | |
unless content.blank? | |
options[:content] = content | |
end | |
response.should have_selector("#{el} > #{sub_el}", options) | |
end | |
end | |
Then /^the response should be a "([^\"]*)" array with (\d+) "([^\"]*)" elements?$/ do |element, count, subelement| | |
response.should have_selector(element) do |el| | |
el.should have_selector(subelement, :count => count.to_i) | |
end | |
end | |
Then /^the response should be a json array with (\d+) "([^\"]*)" objects?$/ do |count, object| | |
response_json = JSON.parse(response.body) | |
response_json.should be_a(Array) | |
response_json.size.should == count.to_i | |
response_json.collect {|h| h.keys.first }.should == [object] * count.to_i | |
end | |
Given /^I have this (?:json|xml) .*? data$/ do |data| | |
@data = replace_placeholders(data) | |
end | |
Then /^the response should be the json:$/ do |json| | |
response_json = JSON.parse(response.body) | |
expected_json = JSON.parse(replace_placeholders(json)) | |
compare_response_to_expected(response_json, expected_json) | |
end | |
Then /^the response should be the xml:$/ do |xml| | |
response_xml = Hash.from_xml(response.body) | |
expected_xml = Hash.from_xml(replace_placeholders(xml, :xml)) | |
compare_response_to_expected(response_xml, expected_xml) | |
end | |
When /^I send a POST request to (.*)$/ do |url| | |
request_page(url_for_test(url), :post, {}) | |
end | |
When /^I send a POST request with the (\w+) data to (.*)$/ do |type,url| | |
request_page(url_for_test(url), :post, @data) | |
end | |
When /^I send a PUT request with the (\w+) data to (.*)$/ do |type,url| | |
request_page(url_for_test(url), :put, @data) | |
end | |
When /^I send a DELETE request (?:with the \w+ data )?to (.*)$/ do |url| | |
extension = url.split('.').last | |
request_page(url_for_test(url), :delete, @data) | |
end | |
When /^I send a PUT request to (.*)$/ do |url| | |
request_page(url_for_test(url), :put, nil) | |
end | |
Then /^print the response$/ do | |
puts response.body | |
end | |
def url_for_test(url) | |
# Transform the URL for the testing environment | |
url.sub! /chargify\.com/, MAIN_HOST | |
url.sub! /^https/, 'http' | |
@subdomain = @site.subdomain | |
url = replace_placeholders(url) | |
url | |
end | |
def replace_placeholders(data, mode = :json) | |
data = data.to_s | |
# Do we need to replace any evals? i.e. [@customer.id] | |
re = /\[@([^\]]*)\]/ | |
while data =~ re | |
Rails.logger.debug "Eval'ing data (#{data.inspect})" | |
replacement = eval("@#{$1}") | |
data.sub! re, replacement.nil? ? '' : replacement.to_s | |
end | |
# Replace `your value` and `auto generated` with null | |
re = /(`your value`|`auto generated`)/ | |
null = (mode == :json) ? 'null' : '' | |
while data =~ re | |
data.sub! re, null | |
end | |
data | |
end | |
def content_type_for(type) | |
case type.to_s.downcase.to_sym | |
when :xml | |
'application/xml' | |
when :json | |
'application/json' | |
else | |
'text/html' | |
end | |
end | |
def ignore_in_response?(value) | |
value =~ /^(`your value`|`auto generated`)$/ | |
end | |
def compare_response_to_expected(response, expected) | |
case response | |
when Hash | |
# Check attribute presence | |
response.keys.sort.should == expected.keys.sort | |
# Check attribute values | |
expected.each do |key,value| | |
unless value.blank? | |
compare_response_to_expected(response[key], value) | |
end | |
end | |
when Array | |
begin | |
response.sort.should == expected.sort | |
rescue | |
response.should == expected | |
end | |
else | |
response.should == expected | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment