package authproxy import ( "io" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" "edge-infra.dev/pkg/lib/fog" ) func TestGetApiEndpoint(t *testing.T) { t.Parallel() gin.SetMode(gin.TestMode) config := &ProxyConfig{ EAGatewayEndpoint: "http://eagateway.emergencyaccess.svc.cluster.local:80", BffEndpoint: "http://ee-bff-golang.edge-backend.svc.cluster.local:80", } tests := map[string]struct { url string exp string }{ "has /api/ea/ prefix": { url: "/api/ea/send", exp: "http://eagateway.emergencyaccess.svc.cluster.local:80/ea/send", }, "no /api/ea/ prefix": { url: "/some/other/url", exp: "http://ee-bff-golang.edge-backend.svc.cluster.local:80/some/other/url", }, "similar prefix": { url: "/api/earth/v2", exp: "http://ee-bff-golang.edge-backend.svc.cluster.local:80/api/earth/v2", }, "with domain": { url: "http://dev1.org/api/ea/send", exp: "http://eagateway.emergencyaccess.svc.cluster.local:80/ea/send", }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() c, _ := gin.CreateTestContext(httptest.NewRecorder()) req, err := http.NewRequest(http.MethodGet, tc.url, nil) if err != nil { t.Fatalf("Couldn't create request: %v\n", err) } c.Request = req res := getAPIEndpoint(c, fog.New(fog.To(io.Discard)), config) assert.NotNil(t, res) assert.Equal(t, tc.exp, res.String()) }) } } func TestFilterEmergencyAccessURL(t *testing.T) { t.Parallel() tests := map[string]struct { path string exp bool }{ "/ea/": { path: "/ea/", exp: true, }, "/ea/startSession": { path: "/ea/startSession", exp: true, }, "ea": { path: "ea", exp: false, }, "ea/": { path: "ea/", exp: false, }, "/ea": { path: "/ea", exp: false, }, "/easilymistaken/": { path: "/easilymistaken/", exp: false, }, "/something/and/then/ea/": { path: "/something/and/then/ea/", exp: false, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() assert.Equal(t, tc.exp, filterEmergencyAccessURL(tc.path)) }) } }