1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Normalize urls with URI adapter to allow International Domain Names support (#668)

* Normalize urls with URI adapter to allow idns support

* Fix normalization update specs
This commit is contained in:
Nikita Misharin 2019-08-30 17:27:59 +03:00 committed by GitHub
parent b9a54d8f73
commit 54b7949b91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 33 deletions

View file

@ -16,6 +16,7 @@ group :test do
gem 'aruba' gem 'aruba'
gem 'cucumber', '~> 2.3' gem 'cucumber', '~> 2.3'
gem 'webmock' gem 'webmock'
gem 'addressable'
end end
group :development, :test do group :development, :test do

View file

@ -71,7 +71,7 @@ module HTTParty
@path = if uri.is_a?(uri_adapter) @path = if uri.is_a?(uri_adapter)
uri uri
elsif String.try_convert(uri) elsif String.try_convert(uri)
uri_adapter.parse uri uri_adapter.parse(uri).normalize
else else
raise ArgumentError, raise ArgumentError,
"bad argument (expected #{uri_adapter} object or URI string)" "bad argument (expected #{uri_adapter} object or URI string)"
@ -95,9 +95,9 @@ module HTTParty
end end
if path.relative? && path.host if path.relative? && path.host
new_uri = options[:uri_adapter].parse("#{@last_uri.scheme}:#{path}") new_uri = options[:uri_adapter].parse("#{@last_uri.scheme}:#{path}").normalize
elsif path.relative? elsif path.relative?
new_uri = options[:uri_adapter].parse("#{base_uri}#{path}") new_uri = options[:uri_adapter].parse("#{base_uri}#{path}").normalize
else else
new_uri = path.clone new_uri = path.clone
end end
@ -305,7 +305,7 @@ module HTTParty
def handle_host_redirection def handle_host_redirection
check_duplicate_location_header check_duplicate_location_header
redirect_path = options[:uri_adapter].parse last_response['location'] redirect_path = options[:uri_adapter].parse(last_response['location']).normalize
return if redirect_path.relative? || path.host == redirect_path.host return if redirect_path.relative? || path.host == redirect_path.host
@changed_hosts = true @changed_hosts = true
end end

View file

@ -405,18 +405,37 @@ RSpec.describe HTTParty do
end end
describe "uri_adapter" do describe "uri_adapter" do
context 'with Addressable::URI' do
before do
@klass.uri_adapter(Addressable::URI)
end
it 'handles international domains' do
uri = 'http://xn--i-7iqv272g.ws/'
stub_request(:get, uri).to_return(body: 'stuff')
response = @klass.get('http://i❤.ws')
expect(response.parsed_response).to eq('stuff')
expect(response.request.uri.to_s).to eq(uri)
end
end
context 'with custom URI Adaptor' do
require 'forwardable' require 'forwardable'
class CustomURIAdaptor class CustomURIAdaptor
extend Forwardable extend Forwardable
def_delegators :@uri, :userinfo, :relative?, :query, :query=, :scheme, :path, :host, :port def_delegators :@uri, :userinfo, :relative?, :query, :query=, :scheme, :path, :host, :port
def initialize uri def initialize(uri)
@uri = uri @uri = uri
end end
def self.parse uri def self.parse(uri)
new URI.parse uri new(URI.parse(uri))
end
def normalize
self
end end
end end
@ -440,7 +459,7 @@ RSpec.describe HTTParty do
@klass.uri_adapter uri_adapter @klass.uri_adapter uri_adapter
expect(@klass.get(uri).parsed_response).to eq('stuff') expect(@klass.get(uri).parsed_response).to eq('stuff')
end end
end
end end
describe "connection_adapter" do describe "connection_adapter" do