Skip to content

Commit

Permalink
versioning spec helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Cheung committed Nov 21, 2011
1 parent cbac06e commit 74ec997
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -19,3 +19,37 @@ def encode_basic(username, password)
RSpec.configure do |config|
config.include Rack::Test::Methods
end

# Versioning

# Returns the path with options[:version] prefixed if options[:using] is :path.
# Returns normal path otherwise.
def versioned_path(options = {})
case options[:using]
when :path
File.join('/', options[:prefix] || '', options[:version], options[:path])
when :header
File.join('/', options[:prefix] || '', options[:path])
else
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
end
end

def versioned_headers(options)
case options[:using]
when :path
{} # no-op
when :header
{
'HTTP_ACCEPT' => "application/vnd.#{options[:vendor]}-#{options[:version]}+#{options[:format]}"
}
else
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
end
end

def versioned_get(path, version_name, version_options = {})
path = versioned_path(version_options.merge(:version => version_name, :path => path))
headers = versioned_headers(version_options.merge(:version => version_name))
get path, {}, headers
end

0 comments on commit 74ec997

Please sign in to comment.