1// Copyright 2020 Google LLC. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package dca 6 7import ( 8 "testing" 9 10 "crypto/tls" 11 12 "google.golang.org/api/internal" 13) 14 15func TestGetEndpoint(t *testing.T) { 16 testCases := []struct { 17 UserEndpoint string 18 DefaultEndpoint string 19 Want string 20 WantErr bool 21 }{ 22 { 23 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 24 Want: "https://foo.googleapis.com/bar/baz", 25 }, 26 { 27 UserEndpoint: "myhost:3999", 28 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 29 Want: "https://myhost:3999/bar/baz", 30 }, 31 { 32 UserEndpoint: "https://host/path/to/bar", 33 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 34 Want: "https://host/path/to/bar", 35 }, 36 { 37 UserEndpoint: "host:123", 38 DefaultEndpoint: "", 39 Want: "host:123", 40 }, 41 { 42 UserEndpoint: "host:123", 43 DefaultEndpoint: "default:443", 44 Want: "host:123", 45 }, 46 { 47 UserEndpoint: "host:123", 48 DefaultEndpoint: "default:443/bar/baz", 49 Want: "host:123/bar/baz", 50 }, 51 } 52 53 for _, tc := range testCases { 54 got, err := getEndpoint(&internal.DialSettings{ 55 Endpoint: tc.UserEndpoint, 56 DefaultEndpoint: tc.DefaultEndpoint, 57 }, nil) 58 if tc.WantErr && err == nil { 59 t.Errorf("want err, got nil err") 60 continue 61 } 62 if !tc.WantErr && err != nil { 63 t.Errorf("want nil err, got %v", err) 64 continue 65 } 66 if tc.Want != got { 67 t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want) 68 } 69 } 70} 71 72func TestGetEndpointWithClientCertSource(t *testing.T) { 73 dummyClientCertSource := func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { return nil, nil } 74 testCases := []struct { 75 UserEndpoint string 76 DefaultEndpoint string 77 DefaultMTLSEndpoint string 78 Want string 79 WantErr bool 80 }{ 81 { 82 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 83 DefaultMTLSEndpoint: "https://foo.mtls.googleapis.com/bar/baz", 84 Want: "https://foo.mtls.googleapis.com/bar/baz", 85 }, 86 { 87 DefaultEndpoint: "https://staging-foo.sandbox.googleapis.com/bar/baz", 88 DefaultMTLSEndpoint: "https://staging-foo.mtls.sandbox.googleapis.com/bar/baz", 89 Want: "https://staging-foo.mtls.sandbox.googleapis.com/bar/baz", 90 }, 91 { 92 UserEndpoint: "myhost:3999", 93 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 94 Want: "https://myhost:3999/bar/baz", 95 }, 96 { 97 UserEndpoint: "https://host/path/to/bar", 98 DefaultEndpoint: "https://foo.googleapis.com/bar/baz", 99 Want: "https://host/path/to/bar", 100 }, 101 { 102 UserEndpoint: "host:port", 103 DefaultEndpoint: "", 104 Want: "host:port", 105 }, 106 } 107 108 for _, tc := range testCases { 109 got, err := getEndpoint(&internal.DialSettings{ 110 Endpoint: tc.UserEndpoint, 111 DefaultEndpoint: tc.DefaultEndpoint, 112 DefaultMTLSEndpoint: tc.DefaultMTLSEndpoint, 113 }, dummyClientCertSource) 114 if tc.WantErr && err == nil { 115 t.Errorf("want err, got nil err") 116 continue 117 } 118 if !tc.WantErr && err != nil { 119 t.Errorf("want nil err, got %v", err) 120 continue 121 } 122 if tc.Want != got { 123 t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want) 124 } 125 } 126} 127