Merge pull request #1450 from influxdata/bugfix/tr-chronograf-basepath
Fix infinite spinner with /chronograf basepathpull/10616/head
commit
ada57b075e
|
@ -1,3 +1,12 @@
|
||||||
|
## v1.3.1 [unreleased]
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
1. [#1450](https://github.com/influxdata/chronograf/pull/1450): Fix infinite spinner when using "/chronograf" as a basepath
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
### UI Improvements
|
||||||
|
|
||||||
## v1.3.0 [2017-05-09]
|
## v1.3.0 [2017-05-09]
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
|
@ -16,7 +16,8 @@ func (i *interceptingResponseWriter) WriteHeader(status int) {
|
||||||
if status >= 300 && status < 400 {
|
if status >= 300 && status < 400 {
|
||||||
location := i.ResponseWriter.Header().Get("Location")
|
location := i.ResponseWriter.Header().Get("Location")
|
||||||
if u, err := url.Parse(location); err == nil && !u.IsAbs() {
|
if u, err := url.Parse(location); err == nil && !u.IsAbs() {
|
||||||
if !strings.HasPrefix(location, i.Prefix) {
|
hasPrefix := strings.HasPrefix(u.Path, i.Prefix)
|
||||||
|
if !hasPrefix || (hasPrefix && !strings.HasPrefix(u.Path[len(i.Prefix):], i.Prefix)) {
|
||||||
i.ResponseWriter.Header().Set("Location", path.Join(i.Prefix, location)+"/")
|
i.ResponseWriter.Header().Set("Location", path.Join(i.Prefix, location)+"/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var prefixingRedirectTests = []struct {
|
||||||
|
CaseName string
|
||||||
|
RedirectTarget string
|
||||||
|
Prefix string
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"ChronografBasepath",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
"/chronograf",
|
||||||
|
"/chronograf/chronograf/v1/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"DifferentBasepath",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
"/delorean",
|
||||||
|
"/delorean/chronograf/v1/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"TrailingSlashPrefix",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
"/delorean/",
|
||||||
|
"/delorean/chronograf/v1/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"NoPrefix",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
"",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"SlashPrefix",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
"/",
|
||||||
|
"/chronograf/v1/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AlreadyPrefixed",
|
||||||
|
"/chronograf/chronograf/v1/",
|
||||||
|
"/chronograf",
|
||||||
|
"/chronograf/chronograf/v1/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_PrefixingRedirector(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
for _, p := range prefixingRedirectTests {
|
||||||
|
t.Run(p.CaseName, func(subt *testing.T) {
|
||||||
|
hf := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
rw.Header().Set("Location", p.RedirectTarget)
|
||||||
|
rw.WriteHeader(http.StatusTemporaryRedirect)
|
||||||
|
})
|
||||||
|
pr := PrefixedRedirect(p.Prefix, hf)
|
||||||
|
|
||||||
|
ts := httptest.NewServer(pr)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
hc := http.Client{
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mockBody := strings.NewReader("")
|
||||||
|
req, _ := http.NewRequest("GET", ts.URL, mockBody)
|
||||||
|
|
||||||
|
resp, err := hc.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
subt.Fatal("Unexpected http err:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := p.Expected
|
||||||
|
if loc := resp.Header.Get("Location"); loc != expected {
|
||||||
|
subt.Fatal("Unexpected redirected location. Expected:", expected, "Actual:", loc)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue