-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verify_host_path_consistency option (#167)
When using different hosts to identify different locales, it's desirable to ensure that only coherent url's work on the application. Mismatching between the locale assigned to a domain (ej: example.es) and the language of the path (ej: '/cesta-de-la-compra') should be considered 404's. This commit adds a configuration option to enable this behavior.
- Loading branch information
1 parent
406bc9c
commit 6970384
Showing
7 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module RouteTranslator | ||
module HostPathConsistencyLambdas | ||
class << self | ||
private | ||
|
||
def lambdas | ||
@lambdas ||= {} | ||
end | ||
|
||
def sanitize_locale(locale) | ||
locale.to_s.gsub('native_', '') | ||
end | ||
end | ||
|
||
module_function | ||
|
||
def for_locale(locale) | ||
sanitized_locale = sanitize_locale(locale) | ||
|
||
lambdas[sanitized_locale] ||= ->(req) { sanitized_locale == RouteTranslator::Host.locale_from_host(req.host).to_s } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/integration/host_locale_path_verify_consistency_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path('../../test_helper', __FILE__) | ||
|
||
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 URI.escape('/манекен') | ||
assert_response :not_found | ||
|
||
host! 'ru.testapp.com' | ||
get '/dummy' | ||
assert_response :not_found | ||
|
||
get URI.escape('/манекен') | ||
assert_response :success | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters