(t *testing.T)
| 162 | } |
| 163 | |
| 164 | func TestCreateTCPStream(t *testing.T) { |
| 165 | // Start target HTTP/S server |
| 166 | expectedResponse := "test http response data" |
| 167 | ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 168 | assert.Equal(t, "", r.Header.Get("Proxy-Authorization"), "Should not pass the auth token to the end target server") |
| 169 | assert.Equal(t, http.MethodGet, r.Method, "Request to the end target server should be a GET") |
| 170 | _, err := fmt.Fprintf(w, expectedResponse) |
| 171 | require.NoError(t, err, "fmt.Fprintf") |
| 172 | })) |
| 173 | ts.EnableHTTP2 = true |
| 174 | // We want to listen on 0.0.0.0 because the proxy container will be on a different non-localhost network. |
| 175 | // In order to do that we have this kind of awkward hack borrowed from: |
| 176 | // https://stackoverflow.com/a/42218765/1787596 |
| 177 | l, err := net.Listen("tcp", "0.0.0.0:0") |
| 178 | if err != nil { |
| 179 | log.Fatal(err) |
| 180 | } |
| 181 | |
| 182 | // Swap out the default test server listener with our custom one listening on 0.0.0.0 |
| 183 | require.NoError(t, ts.Listener.Close(), "ts.Listener.Close()") |
| 184 | ts.Listener = l |
| 185 | ts.StartTLS() |
| 186 | defer ts.Close() |
| 187 | log.Printf("Test server listening on: %v", ts.URL) |
| 188 | |
| 189 | urlSplit := strings.Split(ts.URL, ":") |
| 190 | port := urlSplit[len(urlSplit)-1] |
| 191 | |
| 192 | certDataFile := fmt.Sprintf("%s/testdata/h2o/server.crt", testutils.RootDir()) |
| 193 | certData, err := os.ReadFile(certDataFile) |
| 194 | require.NoError(t, err, "Reading certData") |
| 195 | |
| 196 | config := ClientConfig{ |
| 197 | ProxyAddr: "localhost:8444", |
| 198 | // The h2o server we're using doesn't require an actual token so this can be anything |
| 199 | AuthToken: "fake-token", |
| 200 | Logger: logger, |
| 201 | CertData: certData, |
| 202 | Insecure: true, |
| 203 | } |
| 204 | |
| 205 | c, err := NewClient(config) |
| 206 | require.NoError(t, err, "NewClient") |
| 207 | defer func() { |
| 208 | if err := c.Close(); err != nil { |
| 209 | logger.Warn("Error closing", "err", err) |
| 210 | } |
| 211 | }() |
| 212 | |
| 213 | // host.docker.internal is a docker specific host mapping for the h2o container that resolves to our localhost |
| 214 | dockerHostURL := fmt.Sprintf("%v:%v", containerGateway, port) |
| 215 | conn, err := c.CreateTCPStream(dockerHostURL) |
| 216 | require.NoError(t, err, "CreateTCPStream") |
| 217 | defer func() { |
| 218 | require.NoError(t, conn.Close(), "conn.Close()") |
| 219 | }() |
| 220 | |
| 221 | req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%v", dockerHostURL), nil) |
nothing calls this directly
no test coverage detected