From 408aa244a2dbcc6aef029ff029df468346412ebb Mon Sep 17 00:00:00 2001 From: Nate Brustein Date: Tue, 27 Oct 2015 09:27:38 -0400 Subject: [PATCH] fix(url): preserve query parameters when building urls --- lib/devise_token_auth/url.rb | 5 +++-- test/lib/devise_token_auth/url_test.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/devise_token_auth/url.rb b/lib/devise_token_auth/url.rb index 172a22fd6..fa948149b 100644 --- a/lib/devise_token_auth/url.rb +++ b/lib/devise_token_auth/url.rb @@ -5,8 +5,9 @@ def self.generate(url, params = {}) res = "#{uri.scheme}://#{uri.host}" res += ":#{uri.port}" if (uri.port and uri.port != 80 and uri.port != 443) - res += "#{uri.path}" if uri.path - res += "?#{params.to_query}" + res += "#{uri.path}" if uri.path + query = [uri.query, params.to_query].reject(&:blank?).join('&') + res += "?#{query}" res += "##{uri.fragment}" if uri.fragment return res diff --git a/test/lib/devise_token_auth/url_test.rb b/test/lib/devise_token_auth/url_test.rb index 012dbf076..e668140bf 100644 --- a/test/lib/devise_token_auth/url_test.rb +++ b/test/lib/devise_token_auth/url_test.rb @@ -7,5 +7,22 @@ class DeviseTokenAuth::UrlTest < ActiveSupport::TestCase url = 'http://example.com#fragment' assert_equal DeviseTokenAuth::Url.send(:generate, url, params), "http://example.com?client_id=123#fragment" end + + describe 'with existing query params' do + test 'should preserve existing query params' do + url = 'http://example.com?a=1' + assert_equal DeviseTokenAuth::Url.send(:generate, url), "http://example.com?a=1" + end + + test 'should marge existing query params with new ones' do + params = {client_id: 123} + url = 'http://example.com?a=1' + assert_equal DeviseTokenAuth::Url.send(:generate, url, params), "http://example.com?a=1&client_id=123" + end + + + end + + end end \ No newline at end of file