@@ -19,6 +19,7 @@ package ingress
1919
2020import (
2121 "context"
22+ "encoding/json"
2223 "fmt"
2324 "net/http"
2425 "time"
@@ -102,6 +103,31 @@ spec:
102103 port:
103104 number: 443
104105`
106+
107+ ingressCORS = `
108+ apiVersion: networking.k8s.io/v1
109+ kind: Ingress
110+ metadata:
111+ name: cors
112+ annotations:
113+ k8s.apisix.apache.org/enable-cors: "true"
114+ k8s.apisix.apache.org/cors-allow-origin: "https://allowed.example"
115+ k8s.apisix.apache.org/cors-allow-methods: "GET,POST"
116+ k8s.apisix.apache.org/cors-allow-headers: "Origin,Authorization"
117+ spec:
118+ ingressClassName: %s
119+ rules:
120+ - host: cors.example
121+ http:
122+ paths:
123+ - path: /get
124+ pathType: Exact
125+ backend:
126+ service:
127+ name: nginx
128+ port:
129+ number: 80
130+ `
105131 )
106132 BeforeEach (func () {
107133 s .DeployNginx (framework.NginxOptions {
@@ -167,6 +193,53 @@ spec:
167193 Expect (upstreams [0 ].Timeout .Send ).To (Equal (3 ), "checking Upstream send timeout" )
168194 Expect (upstreams [0 ].Timeout .Connect ).To (Equal (4 ), "checking Upstream connect timeout" )
169195 })
196+
197+ It ("cors annotations" , func () {
198+ Expect (s .CreateResourceFromString (fmt .Sprintf (ingressCORS , s .Namespace ()))).ShouldNot (HaveOccurred (), "creating Ingress" )
199+
200+ s .RequestAssert (& scaffold.RequestAssert {
201+ Method : "GET" ,
202+ Path : "/get" ,
203+ Host : "cors.example" ,
204+ Headers : map [string ]string {
205+ "Origin" : "https://allowed.example" ,
206+ },
207+ Checks : []scaffold.ResponseCheckFunc {
208+ scaffold .WithExpectedStatus (http .StatusOK ),
209+ scaffold .WithExpectedHeaders (map [string ]string {
210+ "Access-Control-Allow-Origin" : "https://allowed.example" ,
211+ "Access-Control-Allow-Methods" : "GET,POST" ,
212+ "Access-Control-Allow-Headers" : "Origin,Authorization" ,
213+ }),
214+ },
215+ })
216+
217+ s .RequestAssert (& scaffold.RequestAssert {
218+ Method : "GET" ,
219+ Path : "/get" ,
220+ Host : "cors.example" ,
221+ Headers : map [string ]string {
222+ "Origin" : "https://blocked.example" ,
223+ },
224+ Checks : []scaffold.ResponseCheckFunc {
225+ scaffold .WithExpectedStatus (http .StatusOK ),
226+ scaffold .WithExpectedNotHeader ("Access-Control-Allow-Origin" ),
227+ },
228+ })
229+
230+ routes , err := s .DefaultDataplaneResource ().Route ().List (context .Background ())
231+ Expect (err ).NotTo (HaveOccurred (), "listing Service" )
232+ Expect (routes ).To (HaveLen (1 ), "checking Route length" )
233+ Expect (routes [0 ].Plugins ).To (HaveKey ("cors" ), "checking Route plugins" )
234+ jsonBytes , err := json .Marshal (routes [0 ].Plugins ["cors" ])
235+ Expect (err ).NotTo (HaveOccurred (), "marshalling cors plugin config" )
236+ var corsConfig map [string ]any
237+ err = json .Unmarshal (jsonBytes , & corsConfig )
238+ Expect (err ).NotTo (HaveOccurred (), "unmarshalling cors plugin config" )
239+ Expect (corsConfig ["allow_origins" ]).To (Equal ("https://allowed.example" ), "checking cors allow origins" )
240+ Expect (corsConfig ["allow_methods" ]).To (Equal ("GET,POST" ), "checking cors allow methods" )
241+ Expect (corsConfig ["allow_headers" ]).To (Equal ("Origin,Authorization" ), "checking cors allow headers" )
242+ })
170243 })
171244
172245 Context ("Plugins" , func () {
0 commit comments