@@ -19,6 +19,7 @@ package v2
1919
2020import (
2121 "context"
22+ "crypto/tls"
2223 "fmt"
2324 "io"
2425 "math"
@@ -34,6 +35,7 @@ import (
3435 "github.com/stretchr/testify/assert"
3536 "k8s.io/apimachinery/pkg/types"
3637 "k8s.io/apimachinery/pkg/util/wait"
38+ "k8s.io/utils/ptr"
3739
3840 apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3941 "github.com/apache/apisix-ingress-controller/test/e2e/framework"
@@ -2033,4 +2035,146 @@ spec:
20332035 })
20342036 })
20352037 })
2038+
2039+ Context ("Test Services With AppProtocol" , func () {
2040+ const apisixRoute = `
2041+ apiVersion: apisix.apache.org/v2
2042+ kind: ApisixRoute
2043+ metadata:
2044+ name: nginx
2045+ spec:
2046+ ingressClassName: %s
2047+ http:
2048+ - name: rule0
2049+ match:
2050+ hosts:
2051+ - nginx.example
2052+ paths:
2053+ - /v1
2054+ backends:
2055+ - serviceName: nginx
2056+ servicePort: 443
2057+ `
2058+ const apisixRouteWithGranularityService = `
2059+ apiVersion: apisix.apache.org/v2
2060+ kind: ApisixRoute
2061+ metadata:
2062+ name: nginx-v2
2063+ spec:
2064+ ingressClassName: %s
2065+ http:
2066+ - name: rule0
2067+ match:
2068+ hosts:
2069+ - nginx.example
2070+ paths:
2071+ - /v2
2072+ backends:
2073+ - serviceName: nginx
2074+ servicePort: 443
2075+ resolveGranularity: service
2076+ `
2077+ const apisixRouteWithBackendWSS = `
2078+ apiVersion: apisix.apache.org/v2
2079+ kind: ApisixRoute
2080+ metadata:
2081+ name: default
2082+ spec:
2083+ ingressClassName: %s
2084+ http:
2085+ - name: rule0
2086+ match:
2087+ hosts:
2088+ - api6.com
2089+ paths:
2090+ - /ws
2091+ backends:
2092+ - serviceName: nginx
2093+ servicePort: 8443
2094+ resolveGranularity: service
2095+ `
2096+
2097+ const apisixTlsSpec = `
2098+ apiVersion: apisix.apache.org/v2
2099+ kind: ApisixTls
2100+ metadata:
2101+ name: test-tls
2102+ spec:
2103+ ingressClassName: %s
2104+ hosts:
2105+ - api6.com
2106+ secret:
2107+ name: test-tls-secret
2108+ namespace: %s
2109+ `
2110+ BeforeEach (func () {
2111+ s .DeployNginx (framework.NginxOptions {
2112+ Namespace : s .Namespace (),
2113+ Replicas : ptr .To (int32 (1 )),
2114+ })
2115+ })
2116+
2117+ It ("HTTPS Backend" , func () {
2118+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "nginx" },
2119+ new (apiv2.ApisixRoute ), fmt .Sprintf (apisixRoute , s .Namespace ()))
2120+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "nginx-v2" },
2121+ new (apiv2.ApisixRoute ), fmt .Sprintf (apisixRouteWithGranularityService , s .Namespace ()))
2122+
2123+ s .RequestAssert (& scaffold.RequestAssert {
2124+ Method : "GET" ,
2125+ Path : "/v1" ,
2126+ Host : "nginx.example" ,
2127+ Check : scaffold .WithExpectedStatus (http .StatusOK ),
2128+ })
2129+ s .RequestAssert (& scaffold.RequestAssert {
2130+ Method : "GET" ,
2131+ Path : "/v2" ,
2132+ Host : "nginx.example" ,
2133+ Check : scaffold .WithExpectedStatus (http .StatusOK ),
2134+ })
2135+ })
2136+
2137+ It ("WSS Backend" , func () {
2138+ err := s .NewKubeTlsSecret ("test-tls-secret" , Cert , Key )
2139+ Expect (err ).NotTo (HaveOccurred (), "creating TLS secret" )
2140+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "test-tls" },
2141+ & apiv2.ApisixTls {}, fmt .Sprintf (apisixTlsSpec , s .Namespace (), s .Namespace ()))
2142+
2143+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" },
2144+ new (apiv2.ApisixRoute ), fmt .Sprintf (apisixRouteWithBackendWSS , s .Namespace ()))
2145+ time .Sleep (6 * time .Second )
2146+
2147+ By ("verify wss connection" )
2148+ u := url.URL {
2149+ Scheme : "wss" ,
2150+ Host : s .GetAPISIXHTTPSEndpoint (),
2151+ Path : "/ws" ,
2152+ }
2153+ headers := http.Header {"Host" : []string {"api6.com" }}
2154+ dialer := websocket.Dialer {
2155+ TLSClientConfig : & tls.Config {
2156+ InsecureSkipVerify : true ,
2157+ ServerName : "api6.com" ,
2158+ },
2159+ }
2160+
2161+ conn , resp , err := dialer .Dial (u .String (), headers )
2162+ Expect (err ).ShouldNot (HaveOccurred (), "WebSocket handshake" )
2163+ Expect (resp .StatusCode ).Should (Equal (http .StatusSwitchingProtocols ))
2164+
2165+ defer func () {
2166+ _ = conn .Close ()
2167+ }()
2168+
2169+ By ("send and receive message through WebSocket" )
2170+ testMessage := "hello, this is APISIX"
2171+ err = conn .WriteMessage (websocket .TextMessage , []byte (testMessage ))
2172+ Expect (err ).ShouldNot (HaveOccurred (), "writing WebSocket message" )
2173+
2174+ // Then our echo
2175+ _ , msg , err := conn .ReadMessage ()
2176+ Expect (err ).ShouldNot (HaveOccurred (), "reading echo message" )
2177+ Expect (string (msg )).To (Equal (testMessage ), "message content verification" )
2178+ })
2179+ })
20362180})
0 commit comments