google.golang.org/grpc@v1.72.2/internal/resolver/delegatingresolver/delegatingresolver_test.go (about) 1 /* 2 * 3 * Copyright 2024 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package delegatingresolver 20 21 import ( 22 "errors" 23 "net/http" 24 "net/url" 25 "testing" 26 27 "github.com/google/go-cmp/cmp" 28 "google.golang.org/grpc/internal/grpctest" 29 ) 30 31 type s struct { 32 grpctest.Tester 33 } 34 35 func Test(t *testing.T) { 36 grpctest.RunSubTests(t, s{}) 37 } 38 39 const ( 40 targetTestAddr = "test.com" 41 envProxyAddr = "proxytest.com" 42 ) 43 44 // overrideHTTPSProxyFromEnvironment function overwrites HTTPSProxyFromEnvironment and 45 // returns a function to restore the default values. 46 func overrideHTTPSProxyFromEnvironment(hpfe func(req *http.Request) (*url.URL, error)) func() { 47 HTTPSProxyFromEnvironment = hpfe 48 return func() { 49 HTTPSProxyFromEnvironment = nil 50 } 51 } 52 53 // Tests that the proxyURLForTarget function correctly resolves the proxy URL 54 // for a given target address. Tests all the possible output cases. 55 func (s) TestproxyURLForTargetEnv(t *testing.T) { 56 err := errors.New("invalid proxy url") 57 tests := []struct { 58 name string 59 hpfeFunc func(req *http.Request) (*url.URL, error) 60 wantURL *url.URL 61 wantErr error 62 }{ 63 { 64 name: "valid_proxy_url_and_nil_error", 65 hpfeFunc: func(_ *http.Request) (*url.URL, error) { 66 return &url.URL{ 67 Scheme: "https", 68 Host: "proxy.example.com", 69 }, nil 70 }, 71 wantURL: &url.URL{ 72 Scheme: "https", 73 Host: "proxy.example.com", 74 }, 75 }, 76 { 77 name: "invalid_proxy_url_and_non-nil_error", 78 hpfeFunc: func(_ *http.Request) (*url.URL, error) { 79 return &url.URL{ 80 Scheme: "https", 81 Host: "notproxy.example.com", 82 }, err 83 }, 84 wantURL: &url.URL{ 85 Scheme: "https", 86 Host: "notproxy.example.com", 87 }, 88 wantErr: err, 89 }, 90 { 91 name: "nil_proxy_url_and_nil_error", 92 hpfeFunc: func(_ *http.Request) (*url.URL, error) { 93 return nil, nil 94 }, 95 wantURL: nil, 96 }, 97 } 98 for _, tt := range tests { 99 t.Run(tt.name, func(t *testing.T) { 100 defer overrideHTTPSProxyFromEnvironment(tt.hpfeFunc)() 101 got, err := proxyURLForTarget(targetTestAddr) 102 if err != tt.wantErr { 103 t.Errorf("parsedProxyURLForProxy(%v) failed with error :%v, want %v\n", targetTestAddr, err, tt.wantErr) 104 } 105 if !cmp.Equal(got, tt.wantURL) { 106 t.Fatalf("parsedProxyURLForProxy(%v) = %v, want %v\n", targetTestAddr, got, tt.wantURL) 107 } 108 }) 109 } 110 }