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 http
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:port",
38			DefaultEndpoint: "",
39			WantErr:         true,
40		},
41	}
42
43	for _, tc := range testCases {
44		got, err := getEndpoint(&internal.DialSettings{
45			Endpoint:        tc.UserEndpoint,
46			DefaultEndpoint: tc.DefaultEndpoint,
47		}, nil)
48		if tc.WantErr && err == nil {
49			t.Errorf("want err, got nil err")
50			continue
51		}
52		if !tc.WantErr && err != nil {
53			t.Errorf("want nil err, got %v", err)
54			continue
55		}
56		if tc.Want != got {
57			t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want)
58		}
59	}
60}
61
62func TestGetEndpointWithClientCertSource(t *testing.T) {
63	dummyClientCertSource := func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { return nil, nil }
64	testCases := []struct {
65		UserEndpoint    string
66		DefaultEndpoint string
67		Want            string
68		WantErr         bool
69	}{
70		{
71			DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
72			Want:            "https://foo.googleapis.com/bar/baz",
73		},
74		{
75			DefaultEndpoint: "https://staging-foo.sandbox.googleapis.com/bar/baz",
76			Want:            "https://staging-foo.sandbox.googleapis.com/bar/baz",
77		},
78		{
79			UserEndpoint:    "myhost:3999",
80			DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
81			Want:            "https://myhost:3999/bar/baz",
82		},
83		{
84			UserEndpoint:    "https://host/path/to/bar",
85			DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
86			Want:            "https://host/path/to/bar",
87		},
88		{
89			UserEndpoint:    "host:port",
90			DefaultEndpoint: "",
91			WantErr:         true,
92		},
93	}
94
95	for _, tc := range testCases {
96		got, err := getEndpoint(&internal.DialSettings{
97			Endpoint:        tc.UserEndpoint,
98			DefaultEndpoint: tc.DefaultEndpoint,
99		}, dummyClientCertSource)
100		if tc.WantErr && err == nil {
101			t.Errorf("want err, got nil err")
102			continue
103		}
104		if !tc.WantErr && err != nil {
105			t.Errorf("want nil err, got %v", err)
106			continue
107		}
108		if tc.Want != got {
109			t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want)
110		}
111	}
112}
113