mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 02:38:33 +02:00
* **New Features** * Routes can promote route-local bypass rules into matching entrypoint middleware, layering route-specific bypasses onto existing entrypoint rules and avoiding duplicate evaluation. * **Behavior Changes** * Entrypoint middleware updates now refresh per-route overlays at runtime; overlay compilation failures result in HTTP 500 (errors are not exposed verbatim). * Route middleware accessors now return safe clones. * **Documentation** * Clarified promotion, consumption, merging and qualification semantics with examples. * **Tests** * Added tests covering promotion, cache invalidation, consumption semantics, and error handling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package entrypoint
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/yusing/godoxy/internal/types"
|
|
)
|
|
|
|
func TestSetMiddlewaresInvalidatesRouteOverlayCache(t *testing.T) {
|
|
ep := NewTestEntrypoint(t, nil)
|
|
srv := newTestHTTPServer(t, ep)
|
|
route := newFakeHTTPRoute(t, "test-route", "")
|
|
route.routeMiddlewares = map[string]types.LabelMap{
|
|
"redirectHTTP": {
|
|
"bypass": "- path /health\n",
|
|
},
|
|
}
|
|
route.handler = func(w http.ResponseWriter, req *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
srv.AddRoute(route)
|
|
|
|
require.NoError(t, ep.SetMiddlewares([]map[string]any{{
|
|
"use": "redirectHTTP",
|
|
}}))
|
|
|
|
first := httptest.NewRecorder()
|
|
srv.ServeHTTP(first, httptest.NewRequest(http.MethodGet, "http://test-route/private", nil))
|
|
require.Equal(t, http.StatusPermanentRedirect, first.Code)
|
|
|
|
require.NoError(t, ep.SetMiddlewares([]map[string]any{{
|
|
"use": "response",
|
|
"set_headers": map[string]string{
|
|
"X-Overlay-Reloaded": "true",
|
|
},
|
|
}}))
|
|
|
|
second := httptest.NewRecorder()
|
|
srv.ServeHTTP(second, httptest.NewRequest(http.MethodGet, "http://test-route/private", nil))
|
|
require.Equal(t, http.StatusNoContent, second.Code)
|
|
require.Equal(t, "true", second.Header().Get("X-Overlay-Reloaded"))
|
|
}
|
|
|
|
func TestServeHTTPHidesEntrypointOverlayCompilationErrors(t *testing.T) {
|
|
ep := NewTestEntrypoint(t, nil)
|
|
srv := newTestHTTPServer(t, ep)
|
|
route := newFakeHTTPRoute(t, "test-route", "")
|
|
route.routeMiddlewares = map[string]types.LabelMap{
|
|
"redirectHTTP": {
|
|
"bypass": "not-a-valid-bypass",
|
|
},
|
|
}
|
|
route.handler = func(w http.ResponseWriter, req *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
srv.AddRoute(route)
|
|
|
|
require.NoError(t, ep.SetMiddlewares([]map[string]any{{
|
|
"use": "redirectHTTP",
|
|
}}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "http://test-route/", nil))
|
|
|
|
require.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
require.Equal(t, "internal server error\n", rec.Body.String())
|
|
}
|