Files
deploy/server/rootpath_test.go
T
zeekayandHanzo Dev 84ee9fa0ce cd: kill the argo naming — binaries, packages, identifiers
Every command is cd-*: cd, cd-server, cd-repo-server,
cd-application-controller, cd-applicationset-controller, cd-cmp-server,
cd-commit-server, cd-dex, cd-git-ask-pass, cd-k8s-auth, cd-notification.
util/argo is util/cd; util/notification/argocd is util/notification/cd.

Preserved deliberately: github.com/argoproj/argocd-example-apps is an
EXTERNAL repo used as an e2e fixture, and notifications-engine + pkg/v2 are
real dependencies. Renaming a URL we do not own would break the tests.

Two bugs I made and caught, both from a careless sed:
- s/argo/cd/g turned 'argocd' into 'cdcd' in two directory names
- renaming the package declaration left every call site saying argo.X

go build ./... exit 0.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 16:00:25 -07:00

129 lines
4.7 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// TestWithRootPathEmptyRootPath tests that withRootPath returns the original handler when RootPath is empty
func TestWithRootPathEmptyRootPath(t *testing.T) {
t.Parallel()
// Create a simple handler
originalHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Create a server with empty RootPath
server := &ArgoCDServer{
ArgoCDServerOpts: ArgoCDServerOpts{
RootPath: "",
},
}
// Call withRootPath
handler := withRootPath(originalHandler, server)
// Verify that the returned handler is the original handler
// Since we can't directly compare function references, we'll use a type assertion
_, isServeMux := handler.(*http.ServeMux)
assert.False(t, isServeMux, "When RootPath is empty, withRootPath should return the original handler, not a ServeMux")
}
// TestWithRootPathNonEmptyRootPath tests that withRootPath returns a ServeMux when RootPath is not empty
func TestWithRootPathNonEmptyRootPath(t *testing.T) {
t.Parallel()
// Create a simple handler
originalHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Create a server with non-empty RootPath
server := &ArgoCDServer{
ArgoCDServerOpts: ArgoCDServerOpts{
RootPath: "/cd",
},
}
// Call withRootPath
handler := withRootPath(originalHandler, server)
// Verify that the returned handler is a ServeMux
_, isServeMux := handler.(*http.ServeMux)
assert.True(t, isServeMux, "When RootPath is not empty, withRootPath should return a ServeMux")
}
// TestNewRedirectServerEmptyRootPath tests that newRedirectServer correctly handles empty rootPath
func TestNewRedirectServerEmptyRootPath(t *testing.T) {
t.Parallel()
// Call newRedirectServer with empty rootPath
server := newRedirectServer(8080, "")
// Verify the server address
assert.Equal(t, "localhost:8080", server.Addr, "When rootPath is empty, server address should be 'localhost:8080'")
// Test the redirect handler
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/applications", http.NoBody)
req.Host = "example.com:8080"
w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, req)
// Verify the redirect URL
assert.Equal(t, http.StatusTemporaryRedirect, w.Code, "Should return a 307 Temporary Redirect status code")
redirectURL := w.Header().Get("Location")
expectedURL := "https://example.com:8080/applications"
assert.Equal(t, expectedURL, redirectURL, "Redirect URL should not include rootPath when rootPath is empty")
}
// TestNewRedirectServerNonEmptyRootPath tests that newRedirectServer correctly handles non-empty rootPath
func TestNewRedirectServerNonEmptyRootPath(t *testing.T) {
t.Parallel()
// Call newRedirectServer with non-empty rootPath
server := newRedirectServer(8080, "/cd")
// Verify the server address
assert.Equal(t, "localhost:8080/cd", server.Addr, "When rootPath is '/cd', server address should be 'localhost:8080/cd'")
// Test the redirect handler
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/applications", http.NoBody)
req.Host = "example.com:8080"
w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, req)
// Verify the redirect URL
assert.Equal(t, http.StatusTemporaryRedirect, w.Code, "Should return a 307 Temporary Redirect status code")
redirectURL := w.Header().Get("Location")
expectedURL := "https://example.com:8080/cd/applications"
assert.Equal(t, expectedURL, redirectURL, "Redirect URL should include rootPath when rootPath is not empty")
}
// TestNewRedirectServerRootPathDuplication tests that newRedirectServer does not duplicate rootPath in the redirect URL
func TestNewRedirectServerRootPathDuplication(t *testing.T) {
t.Parallel()
// Call newRedirectServer with non-empty rootPath
server := newRedirectServer(8080, "/cd")
// Test the redirect handler with a request path that already includes rootPath
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/cd/applications", http.NoBody)
req.Host = "example.com:8080"
w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, req)
// Verify the redirect URL
assert.Equal(t, http.StatusTemporaryRedirect, w.Code, "Should return a 307 Temporary Redirect status code")
redirectURL := w.Header().Get("Location")
// The URL should not have duplicated rootPath
duplicatedURL := "https://example.com:8080/cd/cd/applications"
assert.NotEqual(t, duplicatedURL, redirectURL, "Redirect URL should not have duplicated rootPath")
// The correct URL should be
correctURL := "https://example.com:8080/cd/applications"
assert.Equal(t, correctURL, redirectURL, "Redirect URL should be correct without duplicated rootPath")
}