Skip to content

Commit

Permalink
fix(dashboard): no redirect loop with running on root
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Kolář <mail@josefkolar.cz>
  • Loading branch information
thejoeejoee committed Nov 30, 2024
1 parent 5f59344 commit 9ae3676
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server/server_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *ArgoRolloutsServer) staticFileHttpHandler(w http.ResponseWriter, r *htt
requestedURI := path.Clean(r.RequestURI)
rootPath := path.Clean("/" + s.Options.RootPath)

if requestedURI == "/" {
if requestedURI == "/" && rootPath != "/" {
http.Redirect(w, r, rootPath+"/", http.StatusFound)
return
}
Expand Down
34 changes: 21 additions & 13 deletions server/server_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,39 @@ const TestRootPath = "/test-root"

var (
//go:embed static_test/*
staticTestData embed.FS //nolint
mockServer ArgoRolloutsServer
staticTestData embed.FS //nolint
mockServerPrefix ArgoRolloutsServer
mockServerRoot ArgoRolloutsServer
)

func init() {
static = staticTestData
staticBasePath = "static_test"
indexHtmlFile = staticBasePath + "/index.html"
mockServer = mockArgoRolloutServer()
mockServerPrefix = mockArgoRolloutServer(TestRootPath)
mockServerRoot = mockArgoRolloutServer("/")
}

func TestIndexHtmlIsServed(t *testing.T) {
tests := []struct {
server ArgoRolloutsServer
requestPath string
}{
{TestRootPath + "/"},
{TestRootPath + "/index.html"},
{TestRootPath + "/nonsense/../index.html"},
{TestRootPath + "/test-dir/test.css"},
{mockServerPrefix, TestRootPath + "/"},
{mockServerPrefix, TestRootPath + "/index.html"},
{mockServerPrefix, TestRootPath + "/nonsense/../index.html"},
{mockServerPrefix, TestRootPath + "/test-dir/test.css"},

{mockServerRoot, "/"},
{mockServerRoot, "/index.html"},
{mockServerRoot, "/nonsense/../index.html"},
{mockServerRoot, "/test-dir/test.css"},
}
for _, test := range tests {
t.Run(test.requestPath, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.requestPath, nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
test.server.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
Expand All @@ -60,7 +68,7 @@ func TestIndexHtmlIsServed(t *testing.T) {
func TestWhenFileNotFoundSendIndexPageForUiReactRouter(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, TestRootPath+"/namespace-default", nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
mockServerPrefix.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
Expand All @@ -72,7 +80,7 @@ func TestWhenFileNotFoundSendIndexPageForUiReactRouter(t *testing.T) {
func TestSlashWillBeRedirectedToRootPath(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
mockServerPrefix.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
_, err := io.ReadAll(res.Body)
Expand All @@ -95,18 +103,18 @@ func TestInvalidFilesOrHackingAttemptReturn404(t *testing.T) {
t.Run(test.requestPath, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.requestPath, nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
mockServerPrefix.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, res.StatusCode, http.StatusNotFound)
})
}
}

func mockArgoRolloutServer() ArgoRolloutsServer {
func mockArgoRolloutServer(rootPath string) ArgoRolloutsServer {
s := ArgoRolloutsServer{
Options: ServerOptions{
RootPath: TestRootPath,
RootPath: rootPath,
},
}
return s
Expand Down

0 comments on commit 9ae3676

Please sign in to comment.