From 1296a12a6d1cad13549d52e18d33bc1338ac508f Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 23 Jun 2023 22:19:49 +0200 Subject: [PATCH] Language Selector: Prevent duplicate `locale=` keys --- app/helpers/authenticated_url_helper.rb | 2 +- spec/helpers/authenticated_url_helper_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/authenticated_url_helper_spec.rb diff --git a/app/helpers/authenticated_url_helper.rb b/app/helpers/authenticated_url_helper.rb index f549db09..d33d852e 100644 --- a/app/helpers/authenticated_url_helper.rb +++ b/app/helpers/authenticated_url_helper.rb @@ -86,7 +86,7 @@ module AuthenticatedUrlHelper parsed_url = URI.parse url # Add the given parameters to the query string - query_params = CGI.parse(parsed_url.query || '') + query_params = CGI.parse(parsed_url.query || '').with_indifferent_access query_params.merge!(parameters) # Add the query string back to the URL diff --git a/spec/helpers/authenticated_url_helper_spec.rb b/spec/helpers/authenticated_url_helper_spec.rb new file mode 100644 index 00000000..bc172032 --- /dev/null +++ b/spec/helpers/authenticated_url_helper_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe AuthenticatedUrlHelper do + describe '#add_query_parameters' do + it 'adds the given parameters to the given url' do + expect(described_class.add_query_parameters(root_url, {foo: 'bar'})).to eq(root_url(foo: 'bar')) + end + + it 'does not duplicate existing parameters' do + expect(described_class.add_query_parameters(root_url(foo: 'bar'), {foo: 'baz'})).to eq(root_url(foo: 'baz')) + end + end +end