versioncake 1.3.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/Appraisals +6 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +4 -4
- data/README.md +65 -30
- data/RELEASE.md +1 -1
- data/bin/versioncake +9 -0
- data/gemfiles/rails3.2.gemfile.lock +6 -2
- data/gemfiles/rails4.0.gemfile.lock +1 -1
- data/gemfiles/rails4.1.gemfile +9 -0
- data/gemfiles/rails4.1.gemfile.lock +87 -0
- data/lib/versioncake/cli.rb +60 -0
- data/lib/versioncake/configuration.rb +21 -21
- data/lib/versioncake/railtie.rb +3 -19
- data/lib/versioncake/strategies/extraction_strategy.rb +5 -5
- data/lib/versioncake/strategies/http_accept_parameter_strategy.rb +1 -1
- data/lib/versioncake/strategies/http_header_strategy.rb +2 -2
- data/lib/versioncake/strategies/path_parameter_strategy.rb +11 -0
- data/lib/versioncake/strategies/query_parameter_strategy.rb +2 -2
- data/lib/versioncake/strategies/request_parameter_strategy.rb +2 -2
- data/lib/versioncake/version.rb +1 -1
- data/lib/versioncake/versioned_request.rb +10 -6
- data/lib/versioncake/view_additions.rb +67 -10
- data/lib/versioncake.rb +2 -0
- data/test/{fixtures/templates/versioned.v1.html.erb → app/views/renders/index.html.v1.erb} +0 -0
- data/test/{fixtures/templates/versioned.v2.html.erb → app/views/renders/index.html.v2.erb} +0 -0
- data/test/config/application.rb +3 -2
- data/test/fixtures/templates/v1_extension_scheme.v3.html.erb +7 -0
- data/test/fixtures/templates/v1_extension_scheme.v6.json +4 -0
- data/test/fixtures/templates/versioned.html.v1.erb +1 -0
- data/test/fixtures/templates/versioned.html.v2.erb +1 -0
- data/test/fixtures/templates/{versioned.v3.html.erb → versioned.html.v3.erb} +0 -0
- data/test/fixtures/test_cases.yml +70 -0
- data/test/functional/custom_strategy_controller_test.rb +16 -0
- data/test/functional/multiple_strategy_controller_test.rb +24 -0
- data/test/functional/renders_controller_test.rb +14 -168
- data/test/functional/strategy_controller_test.rb +37 -0
- data/test/unit/cli_test.rb +48 -0
- data/test/unit/configuration_test.rb +15 -19
- data/test/unit/strategies/extraction_strategy_test.rb +2 -2
- data/test/unit/strategies/path_parameter_strategy_test.rb +17 -0
- data/test/unit/strategies/query_parameter_strategy_test.rb +5 -0
- data/test/unit/versioned_request_test.rb +1 -1
- data/test/unit/view_additions_test.rb +35 -0
- data/versioncake.gemspec +1 -0
- metadata +25 -9
- data/test/app/views/renders/index.v1.html.erb +0 -1
- data/test/app/views/renders/index.v2.html.erb +0 -1
@@ -2,8 +2,8 @@ module VersionCake
|
|
2
2
|
class HttpHeaderStrategy < ExtractionStrategy
|
3
3
|
|
4
4
|
def execute(request)
|
5
|
-
if request.headers.key? "HTTP_X_#{
|
6
|
-
request.headers["HTTP_X_#{
|
5
|
+
if request.headers.key? "HTTP_X_#{version_key.upcase}"
|
6
|
+
request.headers["HTTP_X_#{version_key.upcase}"]
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -2,8 +2,8 @@ module VersionCake
|
|
2
2
|
class QueryParameterStrategy < ExtractionStrategy
|
3
3
|
|
4
4
|
def execute(request)
|
5
|
-
if request.query_parameters.key?
|
6
|
-
request.query_parameters[
|
5
|
+
if request.query_parameters.key? version_key.to_sym
|
6
|
+
request.query_parameters[version_key.to_sym].to_s
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -2,8 +2,8 @@ module VersionCake
|
|
2
2
|
class RequestParameterStrategy < ExtractionStrategy
|
3
3
|
|
4
4
|
def execute(request)
|
5
|
-
if request.request_parameters.has_key?
|
6
|
-
request.request_parameters[
|
5
|
+
if request.request_parameters.has_key? version_key.to_sym
|
6
|
+
request.request_parameters[version_key.to_sym]
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
data/lib/versioncake/version.rb
CHANGED
@@ -4,18 +4,22 @@ module VersionCake
|
|
4
4
|
|
5
5
|
def initialize(request, version_override=nil)
|
6
6
|
@version = version_override || extract_version(request)
|
7
|
-
@is_latest_version = @version ==
|
7
|
+
@is_latest_version = @version == config.latest_version
|
8
8
|
end
|
9
9
|
|
10
10
|
def supported_versions
|
11
|
-
|
11
|
+
config.supported_versions(@version)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
+
def config
|
17
|
+
VersionCake::Railtie.config.versioncake
|
18
|
+
end
|
19
|
+
|
16
20
|
def apply_strategies(request)
|
17
21
|
version = nil
|
18
|
-
|
22
|
+
config.extraction_strategies.each do |strategy|
|
19
23
|
version = strategy.extract(request)
|
20
24
|
break unless version.nil?
|
21
25
|
end
|
@@ -25,10 +29,10 @@ module VersionCake
|
|
25
29
|
def extract_version(request)
|
26
30
|
@extracted_version = apply_strategies(request)
|
27
31
|
if @extracted_version.nil?
|
28
|
-
@version =
|
29
|
-
elsif
|
32
|
+
@version = config.default_version || config.latest_version
|
33
|
+
elsif config.supports_version? @extracted_version
|
30
34
|
@version = @extracted_version
|
31
|
-
elsif @extracted_version >
|
35
|
+
elsif @extracted_version > config.latest_version
|
32
36
|
raise ActionController::RoutingError.new("No route match for version")
|
33
37
|
else
|
34
38
|
raise ActionController::RoutingError.new("Version is deprecated")
|
@@ -3,26 +3,83 @@ require 'action_view'
|
|
3
3
|
# register an addition detail for the lookup context to understand,
|
4
4
|
# this will allow us to have the versions available upon lookup in
|
5
5
|
# the resolver.
|
6
|
-
ActionView::LookupContext.register_detail(:versions){ VersionCake::
|
6
|
+
ActionView::LookupContext.register_detail(:versions){ VersionCake::Railtie.config.versioncake.supported_versions }
|
7
7
|
|
8
8
|
ActionView::PathResolver.class_eval do
|
9
|
-
|
10
9
|
# not sure why we are doing this yet, but looks like a good idea
|
11
|
-
|
10
|
+
if ActionPack::VERSION::MAJOR >= 4 && ActionPack::VERSION::MINOR >= 1
|
11
|
+
ActionView::PathResolver::EXTENSIONS.replace({
|
12
|
+
:locale => ".",
|
13
|
+
:formats => ".",
|
14
|
+
:versions => ".",
|
15
|
+
:variants => "+",
|
16
|
+
:handlers => "."
|
17
|
+
})
|
18
|
+
|
19
|
+
ActionView::PathResolver::DEFAULT_PATTERN.replace ":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:versions,}{.:handlers,}"
|
20
|
+
else
|
21
|
+
ActionView::PathResolver::EXTENSIONS.replace [:locale, :formats, :versions, :handlers]
|
22
|
+
|
23
|
+
# The query builder has the @details from the lookup_context and will
|
24
|
+
# match the detail name to the string in the pattern, so we must append
|
25
|
+
# it to the default pattern
|
26
|
+
ActionView::PathResolver::DEFAULT_PATTERN.replace ":prefix/:action{.:locale,}{.:formats,}{.:versions,}{.:handlers,}"
|
27
|
+
end
|
28
|
+
|
29
|
+
# The default extract handler expects that the handler is the last extension and
|
30
|
+
# the format is the next one. Since we are replacing the DEFAULT_PATTERN, we need to
|
31
|
+
# make sure that we extract the format from the correct position.
|
32
|
+
#
|
33
|
+
# The version may be stuck inbetween the format and the handler. This is actually pretty tricky
|
34
|
+
# because the version is optional and the locale is optional-which means when there are 3 'pieces'
|
35
|
+
# it may be the locale, format and handler or the format, version and handler. To check this, we will
|
36
|
+
# try one additional time if there are more pieces, which should cover all the cases:
|
37
|
+
#
|
38
|
+
# Cases:
|
39
|
+
# 1: assume version is in the extension, pieces = ['html','erb']
|
40
|
+
# 2: assume version is in the extension, pieces = ['html','v1','erb']
|
41
|
+
# 3: assume version is in the extension, pieces = ['en','html','erb']
|
42
|
+
# 4: assume version is in the extension, pieces = ['en','html','v1','erb']
|
43
|
+
#
|
44
|
+
def extract_handler_and_format(path, default_formats)
|
45
|
+
pieces = File.basename(path).split(".")
|
46
|
+
pieces.shift
|
47
|
+
|
48
|
+
extension = pieces.pop
|
49
|
+
if ActionPack::VERSION::MAJOR == 4
|
50
|
+
unless extension
|
51
|
+
message = "The file #{path} did not specify a template handler. The default is currently ERB, " \
|
52
|
+
"but will change to RAW in the future."
|
53
|
+
ActiveSupport::Deprecation.warn message
|
54
|
+
end
|
55
|
+
end
|
56
|
+
handler = ActionView::Template.handler_for_extension(extension)
|
57
|
+
format = get_format_from_pieces(pieces, (ActionPack::VERSION::MAJOR == 4 ? ActionView::Template::Types : Mime))
|
12
58
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
59
|
+
[handler, format]
|
60
|
+
end
|
61
|
+
|
62
|
+
# If there are still pieces and we didn't find a valid format, we may
|
63
|
+
# have a version in the extension, so try one more time to pop the format.
|
64
|
+
def get_format_from_pieces(pieces, format_list)
|
65
|
+
format = nil
|
66
|
+
pieces.reverse.each do |piece|
|
67
|
+
if ActionView::PathResolver::EXTENSIONS.is_a?(Hash) &&
|
68
|
+
ActionView::PathResolver::EXTENSIONS.include?(:variants)
|
69
|
+
piece = piece.split(ActionView::PathResolver::EXTENSIONS[:variants], 2).first # remove variant from format
|
70
|
+
end
|
17
71
|
|
72
|
+
format = format_list[piece]
|
73
|
+
break unless format.nil?
|
74
|
+
end
|
75
|
+
format
|
76
|
+
end
|
18
77
|
end
|
19
78
|
|
20
79
|
ActionView::Template.class_eval do
|
21
|
-
|
22
80
|
# the identifier method name filters out numbers,
|
23
81
|
# but we want to preserve them for v1 etc.
|
24
82
|
def identifier_method_name #:nodoc:
|
25
83
|
inspect.gsub(/[^a-z0-9_]/, '_')
|
26
84
|
end
|
27
|
-
|
28
|
-
end
|
85
|
+
end
|
data/lib/versioncake.rb
CHANGED
@@ -2,6 +2,7 @@ require 'versioncake/strategies/extraction_strategy'
|
|
2
2
|
require 'versioncake/strategies/http_accept_parameter_strategy'
|
3
3
|
require 'versioncake/strategies/http_header_strategy'
|
4
4
|
require 'versioncake/strategies/query_parameter_strategy'
|
5
|
+
require 'versioncake/strategies/path_parameter_strategy'
|
5
6
|
require 'versioncake/strategies/request_parameter_strategy'
|
6
7
|
require 'versioncake/strategies/custom_strategy'
|
7
8
|
|
@@ -10,3 +11,4 @@ require 'versioncake/controller_additions'
|
|
10
11
|
require 'versioncake/view_additions'
|
11
12
|
require 'versioncake/versioned_request'
|
12
13
|
require 'versioncake/railtie'
|
14
|
+
require 'versioncake/cli'
|
File without changes
|
File without changes
|
data/test/config/application.rb
CHANGED
@@ -11,7 +11,8 @@ module RendersTest
|
|
11
11
|
config.eager_load = false
|
12
12
|
|
13
13
|
config.active_support.deprecation = :stderr
|
14
|
-
|
15
|
-
config.
|
14
|
+
|
15
|
+
config.versioncake.supported_version_numbers = (1..3)
|
16
|
+
config.versioncake.extraction_strategy = [:http_header, :http_accept_parameter, :query_parameter, :request_parameter]
|
16
17
|
end
|
17
18
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
template v1
|
@@ -0,0 +1 @@
|
|
1
|
+
template v2
|
File without changes
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# http_header_strategy
|
2
|
+
- request:
|
3
|
+
headers:
|
4
|
+
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=1"
|
5
|
+
response: "template v1"
|
6
|
+
|
7
|
+
- request:
|
8
|
+
headers:
|
9
|
+
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=2"
|
10
|
+
response: "template v2"
|
11
|
+
|
12
|
+
- request:
|
13
|
+
headers:
|
14
|
+
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=INVALID"
|
15
|
+
response: "template v2"
|
16
|
+
|
17
|
+
# request_parameter_strategy
|
18
|
+
- request:
|
19
|
+
method: post
|
20
|
+
params:
|
21
|
+
api_version: "1"
|
22
|
+
response: "template v1"
|
23
|
+
|
24
|
+
- request:
|
25
|
+
method: post
|
26
|
+
params:
|
27
|
+
api_version: "2"
|
28
|
+
response: "template v2"
|
29
|
+
|
30
|
+
- request:
|
31
|
+
method: post
|
32
|
+
params:
|
33
|
+
api_version: "INVALID"
|
34
|
+
response: "template v2"
|
35
|
+
|
36
|
+
# query_parameter_strategy
|
37
|
+
- request:
|
38
|
+
params:
|
39
|
+
api_version: "1"
|
40
|
+
response: "template v1"
|
41
|
+
|
42
|
+
- request:
|
43
|
+
params:
|
44
|
+
api_version: "2"
|
45
|
+
response: "template v2"
|
46
|
+
|
47
|
+
- request:
|
48
|
+
params:
|
49
|
+
api_version: "INVALID"
|
50
|
+
response: "template v2"
|
51
|
+
|
52
|
+
# http_header_strategy
|
53
|
+
- request:
|
54
|
+
headers:
|
55
|
+
HTTP_X_API_VERSION: "1"
|
56
|
+
response: "template v1"
|
57
|
+
|
58
|
+
- request:
|
59
|
+
headers:
|
60
|
+
HTTP_X_API_VERSION: "2"
|
61
|
+
response: "template v2"
|
62
|
+
|
63
|
+
- request:
|
64
|
+
headers:
|
65
|
+
HTTP_X_API_VERSION: "INVALID"
|
66
|
+
response: "template v2"
|
67
|
+
|
68
|
+
# general
|
69
|
+
- request:
|
70
|
+
response: "template v2"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_controller/test_case'
|
4
|
+
|
5
|
+
class CustomStrategyTest < ActionController::TestCase
|
6
|
+
tests RendersController
|
7
|
+
|
8
|
+
setup do
|
9
|
+
VersionCake::Configuration.any_instance.stubs(:extraction_strategy => lambda { |request| 2 })
|
10
|
+
end
|
11
|
+
|
12
|
+
test "renders version 2 of the partial based on the header Accept" do
|
13
|
+
get :index
|
14
|
+
assert_equal "template v2", @response.body
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_controller/test_case'
|
4
|
+
|
5
|
+
class MultipleStrategyTest < ActionController::TestCase
|
6
|
+
tests RendersController
|
7
|
+
|
8
|
+
test "renders version 1 of the partial based on the header Accept" do
|
9
|
+
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=1"})
|
10
|
+
get :index
|
11
|
+
assert_equal "template v1", @response.body
|
12
|
+
end
|
13
|
+
|
14
|
+
test "renders the query parameter when accept parameter isn't available" do
|
15
|
+
get :index, "api_version" => "1"
|
16
|
+
assert_equal "template v1", @response.body
|
17
|
+
end
|
18
|
+
|
19
|
+
test "renders the higher priority accept parameter version" do
|
20
|
+
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=2"})
|
21
|
+
get :index, "api_version" => "1"
|
22
|
+
assert_equal "template v2", @response.body
|
23
|
+
end
|
24
|
+
end
|
@@ -4,32 +4,23 @@ require 'action_controller/test_case'
|
|
4
4
|
|
5
5
|
class RendersControllerTest < ActionController::TestCase
|
6
6
|
|
7
|
-
setup do
|
8
|
-
# change the version string for configuration testing
|
9
|
-
VersionCake::ExtractionStrategy.version_string = "version"
|
10
|
-
end
|
11
|
-
|
12
|
-
teardown do
|
13
|
-
VersionCake::ExtractionStrategy.version_string = "api_version"
|
14
|
-
end
|
15
|
-
|
16
7
|
test "render latest version of partial" do
|
17
8
|
get :index
|
18
|
-
assert_equal @response.body
|
9
|
+
assert_equal "template v2", @response.body
|
19
10
|
end
|
20
11
|
|
21
12
|
test "exposes the requested version" do
|
22
|
-
get :index, "
|
23
|
-
assert_equal @controller.requested_version
|
13
|
+
get :index, "api_version" => "1"
|
14
|
+
assert_equal 1, @controller.requested_version
|
24
15
|
end
|
25
16
|
|
26
17
|
test "exposes latest version when requesting the latest" do
|
27
|
-
get :index, "
|
18
|
+
get :index, "api_version" => "3"
|
28
19
|
assert @controller.is_latest_version
|
29
20
|
end
|
30
21
|
|
31
22
|
test "reports not the latest version" do
|
32
|
-
get :index, "
|
23
|
+
get :index, "api_version" => "1"
|
33
24
|
assert !@controller.is_latest_version
|
34
25
|
end
|
35
26
|
|
@@ -39,10 +30,9 @@ class RendersControllerTest < ActionController::TestCase
|
|
39
30
|
end
|
40
31
|
|
41
32
|
test "exposes the default version when the version is not set default is set" do
|
42
|
-
VersionCake::Configuration.default_version
|
33
|
+
VersionCake::Configuration.any_instance.stubs(:default_version => 1)
|
43
34
|
get :index
|
44
35
|
assert_equal 1, @controller.derived_version
|
45
|
-
VersionCake::Configuration.default_version = nil
|
46
36
|
end
|
47
37
|
|
48
38
|
test "requested version is blank when the version is not set" do
|
@@ -51,158 +41,9 @@ class RendersControllerTest < ActionController::TestCase
|
|
51
41
|
end
|
52
42
|
|
53
43
|
test "set_version can be called to override the requested version" do
|
54
|
-
get :index, "
|
44
|
+
get :index, "api_version" => "1", "override_version" => 2
|
55
45
|
assert_equal 2, @controller.derived_version
|
56
46
|
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class ParameterStrategyTest < ActionController::TestCase
|
60
|
-
tests RendersController
|
61
|
-
|
62
|
-
setup do
|
63
|
-
VersionCake::Configuration.extraction_strategy = :query_parameter
|
64
|
-
end
|
65
|
-
|
66
|
-
test "render version 1 of the partial based on the parameter _api_version" do
|
67
|
-
get :index, "api_version" => "1"
|
68
|
-
assert_equal @response.body, "index.v1.html.erb"
|
69
|
-
end
|
70
|
-
|
71
|
-
test "render version 2 of the partial based on the parameter _api_version" do
|
72
|
-
get :index, "api_version" => "2"
|
73
|
-
assert_equal @response.body, "index.v2.html.erb"
|
74
|
-
end
|
75
|
-
|
76
|
-
test "render the latest available version (v2) of the partial based on the parameter _api_version" do
|
77
|
-
get :index, "api_version" => "3"
|
78
|
-
assert_equal @response.body, "index.v2.html.erb"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class CustomHeaderStrategyTest < ActionController::TestCase
|
83
|
-
tests RendersController
|
84
|
-
|
85
|
-
setup do
|
86
|
-
VersionCake::Configuration.extraction_strategy = :http_header
|
87
|
-
end
|
88
|
-
|
89
|
-
test "renders version 1 of the partial based on the header API-Version" do
|
90
|
-
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "1"})
|
91
|
-
get :index
|
92
|
-
assert_equal @response.body, "index.v1.html.erb"
|
93
|
-
end
|
94
|
-
|
95
|
-
test "renders version 2 of the partial based on the header API-Version" do
|
96
|
-
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "2"})
|
97
|
-
get :index
|
98
|
-
assert_equal @response.body, "index.v2.html.erb"
|
99
|
-
end
|
100
|
-
|
101
|
-
test "renders the latest available version (v2) of the partial based on the header API-Version" do
|
102
|
-
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "3"})
|
103
|
-
get :index
|
104
|
-
assert_equal @response.body, "index.v2.html.erb"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
class RequestBodyStrategyTest < ActionController::TestCase
|
109
|
-
tests RendersController
|
110
|
-
|
111
|
-
setup do
|
112
|
-
VersionCake::Configuration.extraction_strategy = :request_parameter
|
113
|
-
end
|
114
|
-
|
115
|
-
test "requested version is in the body" do
|
116
|
-
post :index, "api_version" => "2"
|
117
|
-
assert_equal 2, @controller.requested_version
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
class AcceptHeaderStrategyTest < ActionController::TestCase
|
122
|
-
tests RendersController
|
123
|
-
|
124
|
-
setup do
|
125
|
-
VersionCake::Configuration.extraction_strategy = :http_accept_parameter
|
126
|
-
end
|
127
|
-
|
128
|
-
test "render version 1 of the partial based on the header Accept" do
|
129
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=1"})
|
130
|
-
get :index
|
131
|
-
assert_equal @response.body, "index.v1.html.erb"
|
132
|
-
end
|
133
|
-
|
134
|
-
test "render version 2 of the partial based on the header Accept" do
|
135
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=2"})
|
136
|
-
get :index
|
137
|
-
assert_equal @response.body, "index.v2.html.erb"
|
138
|
-
end
|
139
|
-
|
140
|
-
test "render the latest available version (v2) of the partial based on the header Accept" do
|
141
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=3"})
|
142
|
-
get :index
|
143
|
-
assert_equal @response.body, "index.v2.html.erb"
|
144
|
-
end
|
145
|
-
|
146
|
-
test "render the latest version of the partial" do
|
147
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=abc"})
|
148
|
-
get :index
|
149
|
-
assert_equal @response.body, "index.v2.html.erb"
|
150
|
-
end
|
151
|
-
|
152
|
-
test "render the default version version of the partial" do
|
153
|
-
VersionCake::Configuration.default_version = 1
|
154
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=abc"})
|
155
|
-
get :index
|
156
|
-
assert_equal @response.body, "index.v1.html.erb"
|
157
|
-
VersionCake::Configuration.default_version = nil
|
158
|
-
end
|
159
|
-
|
160
|
-
end
|
161
|
-
|
162
|
-
class CustomStrategyTest < ActionController::TestCase
|
163
|
-
tests RendersController
|
164
|
-
|
165
|
-
setup do
|
166
|
-
VersionCake::Configuration.extraction_strategy = lambda { |request| 2 }
|
167
|
-
end
|
168
|
-
|
169
|
-
test "renders version 2 of the partial based on the header Accept" do
|
170
|
-
get :index
|
171
|
-
assert_equal @response.body, "index.v2.html.erb"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
class MultipleStrategyTest < ActionController::TestCase
|
176
|
-
tests RendersController
|
177
|
-
|
178
|
-
setup do
|
179
|
-
VersionCake::Configuration.extraction_strategy = [:http_accept_parameter, :query_parameter]
|
180
|
-
end
|
181
|
-
|
182
|
-
test "renders version 1 of the partial based on the header Accept" do
|
183
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=1"})
|
184
|
-
get :index
|
185
|
-
assert_equal @response.body, "index.v1.html.erb"
|
186
|
-
end
|
187
|
-
|
188
|
-
test "renders the query parameter when accept parameter isn't available" do
|
189
|
-
get :index, "api_version" => "1"
|
190
|
-
assert_equal @response.body, "index.v1.html.erb"
|
191
|
-
end
|
192
|
-
|
193
|
-
test "renders the higher priority accept parameter version" do
|
194
|
-
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=2"})
|
195
|
-
get :index, "api_version" => "1"
|
196
|
-
assert_equal @response.body, "index.v2.html.erb"
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
class UnsupportedVersionTest < ActionController::TestCase
|
201
|
-
tests RendersController
|
202
|
-
|
203
|
-
setup do
|
204
|
-
VersionCake::Configuration.extraction_strategy = :query_parameter
|
205
|
-
end
|
206
47
|
|
207
48
|
test "responds with 404 when the version is larger than the supported version" do
|
208
49
|
assert_raise ActionController::RoutingError do
|
@@ -211,9 +52,14 @@ class UnsupportedVersionTest < ActionController::TestCase
|
|
211
52
|
end
|
212
53
|
|
213
54
|
test "responds with 404 when the version is lower than the latest version, but not an available version" do
|
214
|
-
VersionCake::Configuration.supported_version_numbers = [2,3]
|
215
55
|
assert_raise ActionController::RoutingError do
|
216
|
-
get :index, "api_version" => "
|
56
|
+
get :index, "api_version" => "0"
|
217
57
|
end
|
218
58
|
end
|
59
|
+
|
60
|
+
test "render the default version version of the partial" do
|
61
|
+
VersionCake::Configuration.any_instance.stubs(:default_version => 1)
|
62
|
+
get :index, "api_version" => "abc"
|
63
|
+
assert_equal "template v1", @response.body
|
64
|
+
end
|
219
65
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_controller/test_case'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class StrategyControllerTest < ActionController::TestCase
|
7
|
+
tests RendersController
|
8
|
+
|
9
|
+
setup do
|
10
|
+
@test_cases = YAML.load(File.open(Rails.root.join('fixtures', 'test_cases.yml')))
|
11
|
+
end
|
12
|
+
|
13
|
+
test "test yml test cases" do
|
14
|
+
@test_cases.each do |test_case|
|
15
|
+
request = (test_case['request'] || {})
|
16
|
+
headers = request['headers']
|
17
|
+
params = request['params']
|
18
|
+
method = (request['method'] || "get").to_sym
|
19
|
+
response = test_case['response']
|
20
|
+
@controller.request.stubs(:headers).returns(headers || {})
|
21
|
+
begin
|
22
|
+
send(method, :index, params || {})
|
23
|
+
assert_equal(response, @response.body, custom_message(headers, params, method, response))
|
24
|
+
rescue => e
|
25
|
+
fail(custom_message(headers, params, method, response) + ", but it failed with an exception '#{e.message}'")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def custom_message(headers, params, method, response)
|
31
|
+
data = []
|
32
|
+
data << "headers:#{headers}" if headers
|
33
|
+
data << "params:#{params}" if params
|
34
|
+
"Expected #{data.join(",")} with method #{method} to yield #{response}"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
require './lib/versioncake/cli'
|
3
|
+
|
4
|
+
class CliTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
setup do
|
7
|
+
File.stubs :rename
|
8
|
+
capture_stdout(true)
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
capture_stdout(false)
|
13
|
+
end
|
14
|
+
|
15
|
+
def capture_stdout(flag=true)
|
16
|
+
if flag
|
17
|
+
out = StringIO.new
|
18
|
+
$stdout = out
|
19
|
+
else
|
20
|
+
$stdout = STDOUT
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'it can detect old file names' do
|
25
|
+
renamed_files = VersionCake::Cli.new.migrate('./test/fixtures/templates')
|
26
|
+
assert_equal 2, renamed_files.size
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'it renames old filenames to new filenames' do
|
30
|
+
renamed_files = VersionCake::Cli.new.migrate('./test/fixtures/templates')
|
31
|
+
expected_renamed_files = [
|
32
|
+
['./test/fixtures/templates/v1_extension_scheme.v3.html.erb', './test/fixtures/templates/v1_extension_scheme.html.v3.erb'],
|
33
|
+
['./test/fixtures/templates/v1_extension_scheme.v6.json', './test/fixtures/templates/v1_extension_scheme.json.v6']
|
34
|
+
]
|
35
|
+
assert_empty expected_renamed_files - renamed_files
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'it reports no files changed when new filenames exist' do
|
39
|
+
assert_empty VersionCake::Cli.new.migrate('./test/app/views')
|
40
|
+
end
|
41
|
+
|
42
|
+
test 'it raises if it cannot find the path' do
|
43
|
+
assert_raises ArgumentError do
|
44
|
+
VersionCake::Cli.new.migrate('./a/missing/path')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|