@@ -1659,6 +1659,76 @@ spec:
16591659 port: 80
16601660`
16611661
1662+ var corsTestService = `
1663+ apiVersion: v1
1664+ kind: Service
1665+ metadata:
1666+ name: cors-test-service
1667+ spec:
1668+ selector:
1669+ app: cors-test
1670+ ports:
1671+ - port: 80
1672+ targetPort: 5678
1673+ ---
1674+ apiVersion: apps/v1
1675+ kind: Deployment
1676+ metadata:
1677+ name: cors-test
1678+ spec:
1679+ replicas: 1
1680+ selector:
1681+ matchLabels:
1682+ app: cors-test
1683+ template:
1684+ metadata:
1685+ labels:
1686+ app: cors-test
1687+ spec:
1688+ containers:
1689+ - name: cors-test
1690+ image: hashicorp/http-echo
1691+ args: ["-text=hello", "-listen=:5678"]
1692+ ports:
1693+ - containerPort: 5678
1694+ `
1695+
1696+ var corsFilter = `
1697+ apiVersion: gateway.networking.k8s.io/v1
1698+ kind: HTTPRoute
1699+ metadata:
1700+ name: http-route-cors
1701+ namespace: %s
1702+ spec:
1703+ parentRefs:
1704+ - name: %s
1705+ hostnames:
1706+ - cors-test.example
1707+ rules:
1708+ - matches:
1709+ - path:
1710+ type: PathPrefix
1711+ value: /
1712+ filters:
1713+ - type: CORS
1714+ cors:
1715+ allowOrigins:
1716+ - http://example.com
1717+ allowMethods:
1718+ - GET
1719+ - POST
1720+ - PUT
1721+ - DELETE
1722+ allowHeaders:
1723+ - "Origin"
1724+ exposeHeaders:
1725+ - "Origin"
1726+ allowCredentials: true
1727+ backendRefs:
1728+ - name: cors-test-service
1729+ port: 80
1730+ `
1731+
16621732 BeforeEach (beforeEachHTTP )
16631733
16641734 It ("HTTPRoute RequestHeaderModifier" , func () {
@@ -1927,6 +1997,53 @@ spec:
19271997 Interval : time .Second * 2 ,
19281998 })
19291999 })
2000+ It ("HTTPRoute CORS Filter" , func () {
2001+ By ("create test service and deployment" )
2002+ Expect (s .CreateResourceFromStringWithNamespace (corsTestService , s .Namespace ())).
2003+ NotTo (HaveOccurred (), "creating CORS test service" )
2004+
2005+ By ("create HTTPRoute with CORS filter" )
2006+ s .ResourceApplied ("HTTPRoute" , "http-route-cors" , fmt .Sprintf (corsFilter , s .Namespace (), s .Namespace ()), 1 )
2007+ By ("test simple GET request with CORS headers from allowed origin" )
2008+ s .RequestAssert (& scaffold.RequestAssert {
2009+ Method : "GET" ,
2010+ Path : "/" ,
2011+ Host : "cors-test.example" ,
2012+ Headers : map [string ]string {
2013+ "Origin" : "http://example.com" ,
2014+ },
2015+ Checks : []scaffold.ResponseCheckFunc {
2016+ scaffold .WithExpectedStatus (http .StatusOK ),
2017+ scaffold .WithExpectedBodyContains ("hello" ),
2018+ scaffold .WithExpectedHeaders (map [string ]string {
2019+ "Access-Control-Allow-Origin" : "http://example.com" ,
2020+ "Access-Control-Allow-Methods" : "GET,POST,PUT,DELETE" ,
2021+ "Access-Control-Allow-Headers" : "Origin" ,
2022+ "Access-Control-Expose-Headers" : "Origin" ,
2023+ "Access-Control-Allow-Credentials" : "true" ,
2024+ }),
2025+ },
2026+ Timeout : time .Second * 30 ,
2027+ Interval : time .Second * 2 ,
2028+ })
2029+
2030+ By ("test simple GET request with CORS headers from disallowed origin" )
2031+ s .RequestAssert (& scaffold.RequestAssert {
2032+ Method : "GET" ,
2033+ Path : "/" ,
2034+ Host : "cors-test.example" ,
2035+ Headers : map [string ]string {
2036+ "Origin" : "http://disallowed.com" ,
2037+ },
2038+ Checks : []scaffold.ResponseCheckFunc {
2039+ scaffold .WithExpectedStatus (http .StatusOK ),
2040+ scaffold .WithExpectedBodyContains ("hello" ),
2041+ scaffold .WithExpectedNotHeader ("Access-Control-Allow-Origin" ),
2042+ },
2043+ Timeout : time .Second * 30 ,
2044+ Interval : time .Second * 2 ,
2045+ })
2046+ })
19302047 })
19312048
19322049 Context ("HTTPRoute Multiple Backend" , func () {
0 commit comments