From b1a9ac0d3723fb1582cce6f691f348da89c7bc95 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 1 Jul 2017 16:21:51 +0200 Subject: [PATCH] Integrate consistency lambdas into host Fix #91, #171 --- CHANGELOG.md | 6 +++ README.md | 4 -- lib/route_translator.rb | 6 +-- lib/route_translator/extensions/route_set.rb | 4 +- lib/route_translator/host.rb | 10 ++++ .../host_path_consistency_lambdas.rb | 21 --------- lib/route_translator/version.rb | 2 +- ...ost_locale_path_verify_consistency_test.rb | 35 -------------- test/integration/host_locales_test.rb | 46 +++++++++++-------- test/support/configuration_helper.rb | 1 - 10 files changed, 49 insertions(+), 86 deletions(-) delete mode 100644 lib/route_translator/host_path_consistency_lambdas.rb delete mode 100644 test/integration/host_locale_path_verify_consistency_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 76f33c26..dbe7a33e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 6.0.0.develop / unreleased + +* [BUGFIX] Verify host path consistency by default ([#91](https://github.com/enriclluelles/route_translator/issues/91), [#171](https://github.com/enriclluelles/route_translator/issues/171)) +* [FEATURE] Remove the option to verify host path consistency +* [ENHANCEMENT] Update development dependencies + ## 5.5.1 / 2017-11-14 * [BUGFIX] Change spec to reflect Rails 5.1.3 change in url generation ([#172](https://github.com/enriclluelles/route_translator/issues/172)) diff --git a/README.md b/README.md index 8ff12f92..2d6bef78 100644 --- a/README.md +++ b/README.md @@ -284,10 +284,6 @@ end * **locale_segment_proc** The locale segment of the url will by default be `locale.to_s.downcase` You can supply your own mechanism via a Proc that takes `locale` as an argument, e.g. `config.locale_segment_proc = ->(locale) { locale.to_s.upcase }` -* **verify_host_path_consistency** - By default, if you use different hosts to translate your application, all translated paths will work on all hosts. Set this option to `true` to force - a matching of the host associated locale with the translated path locale as part of the route definition. - Defaults to `false`. ### Host-based Locale diff --git a/lib/route_translator.rb b/lib/route_translator.rb index 9494ce55..4112aa51 100644 --- a/lib/route_translator.rb +++ b/lib/route_translator.rb @@ -5,7 +5,6 @@ require 'route_translator/extensions' require 'route_translator/translator' require 'route_translator/host' -require 'route_translator/host_path_consistency_lambdas' require 'route_translator/locale_sanitizer' module RouteTranslator @@ -16,7 +15,7 @@ module RouteTranslator Configuration = Struct.new(:available_locales, :disable_fallback, :force_locale, :hide_locale, :host_locales, :generate_unlocalized_routes, :generate_unnamed_unlocalized_routes, :locale_param_key, - :locale_segment_proc, :verify_host_path_consistency) + :locale_segment_proc) class << self private @@ -42,11 +41,10 @@ def config(&block) @config.generate_unnamed_unlocalized_routes ||= false @config.locale_param_key ||= :locale @config.locale_segment_proc ||= nil - @config.verify_host_path_consistency ||= false yield @config if block - resolve_host_locale_config_conflicts unless @config.host_locales.empty? + resolve_host_locale_config_conflicts if @config.host_locales.present? @config end diff --git a/lib/route_translator/extensions/route_set.rb b/lib/route_translator/extensions/route_set.rb index a7e858f1..dc1b4263 100644 --- a/lib/route_translator/extensions/route_set.rb +++ b/lib/route_translator/extensions/route_set.rb @@ -32,8 +32,8 @@ def translate_mapping(locale, route_set, translated_options, translated_path_ast blocks = scope[:blocks] ? scope[:blocks].dup : [] - if RouteTranslator.config.verify_host_path_consistency - blocks.push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale) + if RouteTranslator.config.host_locales.present? + blocks.push RouteTranslator::Host.lambdas_for_locale(locale) end ::ActionDispatch::Routing::Mapper::Mapping.new(route_set, translated_path_ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, blocks, via, translated_options_constraints, anchor, options) diff --git a/lib/route_translator/host.rb b/lib/route_translator/host.rb index 747cb9b3..81051c86 100644 --- a/lib/route_translator/host.rb +++ b/lib/route_translator/host.rb @@ -5,6 +5,10 @@ module Host class << self private + def lambdas + @lambdas ||= {} + end + def regex_for(host_string) escaped = Regexp.escape(host_string).gsub('\*', '.*?').gsub('\.', '\.?') Regexp.new("^#{escaped}$", Regexp::IGNORECASE) @@ -28,5 +32,11 @@ def locale_from_host(host) locales &= I18n.available_locales (locales.first || I18n.default_locale).to_sym end + + def lambdas_for_locale(locale) + sanitized_locale = RouteTranslator::LocaleSanitizer.sanitize(locale) + + lambdas[sanitized_locale] ||= ->(req) { sanitized_locale == RouteTranslator::Host.locale_from_host(req.host).to_s } + end end end diff --git a/lib/route_translator/host_path_consistency_lambdas.rb b/lib/route_translator/host_path_consistency_lambdas.rb deleted file mode 100644 index d292b282..00000000 --- a/lib/route_translator/host_path_consistency_lambdas.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module RouteTranslator - module HostPathConsistencyLambdas - class << self - private - - def lambdas - @lambdas ||= {} - end - end - - module_function - - def for_locale(locale) - sanitized_locale = RouteTranslator::LocaleSanitizer.sanitize(locale) - - lambdas[sanitized_locale] ||= ->(req) { sanitized_locale == RouteTranslator::Host.locale_from_host(req.host).to_s } - end - end -end diff --git a/lib/route_translator/version.rb b/lib/route_translator/version.rb index ab517481..78a5925a 100644 --- a/lib/route_translator/version.rb +++ b/lib/route_translator/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RouteTranslator - VERSION = '5.5.1'.freeze + VERSION = '6.0.0.develop'.freeze end diff --git a/test/integration/host_locale_path_verify_consistency_test.rb b/test/integration/host_locale_path_verify_consistency_test.rb deleted file mode 100644 index 65a11a2f..00000000 --- a/test/integration/host_locale_path_verify_consistency_test.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'test_helper' - -class HostLocalePathVerifyConsistencyTest < ActionDispatch::IntegrationTest - include RouteTranslator::ConfigurationHelper - - def setup - config_verify_host_path_consistency true - config_host_locales '*.es' => 'es', 'ru.*.com' => 'ru' - Dummy::Application.reload_routes! - end - - def teardown - config_verify_host_path_consistency false - config_host_locales - Dummy::Application.reload_routes! - end - - def test_host_path_consistency - host! 'www.testapp.es' - get '/dummy' - assert_response :success - - get "/#{CGI.escape('манекен')}" - assert_response :not_found - - host! 'ru.testapp.com' - get '/dummy' - assert_response :not_found - - get "/#{CGI.escape('манекен')}" - assert_response :success - end -end diff --git a/test/integration/host_locales_test.rb b/test/integration/host_locales_test.rb index c9042f2b..382b64d5 100644 --- a/test/integration/host_locales_test.rb +++ b/test/integration/host_locales_test.rb @@ -16,13 +16,13 @@ def teardown end def test_root_path - ## root of es com + # root of es com host! 'www.testapp.es' get '/' assert_equal 'es', @response.body assert_response :success - ## root of ru com + # root of ru com host! 'ru.testapp.com' get '/' assert_equal 'ru', @response.body @@ -30,39 +30,27 @@ def test_root_path end def test_explicit_path - ## native es route on es com + # native es route on es com host! 'www.testapp.es' get '/dummy' assert_equal 'es', @response.body assert_response :success - ## ru route on es com - host! 'www.testapp.es' - get "/ru/#{CGI.escape('манекен')}" - assert_equal 'ru', @response.body - assert_response :success - - ## native ru route on ru com + # native ru route on ru com host! 'ru.testapp.com' get "/#{CGI.escape('манекен')}" assert_equal 'ru', @response.body assert_response :success - - ## es route on ru com - host! 'ru.testapp.com' - get '/es/dummy' - assert_equal 'es', @response.body - assert_response :success end def test_generated_path - ## native es route on es com + # native es route on es com host! 'www.testapp.es' get '/native' assert_equal '/mostrar', @response.body assert_response :success - ## native ru route on ru com + # native ru route on ru com host! 'ru.testapp.com' get '/native' assert_equal "/#{CGI.escape('показывать')}", @response.body @@ -75,4 +63,26 @@ def test_preserve_i18n_locale assert_equal :en, I18n.locale end + + def test_non_native_path + # ru route on es com + host! 'www.testapp.es' + get "/ru/#{CGI.escape('манекен')}" + assert_response :not_found + + # es route on ru com + host! 'ru.testapp.com' + get '/es/dummy' + assert_response :not_found + + # unprefixed es route on ru com + host! 'ru.testapp.com' + get "/#{CGI.escape('dummy')}" + assert_response :not_found + + # unprefixed ru route on es com + host! 'www.testapp.es' + get "/#{CGI.escape('манекен')}" + assert_response :not_found + end end diff --git a/test/support/configuration_helper.rb b/test/support/configuration_helper.rb index d796a5df..51869323 100644 --- a/test/support/configuration_helper.rb +++ b/test/support/configuration_helper.rb @@ -15,7 +15,6 @@ def config_reset config_default_locale_settings :en config_host_locales {} config_locale_segment_proc false - config_verify_host_path_consistency false BOOLEAN_OPTIONS.each do |option, default_value| send(:"config_#{option}", default_value)