1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Fixes domain name duplication in url

When a bucket url like bucket.s3.amazonaws.com with bucket name bucket
is present in params passed into request_params method, it
should not dupplicate the subdomain.
current: bucket.bucket.s3.amazonaws.com
fix: bucket.s3.amazonaws.com

Fix test failure due to Hash#slice not present for ruby 2.3
This commit is contained in:
Midhun Krishna 2021-02-09 15:26:51 +05:30
parent c7a11ba377
commit 0d5466549e
3 changed files with 116 additions and 3 deletions

View file

@ -298,6 +298,8 @@ module Fog
host = params.fetch(:cname, bucket_name)
elsif path_style
path = bucket_to_path bucket_name, path
elsif host.start_with?("#{bucket_name}.")
# no-op
else
host = [bucket_name, host].join('.')
end

View file

@ -1,4 +1,117 @@
Shindo.tests("Storage[:aws] | directory", ["aws"]) do
tests('Fog::Storage[:aws]', "#request_params") do
def slice(hash, *args)
hash.select { |k, _| args.include?(k) }
end
instance = Fog::Storage[:aws]
method = instance.method(:request_params)
params = {bucket_name: 'profile-uploads', host: 'profile-uploads.s3.us-west-2.amazonaws.com'}
tests("given #{params}, request_params[:host]").returns("profile-uploads.s3.us-west-2.amazonaws.com") do
method.call(params)[:host]
end
params = {bucket_name: 'profile-uploads.johnsmith.net', cname: 'profile-uploads.johnsmith.net', virtual_host: true}
tests("given #{params}, request_params[:host]").returns("profile-uploads.johnsmith.net") do
method.call(params)[:host]
end
params = {bucket_name: 'profile-uploads.johnsmith.net', cname: 'profile-uploads.johnsmith.net', virtual_host: false}
tests("given #{params}, request_params[:host], request_params[:path]").
returns({host: "s3.amazonaws.com", path: "/profile-uploads.johnsmith.net/"}) do
slice(method.call(params), :host, :path)
end
params = {bucket_name: 'profile-uploads.johnsmith.net', bucket_cname: 'profile-uploads.johnsmith.net'}
tests("given #{params}, request_params[:host]").returns("profile-uploads.johnsmith.net") do
method.call(params)[:host]
end
params = {bucket_name: 'profile-uploads'}
tests("given #{params}, request_params[:path], request_params[:host]").
returns({path: "/", host: "profile-uploads.s3.amazonaws.com"}) do
slice(method.call(params), :path, :host)
end
params = {bucket_name: 'profile-uploads', path_style: true}
tests("given #{params}, request_params[:path], request_params[:host]").
returns({path: "/profile-uploads/", host: "s3.amazonaws.com"}) do
slice(method.call(params), :path, :host)
end
params = {bucket_name: 'profile-uploads', path_style: false}
tests("given #{params}, request_params[:path], request_params[:host]").
returns({path: "/", host: "profile-uploads.s3.amazonaws.com"}) do
slice(method.call(params), :path, :host)
end
params = {scheme: 'https', bucket_name: 'profile.uploads', path_style: false}
tests("given #{params}, request_params[:path], request_params[:host]").
returns(path: "/profile.uploads/", host: "s3.amazonaws.com") do
slice(method.call(params), :path, :host)
end
params = {:headers=>{:"Content-Type"=>"application/json"}}
tests("given #{params}, request_params[:headers]").returns({:"Content-Type"=>"application/json"}) do
method.call(params)[:headers]
end
params = {headers: {}}
tests("given #{params}, request_params[:headers]").returns({}) do
method.call(params)[:headers]
end
params = {scheme: 'http'}
tests("given #{params}, request_params[:scheme]").returns('http') do
method.call(params)[:scheme]
end
params = {}
tests("given #{params}, request_params[:scheme]").returns('https') do
method.call(params)[:scheme]
end
params = {scheme: 'http', port: 8080}
tests("given #{params} (default scheme), request_params[:port]").returns(8080) do
method.call(params)[:port]
end
params = {scheme: 'https', port: 443}
tests("given #{params}, request_params[:port]").returns(nil) do
method.call(params)[:port]
end
params = {}
tests("given #{params}, request_params[:host]").returns("s3.amazonaws.com") do
method.call(params)[:host]
end
params = {region: 'us-east-1'}
tests("given #{params}, request_params[:host]").returns("s3.amazonaws.com") do
method.call(params)[:host]
end
params = {region: 'us-west-2'}
tests("given #{params}, request_params[:host]").returns("s3.us-west-2.amazonaws.com") do
method.call(params)[:host]
end
params= {region: 'us-east-1', host: 's3.us-west-2.amazonaws.com'}
tests("given #{params}, request_params[:host]").returns("s3.us-west-2.amazonaws.com") do
method.call(params)[:host]
end
params = {object_name: 'image.png'}
tests("given #{params}, request_params[:host]").returns("/image.png") do
method.call(params)[:path]
end
params = {object_name: 'image.png', path: '/images/image.png'}
tests("given #{params}, request_params[:host]").returns("/images/image.png") do
method.call(params)[:path]
end
end
directory_attributes = {
:key => uniq_id('fogdirectorytests')
@ -85,7 +198,5 @@ Shindo.tests("Storage[:aws] | directory", ["aws"]) do
@instance.versioning?
end
end
end
end

View file

@ -158,7 +158,7 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :IndexDocument => 'index.html')
end
tests("#put_bucket_website('#{@aws_bucket_name}', :RedirectAllRequestsTo => 'redirect.example..com')").succeeds do
tests("#put_bucket_website('#{@aws_bucket_name}', :RedirectAllRequestsTo => 'redirect.example.com')").succeeds do
Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :RedirectAllRequestsTo => 'redirect.example.com')
end