|
| 1 | +package net |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/OpenListTeam/OpenList/v4/internal/conf" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewOSSClientUsesEnvironmentHTTPSProxy(t *testing.T) { |
| 12 | + oldConf := conf.Conf |
| 13 | + conf.Conf = conf.DefaultConfig("data") |
| 14 | + defer func() { |
| 15 | + conf.Conf = oldConf |
| 16 | + }() |
| 17 | + |
| 18 | + t.Setenv("HTTP_PROXY", "") |
| 19 | + t.Setenv("http_proxy", "") |
| 20 | + t.Setenv("HTTPS_PROXY", "http://127.0.0.1:7890") |
| 21 | + t.Setenv("https_proxy", "") |
| 22 | + t.Setenv("NO_PROXY", "") |
| 23 | + t.Setenv("no_proxy", "") |
| 24 | + |
| 25 | + client, err := NewOSSClient("https://oss-cn-hangzhou.aliyuncs.com", "test-access-key", "test-access-secret") |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("expected no error, got %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + if client.HTTPClient == nil { |
| 31 | + t.Fatal("expected OSS client to use a custom HTTP client") |
| 32 | + } |
| 33 | + |
| 34 | + transport, ok := client.HTTPClient.Transport.(*http.Transport) |
| 35 | + if !ok { |
| 36 | + t.Fatalf("expected *http.Transport, got %T", client.HTTPClient.Transport) |
| 37 | + } |
| 38 | + |
| 39 | + if transport.Proxy == nil { |
| 40 | + t.Fatal("expected proxy function to be configured") |
| 41 | + } |
| 42 | + |
| 43 | + req := &http.Request{URL: &url.URL{Scheme: "https", Host: "oss-cn-hangzhou.aliyuncs.com"}} |
| 44 | + proxyURL, err := transport.Proxy(req) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("expected no proxy lookup error, got %v", err) |
| 47 | + } |
| 48 | + if proxyURL == nil { |
| 49 | + t.Fatal("expected HTTPS proxy to be used") |
| 50 | + } |
| 51 | + if got, want := proxyURL.String(), "http://127.0.0.1:7890"; got != want { |
| 52 | + t.Fatalf("expected proxy %q, got %q", want, got) |
| 53 | + } |
| 54 | +} |
0 commit comments