Skip to content

Commit d3af146

Browse files
committed
e2e/tests(proxy): fix TestProxyBadGateway
Enable TestProxyBadGateway in all cases.
1 parent 0c636af commit d3af146

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

e2e/tests/proxy_test.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,28 +239,46 @@ func TestProxyBadGateway(t *testing.T) {
239239
"httpbin:1",
240240
}
241241

242-
var expectedErrorMessage string
243-
switch {
244-
case os.Getenv("FORWARDER_PROXY") != "":
245-
expectedErrorMessage = forwarder.UpstreamProxyServiceName + " failed to connect to remote host"
246-
case os.Getenv("FORWARDER_PAC") != "":
247-
// Proxy name depends on the PAC file. Skip it for now.
248-
expectedErrorMessage = "failed to connect to remote host"
249-
default:
250-
expectedErrorMessage = "forwarder failed to connect to remote host"
242+
expectedErrorMessage := "forwarder dial tcp"
243+
if os.Getenv("FORWARDER_PROXY") != "" {
244+
// Make sure the error originates from the upstream proxy.
245+
expectedErrorMessage = forwarder.UpstreamProxyServiceName + " dial tcp"
246+
} else if os.Getenv("FORWARDER_PAC") != "" {
247+
// In case of PAC, we can never now the exact proxy that has been used.
248+
expectedErrorMessage = "dial tcp"
249+
}
250+
251+
enableInsecureSkipVerifyIfCAIsGenerated := func(tr *http.Transport) {
252+
if os.Getenv("SETUP") == "flag-mitm-genca" {
253+
tr.TLSClientConfig.InsecureSkipVerify = true
254+
}
255+
}
256+
257+
var cres *http.Response
258+
saveProxyConnectResponse := func(tr *http.Transport) {
259+
tr.OnProxyConnectResponse = func(ctx context.Context, proxyURL *url.URL, connectReq *http.Request, connectRes *http.Response) error {
260+
cres = connectRes
261+
return nil
262+
}
251263
}
252264

253265
for _, scheme := range []string{"http", "https"} {
254266
for _, h := range hosts {
255267
t.Run(scheme+"_"+h, func(t *testing.T) {
256-
res := newClient(t, scheme+"://"+h).GET("/status/200")
268+
c := newClient(t, scheme+"://"+h,
269+
enableInsecureSkipVerifyIfCAIsGenerated,
270+
saveProxyConnectResponse)
271+
272+
res := c.GET("/status/200")
257273
res.ExpectStatus(http.StatusBadGateway)
258274

259-
// Check if the error message is correctly propagated to the client.
260-
// Especially when several proxies are chained.
261-
// FIXME(hg): When HTTPS CONNECT request fails it does not propagate error message - HTTP client does not return it.
262-
if scheme == "http" && !strings.Contains(string(res.Body), expectedErrorMessage) {
263-
t.Fatalf("Expected valid error message, got %s", res.Body)
275+
// Fallback to cres if no response was received - this is to work around Go behavior.
276+
if len(res.Header) == 0 {
277+
res = c.MakeResponse(cres)
278+
}
279+
280+
if msg := res.Header.Get("X-Forwarder-Error"); !strings.Contains(msg, expectedErrorMessage) {
281+
t.Fatalf("Expected error message to contain %q, got %q", expectedErrorMessage, msg)
264282
}
265283
})
266284
}

0 commit comments

Comments
 (0)