From 50532d2f67f16f471b9b7f777bda164532aaba34 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 Fixes #91 and closes #171 --- CHANGELOG.md | 5 +++ README.md | 1 - lib/route_translator.rb | 4 +-- 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 | 34 ------------------- test/integration/host_locales_test.rb | 34 ++++++++++++------- 9 files changed, 41 insertions(+), 74 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 f784a25f..cf23d7bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 10.0.0.alpha1 / 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 + ## 9.0.0 / 2020-11-07 * [ENHANCEMENT] Check for `empty?` instead of `any?` on available_locales array diff --git a/README.md b/README.md index 30b33680..e6cb4059 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,6 @@ end | `host_locales` | Sets `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details | `{}` | | `locale_param_key` | The param key used to set the locale to the newly generated routes | `:locale` | | `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. `->(locale) { locale.to_s.upcase }` | `false` | -| `verify_host_path_consistency` | Forces a matching of the host associated locale with the translated path locale as part of the route definition. By default, if you use different hosts to translate your application, all translated paths will work on all hosts | `false` | ### Host-based Locale diff --git a/lib/route_translator.rb b/lib/route_translator.rb index aafe8166..cc9c7dbd 100644 --- a/lib/route_translator.rb +++ b/lib/route_translator.rb @@ -6,7 +6,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 @@ -23,8 +22,7 @@ module RouteTranslator hide_locale: false, host_locales: {}, locale_param_key: :locale, - locale_segment_proc: false, - verify_host_path_consistency: false + locale_segment_proc: false }.freeze Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys) diff --git a/lib/route_translator/extensions/route_set.rb b/lib/route_translator/extensions/route_set.rb index 2e863d61..d81e6ae0 100644 --- a/lib/route_translator/extensions/route_set.rb +++ b/lib/route_translator/extensions/route_set.rb @@ -33,8 +33,8 @@ def translate_mapping(locale, route_set, translated_options, translated_path_ast options: scope[:options] ? scope[:options].merge(translated_options) : translated_options } - if RouteTranslator.config.verify_host_path_consistency - scope_params[:blocks].push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale) + if RouteTranslator.config.host_locales.present? + scope_params[:blocks].push RouteTranslator::Host.lambdas_for_locale(locale) end ::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options diff --git a/lib/route_translator/host.rb b/lib/route_translator/host.rb index e5e1a65c..2393991f 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&.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 69f401e4..b2e9ae2a 100644 --- a/lib/route_translator/version.rb +++ b/lib/route_translator/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RouteTranslator - VERSION = '9.0.0' + VERSION = '10.0.0.alpha1' 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 9f4c01e7..00000000 --- a/test/integration/host_locale_path_verify_consistency_test.rb +++ /dev/null @@ -1,34 +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' - Rails.application.reload_routes! - end - - def teardown - teardown_config - Rails.application.reload_routes! - end - - def test_host_path_consistency - host! 'www.testapp.es' - get '/dummy' - assert_response :success - - get Addressable::URI.normalize_component('/манекен') - assert_response :not_found - - host! 'ru.testapp.com' - get '/dummy' - assert_response :not_found - - get Addressable::URI.normalize_component('/манекен') - assert_response :success - end -end diff --git a/test/integration/host_locales_test.rb b/test/integration/host_locales_test.rb index 13f04ef8..e81347b7 100644 --- a/test/integration/host_locales_test.rb +++ b/test/integration/host_locales_test.rb @@ -36,23 +36,11 @@ def test_explicit_path assert_equal 'es', @response.body assert_response :success - # ru route on es com - host! 'www.testapp.es' - get Addressable::URI.normalize_component('/ru/манекен') - assert_equal 'ru', @response.body - assert_response :success - # native ru route on ru com host! 'ru.testapp.com' get Addressable::URI.normalize_component('/манекен') 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 @@ -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 Addressable::URI.normalize_component('/ru/манекен') + 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 '/dummy' + assert_response :not_found + + # unprefixed ru route on es com + host! 'www.testapp.es' + get Addressable::URI.normalize_component('/манекен') + assert_response :not_found + end end