Skip to content

Commit

Permalink
DeviseTokenAuth::Url.generate should accept relate path
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqing committed Jan 24, 2019
1 parent 1254483 commit cac9e03
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/devise_token_auth/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ module DeviseTokenAuth::Url

def self.generate(url, params = {})
uri = URI(url)
query_params = Hash[URI.decode_www_form(uri.query || '')].merge(params)
uri.query = URI.encode_www_form(query_params)

res = "#{uri.scheme}://#{uri.host}"
res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
res += uri.path.to_s if uri.path
query = [uri.query, params.to_query].reject(&:blank?).join('&')
res += "?#{query}"
res += "##{uri.fragment}" if uri.fragment

res
uri.to_s
end

def self.whitelisted?(url)
Expand Down
6 changes: 6 additions & 0 deletions test/lib/devise_token_auth/url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DeviseTokenAuth::UrlTest < ActiveSupport::TestCase
assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?client_id=123#fragment'
end

test 'relative path should be returned if url is relative' do
params = { client_id: 123 }
url = '/foobar'
assert_equal DeviseTokenAuth::Url.send(:generate, url, params), '/foobar?client_id=123'
end

describe 'with existing query params' do
test 'should preserve existing query params' do
url = 'http://example.com?a=1'
Expand Down

0 comments on commit cac9e03

Please sign in to comment.