use the scheme and host from x-forward-proto and x-forward-host if they exits and correct the scheme for Location header during image upload

Signed-off-by: yuzou <zouyu7@huawei.com>
This commit is contained in:
yuzou 2015-10-15 16:56:28 +08:00
parent e02a0b0399
commit d724bb1d24
2 changed files with 43 additions and 10 deletions

View file

@ -204,7 +204,9 @@ func (cr clonedRoute) URL(pairs ...string) (*url.URL, error) {
routeURL.Path = routeURL.Path[1:] routeURL.Path = routeURL.Path[1:]
} }
return cr.root.ResolveReference(routeURL), nil url := cr.root.ResolveReference(routeURL)
url.Scheme = cr.root.Scheme
return url, nil
} }
// appendValuesURL appends the parameters to the url. // appendValuesURL appends the parameters to the url.

View file

@ -166,6 +166,11 @@ func TestBuilderFromRequest(t *testing.T) {
request: &http.Request{URL: u, Host: u.Host}, request: &http.Request{URL: u, Host: u.Host},
base: "http://example.com", base: "http://example.com",
}, },
{
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
base: "http://example.com",
},
{ {
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader}, request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
base: "https://example.com", base: "https://example.com",
@ -197,15 +202,26 @@ func TestBuilderFromRequest(t *testing.T) {
} }
for _, testCase := range makeURLBuilderTestCases(builder) { for _, testCase := range makeURLBuilderTestCases(builder) {
url, err := testCase.build() buildURL, err := testCase.build()
if err != nil { if err != nil {
t.Fatalf("%s: error building url: %v", testCase.description, err) t.Fatalf("%s: error building url: %v", testCase.description, err)
} }
expectedURL := tr.base + testCase.expectedPath var expectedURL string
proto, ok := tr.request.Header["X-Forwarded-Proto"]
if !ok {
expectedURL = tr.base + testCase.expectedPath
} else {
urlBase, err := url.Parse(tr.base)
if err != nil {
t.Fatal(err)
}
urlBase.Scheme = proto[0]
expectedURL = urlBase.String() + testCase.expectedPath
}
if url != expectedURL { if buildURL != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL) t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
} }
} }
} }
@ -229,6 +245,11 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
request: &http.Request{URL: u, Host: u.Host}, request: &http.Request{URL: u, Host: u.Host},
base: "http://example.com/prefix/", base: "http://example.com/prefix/",
}, },
{
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
base: "http://example.com/prefix/",
},
{ {
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader}, request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
base: "https://example.com/prefix/", base: "https://example.com/prefix/",
@ -253,15 +274,25 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
} }
for _, testCase := range makeURLBuilderTestCases(builder) { for _, testCase := range makeURLBuilderTestCases(builder) {
url, err := testCase.build() buildURL, err := testCase.build()
if err != nil { if err != nil {
t.Fatalf("%s: error building url: %v", testCase.description, err) t.Fatalf("%s: error building url: %v", testCase.description, err)
} }
var expectedURL string
proto, ok := tr.request.Header["X-Forwarded-Proto"]
if !ok {
expectedURL = tr.base[0:len(tr.base)-1] + testCase.expectedPath
} else {
urlBase, err := url.Parse(tr.base)
if err != nil {
t.Fatal(err)
}
urlBase.Scheme = proto[0]
expectedURL = urlBase.String()[0:len(urlBase.String())-1] + testCase.expectedPath
}
expectedURL := tr.base[0:len(tr.base)-1] + testCase.expectedPath if buildURL != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
if url != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
} }
} }
} }