From 7342b5355f38ce4f56292c6d264b7a647c855b8e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 16 Jul 2020 13:28:29 -0700 Subject: [PATCH] fix: obtain correct location string with DNS style buckets (#10060) closes #10054 --- cmd/api-response.go | 3 +-- cmd/api-response_test.go | 17 ++++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/api-response.go b/cmd/api-response.go index 43e5ffb29..22df12678 100644 --- a/cmd/api-response.go +++ b/cmd/api-response.go @@ -396,8 +396,7 @@ func getObjectLocation(r *http.Request, domains []string, bucket, object string) } // If domain is set then we need to use bucket DNS style. for _, domain := range domains { - if strings.Contains(r.Host, domain) { - u.Host = bucket + "." + r.Host + if strings.HasPrefix(r.Host, bucket+"."+domain) { u.Path = path.Join(SlashSeparator, object) break } diff --git a/cmd/api-response_test.go b/cmd/api-response_test.go index 76c70f198..b113887d2 100644 --- a/cmd/api-response_test.go +++ b/cmd/api-response_test.go @@ -77,7 +77,7 @@ func TestObjectLocation(t *testing.T) { // Server with virtual domain name. { request: &http.Request{ - Host: "mys3.bucket.org", + Host: "mybucket.mys3.bucket.org", Header: map[string][]string{}, }, domains: []string{"mys3.bucket.org"}, @@ -87,7 +87,7 @@ func TestObjectLocation(t *testing.T) { }, { request: &http.Request{ - Host: "mys3.bucket.org", + Host: "mybucket.mys3.bucket.org", Header: map[string][]string{ "X-Forwarded-Scheme": {httpsScheme}, }, @@ -98,11 +98,14 @@ func TestObjectLocation(t *testing.T) { expectedLocation: "https://mybucket.mys3.bucket.org/test/1.txt", }, } - for i, testCase := range testCases { - gotLocation := getObjectLocation(testCase.request, testCase.domains, testCase.bucket, testCase.object) - if testCase.expectedLocation != gotLocation { - t.Errorf("Test %d: expected %s, got %s", i+1, testCase.expectedLocation, gotLocation) - } + for _, testCase := range testCases { + testCase := testCase + t.Run("", func(t *testing.T) { + gotLocation := getObjectLocation(testCase.request, testCase.domains, testCase.bucket, testCase.object) + if testCase.expectedLocation != gotLocation { + t.Errorf("expected %s, got %s", testCase.expectedLocation, gotLocation) + } + }) } }