Skip to content

Commit 263892a

Browse files
author
Jerry Cheung
committed
versioning spec helpers
1 parent 87642b5 commit 263892a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

spec/spec_helper.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,37 @@ def encode_basic(username, password)
1919
RSpec.configure do |config|
2020
config.include Rack::Test::Methods
2121
end
22+
23+
# Versioning
24+
25+
# Returns the path with options[:version] prefixed if options[:using] is :path.
26+
# Returns normal path otherwise.
27+
def versioned_path(options = {})
28+
case options[:using]
29+
when :path
30+
File.join('/', options[:prefix] || '', options[:version], options[:path])
31+
when :header
32+
File.join('/', options[:prefix] || '', options[:path])
33+
else
34+
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
35+
end
36+
end
37+
38+
def versioned_headers(options)
39+
case options[:using]
40+
when :path
41+
{} # no-op
42+
when :header
43+
{
44+
'HTTP_ACCEPT' => "application/vnd.#{options[:vendor]}-#{options[:version]}+#{options[:format]}"
45+
}
46+
else
47+
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
48+
end
49+
end
50+
51+
def versioned_get(path, version_name, version_options = {})
52+
path = versioned_path(version_options.merge(:version => version_name, :path => path))
53+
headers = versioned_headers(version_options.merge(:version => version_name))
54+
get path, {}, headers
55+
end

0 commit comments

Comments
 (0)