/
connection_test.go
130 lines (117 loc) · 3.7 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// +build go1.13
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package armcore
import (
"context"
"net/http"
"strings"
"testing"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
)
type mockTokenCred struct{}
func (mockTokenCred) AuthenticationPolicy(azcore.AuthenticationPolicyOptions) azcore.Policy {
return azcore.PolicyFunc(func(req *azcore.Request) (*azcore.Response, error) {
return req.Next()
})
}
func (mockTokenCred) GetToken(context.Context, azcore.TokenRequestOptions) (*azcore.AccessToken, error) {
return &azcore.AccessToken{
Token: "abc123",
ExpiresOn: time.Now().Add(1 * time.Hour),
}, nil
}
func TestNewDefaultConnection(t *testing.T) {
opt := DefaultConnectionOptions()
con := NewDefaultConnection(mockTokenCred{}, &opt)
if ep := con.Endpoint(); ep != AzurePublicCloud {
t.Fatalf("unexpected endpoint %s", ep)
}
}
func TestNewConnection(t *testing.T) {
const customEndpoint = "https://contoso.com/fake/endpoint"
con := NewConnection(customEndpoint, mockTokenCred{}, nil)
if ep := con.Endpoint(); ep != customEndpoint {
t.Fatalf("unexpected endpoint %s", ep)
}
}
func TestNewConnectionWithOptions(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.AppendResponse()
opt := DefaultConnectionOptions()
opt.HTTPClient = srv
con := NewConnection(srv.URL(), mockTokenCred{}, &opt)
if ep := con.Endpoint(); ep != srv.URL() {
t.Fatalf("unexpected endpoint %s", ep)
}
req, err := azcore.NewRequest(context.Background(), http.MethodGet, srv.URL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if ua := resp.Request.Header.Get(azcore.HeaderUserAgent); !strings.HasPrefix(ua, UserAgent) {
t.Fatalf("unexpected User-Agent %s", ua)
}
}
func TestNewConnectionWithCustomTelemetry(t *testing.T) {
const myTelemetry = "something"
srv, close := mock.NewServer()
defer close()
srv.AppendResponse()
opt := DefaultConnectionOptions()
opt.HTTPClient = srv
opt.Telemetry.Value = myTelemetry
con := NewConnection(srv.URL(), mockTokenCred{}, &opt)
if ep := con.Endpoint(); ep != srv.URL() {
t.Fatalf("unexpected endpoint %s", ep)
}
req, err := azcore.NewRequest(context.Background(), http.MethodGet, srv.URL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if ua := resp.Request.Header.Get(azcore.HeaderUserAgent); !strings.HasPrefix(ua, myTelemetry+" "+UserAgent) {
t.Fatalf("unexpected User-Agent %s", ua)
}
}
func TestNewConnectionWithPipeline(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.AppendResponse()
p := getPipeline(srv)
con := NewConnectionWithPipeline(srv.URL(), p)
req, err := azcore.NewRequest(context.Background(), http.MethodGet, srv.URL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
}
func TestScope(t *testing.T) {
if s := endpointToScope(AzureGermany); s != "https://management.microsoftazure.de//.default" {
t.Fatalf("unexpected scope %s", s)
}
if s := endpointToScope("https://management.usgovcloudapi.net"); s != "https://management.usgovcloudapi.net//.default" {
t.Fatalf("unexpected scope %s", s)
}
}