1 package authproxy
2
3 import (
4 "io"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8
9 "github.com/gin-gonic/gin"
10 "github.com/stretchr/testify/assert"
11
12 "edge-infra.dev/pkg/lib/fog"
13 )
14
15 func TestGetApiEndpoint(t *testing.T) {
16 t.Parallel()
17
18 gin.SetMode(gin.TestMode)
19 config := &ProxyConfig{
20 EAGatewayEndpoint: "http://eagateway.emergencyaccess.svc.cluster.local:80",
21 BffEndpoint: "http://ee-bff-golang.edge-backend.svc.cluster.local:80",
22 }
23 tests := map[string]struct {
24 url string
25 exp string
26 }{
27 "has /api/ea/ prefix": {
28 url: "/api/ea/send",
29 exp: "http://eagateway.emergencyaccess.svc.cluster.local:80/ea/send",
30 },
31 "no /api/ea/ prefix": {
32 url: "/some/other/url",
33 exp: "http://ee-bff-golang.edge-backend.svc.cluster.local:80/some/other/url",
34 },
35 "similar prefix": {
36 url: "/api/earth/v2",
37 exp: "http://ee-bff-golang.edge-backend.svc.cluster.local:80/api/earth/v2",
38 },
39 "with domain": {
40 url: "http://dev1.org/api/ea/send",
41 exp: "http://eagateway.emergencyaccess.svc.cluster.local:80/ea/send",
42 },
43 }
44 for name, tc := range tests {
45 tc := tc
46 t.Run(name, func(t *testing.T) {
47 t.Parallel()
48 c, _ := gin.CreateTestContext(httptest.NewRecorder())
49
50 req, err := http.NewRequest(http.MethodGet, tc.url, nil)
51 if err != nil {
52 t.Fatalf("Couldn't create request: %v\n", err)
53 }
54
55 c.Request = req
56
57 res := getAPIEndpoint(c, fog.New(fog.To(io.Discard)), config)
58 assert.NotNil(t, res)
59 assert.Equal(t, tc.exp, res.String())
60 })
61 }
62 }
63
64 func TestFilterEmergencyAccessURL(t *testing.T) {
65 t.Parallel()
66
67 tests := map[string]struct {
68 path string
69 exp bool
70 }{
71 "/ea/": {
72 path: "/ea/",
73 exp: true,
74 },
75 "/ea/startSession": {
76 path: "/ea/startSession",
77 exp: true,
78 },
79 "ea": {
80 path: "ea",
81 exp: false,
82 },
83 "ea/": {
84 path: "ea/",
85 exp: false,
86 },
87 "/ea": {
88 path: "/ea",
89 exp: false,
90 },
91 "/easilymistaken/": {
92 path: "/easilymistaken/",
93 exp: false,
94 },
95 "/something/and/then/ea/": {
96 path: "/something/and/then/ea/",
97 exp: false,
98 },
99 }
100
101 for name, tc := range tests {
102 tc := tc
103 t.Run(name, func(t *testing.T) {
104 t.Parallel()
105 assert.Equal(t, tc.exp, filterEmergencyAccessURL(tc.path))
106 })
107 }
108 }
109
View as plain text