Skip to content

Commit

Permalink
fix: Removing lang codes with path pattern only removes from the firs…
Browse files Browse the repository at this point in the history
…t path segment (#221)

* fix: Removing lang codes with path pattern only removes from the first path segment

* lint

* Update regex

Co-authored-by: Andrew Wilson <[email protected]>
  • Loading branch information
bstewart00 and Andrew Wilson authored Nov 16, 2022
1 parent 2945e69 commit 0f6df67
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/wovnrb/url_language_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def remove_lang_from_uri_component(uri, lang)
rp = Regexp.new("(^|(//))#{lang_code}\\.", 'i')
uri.sub(rp, '\1')
when 'path'
uri.sub(%r{/#{lang_code}(/|$)}, '/')
# ^(.*://|//)? 1: schema (optional) like https://
# ([^/]*/)? 2: host (optional) like wovn.io, with trailing '/' (mandatory)
# (/|$) 3: path or end-of-string
lang_code_pattern = %r{^(.*://|//)?([^/]*/)?#{lang_code}(/|$)}
uri.sub(lang_code_pattern, '\1\2')
else
raise RuntimeError("Invalid URL pattern: #{@store.settings['url_pattern']}")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '3.7.1'.freeze
VERSION = '3.7.2'.freeze
end
15 changes: 9 additions & 6 deletions test/lib/url_language_switcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -876,17 +876,20 @@ def test_remove_lang_subdomain_with_custom_lang_alias
def test_remove_lang_path
settings = Wovnrb.get_settings
store = Wovnrb.get_store(settings)
url_lang_switcher = UrlLanguageSwitcher.new(store)
sut = UrlLanguageSwitcher.new(store)

keys = Wovnrb::Lang::LANG.keys
assert_equal(77, keys.size)

keys.each do |key|
uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component("wovn.io/#{key}", key)
assert_equal('wovn.io/', uri_without_scheme)

uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component("https://wovn.io/#{key}/", key)
assert_equal('https://wovn.io/', uri_with_scheme)
assert_equal('/', sut.remove_lang_from_uri_component("/#{key}", key))
assert_equal("/dir/#{key}/page.html", sut.remove_lang_from_uri_component("/#{key}/dir/#{key}/page.html", key))
assert_equal('?query', sut.remove_lang_from_uri_component('?query', key))
assert_equal('wovn.io/', sut.remove_lang_from_uri_component("wovn.io/#{key}", key))
assert_equal("wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io/#{key}/dir/#{key}/page.html", key))
assert_equal("wovn.io:5000/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io:5000/#{key}/dir/#{key}/page.html", key))
assert_equal('https://wovn.io/', sut.remove_lang_from_uri_component("https://wovn.io/#{key}/", key))
assert_equal("https://wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("https://wovn.io/#{key}/dir/#{key}/page.html", key))
end
end

Expand Down

0 comments on commit 0f6df67

Please sign in to comment.