github.com/openshift-online/ocm-sdk-go@v0.1.473/h2c_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 /* 5 Copyright (c) 2021 Red Hat, Inc. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 // This file contains tests for the support for Unix sockets. 21 22 package sdk 23 24 import ( 25 "net/http" 26 "net/url" 27 "os" 28 "path/filepath" 29 "time" 30 31 "github.com/onsi/gomega/ghttp" 32 33 . "github.com/onsi/ginkgo/v2/dsl/core" // nolint 34 . "github.com/onsi/gomega" // nolint 35 . "github.com/openshift-online/ocm-sdk-go/testing" // nolint 36 ) 37 38 var _ = Describe("H2C", func() { 39 var ( 40 accessToken string 41 refreshToken string 42 oidServer *ghttp.Server 43 ) 44 45 BeforeEach(func() { 46 // Create the tokens: 47 accessToken = MakeTokenString("Bearer", 5*time.Minute) 48 refreshToken = MakeTokenString("Refresh", 10*time.Hour) 49 50 // Create the OpenID server: 51 oidServer = MakeTCPServer() 52 oidServer.AppendHandlers( 53 ghttp.CombineHandlers( 54 RespondWithAccessAndRefreshTokens(accessToken, refreshToken), 55 ), 56 ) 57 }) 58 59 AfterEach(func() { 60 // Stop the OpenID server: 61 oidServer.Close() 62 }) 63 64 Describe("With TCP", func() { 65 var ( 66 apiServer *ghttp.Server 67 connection *Connection 68 ) 69 70 BeforeEach(func() { 71 var err error 72 73 // Create the API server: 74 apiServer = MakeTCPH2CServer() 75 76 // Alter the URL scheme to force use of HTTP/2 without TLS: 77 apiURL, err := url.Parse(apiServer.URL()) 78 Expect(err).ToNot(HaveOccurred()) 79 apiURL.Scheme = "h2c" 80 81 // Create the connection: 82 connection, err = NewConnectionBuilder(). 83 Logger(logger). 84 TokenURL(oidServer.URL()). 85 URL(apiURL.String()). 86 Tokens(accessToken, refreshToken). 87 Build() 88 Expect(err).ToNot(HaveOccurred()) 89 }) 90 91 AfterEach(func() { 92 // Close the connection: 93 err := connection.Close() 94 Expect(err).ToNot(HaveOccurred()) 95 96 // Stop the API server: 97 apiServer.Close() 98 }) 99 100 It("Uses HTTP/2.0", func() { 101 // Configure the server: 102 apiServer.AppendHandlers( 103 ghttp.CombineHandlers( 104 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 105 Expect(r.Proto).To(Equal("HTTP/2.0")) 106 }), 107 RespondWithJSON(http.StatusOK, `{ 108 "href": "/api/clusters_mgmt" 109 }`), 110 ), 111 ) 112 113 // Send the request: 114 response, err := connection.Get(). 115 Path("/mypath"). 116 Send() 117 Expect(err).ToNot(HaveOccurred()) 118 Expect(response).ToNot(BeNil()) 119 Expect(response.String()).To(MatchJSON(`{ 120 "href": "/api/clusters_mgmt" 121 }`)) 122 }) 123 }) 124 125 Describe("With Unix socket", func() { 126 var ( 127 apiServer *ghttp.Server 128 apiSocket string 129 connection *Connection 130 ) 131 132 BeforeEach(func() { 133 var err error 134 135 // Create the API server: 136 apiServer, apiSocket = MakeUnixH2CServer() 137 apiURL := "unix+h2c://127.0.0.1" + apiSocket 138 139 // Create the connection: 140 connection, err = NewConnectionBuilder(). 141 Logger(logger). 142 TokenURL(oidServer.URL()). 143 URL(apiURL). 144 Tokens(accessToken, refreshToken). 145 Build() 146 Expect(err).ToNot(HaveOccurred()) 147 }) 148 149 AfterEach(func() { 150 var err error 151 152 // Close the connection: 153 err = connection.Close() 154 Expect(err).ToNot(HaveOccurred()) 155 156 // Stop the API server: 157 apiServer.Close() 158 159 // Remore the temporary files and directories: 160 err = os.RemoveAll(filepath.Dir(apiSocket)) 161 Expect(err).ToNot(HaveOccurred()) 162 }) 163 164 It("Uses HTTP/2.0", func() { 165 // Configure the server: 166 apiServer.AppendHandlers( 167 ghttp.CombineHandlers( 168 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 169 Expect(r.Proto).To(Equal("HTTP/2.0")) 170 }), 171 RespondWithJSON(http.StatusOK, `{ 172 "href": "/api/clusters_mgmt" 173 }`), 174 ), 175 ) 176 177 // Send the request: 178 response, err := connection.Get(). 179 Path("/mypath"). 180 Send() 181 Expect(err).ToNot(HaveOccurred()) 182 Expect(response).ToNot(BeNil()) 183 Expect(response.String()).To(MatchJSON(`{ 184 "href": "/api/clusters_mgmt" 185 }`)) 186 }) 187 }) 188 })