Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/portal/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ func AddRoutes(router *gin.Engine, config PortalConfig) {
}
proxy := httputil.NewSingleHostReverseProxy(remote)
router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.Next()
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "not found",
})
return
}
if c.Request.Method != "GET" {
Expand All @@ -60,6 +63,14 @@ func AddRoutes(router *gin.Engine, config PortalConfig) {
} else {
embeddedBuildFolder := newStaticFileSystem()
fallbackFileSystem := newFallbackFileSystem(embeddedBuildFolder)
router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "not found",
})
}
})
router.Use(static.Serve("/", embeddedBuildFolder))
router.Use(static.Serve("/", fallbackFileSystem))
}
Expand Down
62 changes: 62 additions & 0 deletions internal/portal/embed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package portal

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func init() {
gin.SetMode(gin.TestMode)
}

func TestAddRoutes_NoRoute_APIReturnsJSON404(t *testing.T) {
t.Parallel()

t.Run("embedded mode returns JSON 404 for unmatched API routes", func(t *testing.T) {
t.Parallel()

router := gin.New()
AddRoutes(router, PortalConfig{})

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/nonexistent", nil)
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusNotFound, w.Code)

var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err)

assert.Equal(t, float64(http.StatusNotFound), response["status"])
assert.Equal(t, "not found", response["message"])
})

t.Run("proxy mode returns JSON 404 for unmatched API routes", func(t *testing.T) {
t.Parallel()

router := gin.New()
AddRoutes(router, PortalConfig{
ProxyURL: "http://localhost:19999",
})

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/nonexistent", nil)
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusNotFound, w.Code)

var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err)

assert.Equal(t, float64(http.StatusNotFound), response["status"])
assert.Equal(t, "not found", response["message"])
})
}
Loading