Skip to content

Commit

Permalink
Merge pull request lynndylanhurley#421 from nbrustein/preserve-params…
Browse files Browse the repository at this point in the history
…-in-url-generate

fix(url): preserve query parameters when building urls
  • Loading branch information
booleanbetrayal committed Oct 27, 2015
2 parents 728df21 + 408aa24 commit 600df95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/devise_token_auth/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/lib/devise_token_auth/url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 600df95

Please sign in to comment.