|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package utils |
| 15 | + |
| 16 | +import ( |
| 17 | + "net/http" |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +// proxyForURL returns the proxy URL string that the client's transport would |
| 22 | +// use for rawurl ("" means a direct connection / no proxy). |
| 23 | +func proxyForURL(t *testing.T, rawurl string) string { |
| 24 | + t.Helper() |
| 25 | + c := NewHTTPClient(0, nil) |
| 26 | + tr, ok := c.client.Transport.(*http.Transport) |
| 27 | + if !ok || tr.Proxy == nil { |
| 28 | + t.Fatalf("transport proxy func is not set") |
| 29 | + } |
| 30 | + req, err := http.NewRequest(http.MethodGet, rawurl, nil) |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + u, err := tr.Proxy(req) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + if u == nil { |
| 39 | + return "" |
| 40 | + } |
| 41 | + return u.String() |
| 42 | +} |
| 43 | + |
| 44 | +// TestNewHTTPClientHonorsNoProxy verifies that a host listed in NO_PROXY is |
| 45 | +// reached directly even when HTTP(S)_PROXY is set, while other hosts use it. |
| 46 | +func TestNewHTTPClientHonorsNoProxy(t *testing.T) { |
| 47 | + t.Setenv("TIUP_INNER_HTTP_PROXY", "") |
| 48 | + t.Setenv("HTTP_PROXY", "http://proxy.example.com:3128") |
| 49 | + t.Setenv("HTTPS_PROXY", "http://proxy.example.com:3128") |
| 50 | + t.Setenv("NO_PROXY", "10.0.0.0/8,.internal.example.com") |
| 51 | + |
| 52 | + if got := proxyForURL(t, "http://pd-1.internal.example.com:2379/pd/api/v1/config"); got != "" { |
| 53 | + t.Errorf("host in NO_PROXY must bypass the proxy, got %q", got) |
| 54 | + } |
| 55 | + if got := proxyForURL(t, "https://tiup-mirrors.pingcap.com/timestamp.json"); got != "http://proxy.example.com:3128" { |
| 56 | + t.Errorf("external host must use the proxy, got %q", got) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestNewHTTPClientInnerProxyRespectsNoProxy verifies the TIUP_INNER_HTTP_PROXY |
| 61 | +// override still applies to external hosts but also respects NO_PROXY. |
| 62 | +func TestNewHTTPClientInnerProxyRespectsNoProxy(t *testing.T) { |
| 63 | + t.Setenv("HTTP_PROXY", "") |
| 64 | + t.Setenv("HTTPS_PROXY", "") |
| 65 | + t.Setenv("NO_PROXY", ".internal.example.com") |
| 66 | + t.Setenv("TIUP_INNER_HTTP_PROXY", "http://inner.example.com:3128") |
| 67 | + |
| 68 | + if got := proxyForURL(t, "https://tiup-mirrors.pingcap.com/timestamp.json"); got != "http://inner.example.com:3128" { |
| 69 | + t.Errorf("external host must use the inner proxy, got %q", got) |
| 70 | + } |
| 71 | + if got := proxyForURL(t, "http://pd-1.internal.example.com:2379/x"); got != "" { |
| 72 | + t.Errorf("host in NO_PROXY must bypass the inner proxy, got %q", got) |
| 73 | + } |
| 74 | +} |
0 commit comments