forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpclient_test.go
110 lines (88 loc) · 2.83 KB
/
httpclient_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
package azuremonitor
import (
"context"
"crypto/tls"
"encoding/json"
"net/http"
"testing"
"github.com/grafana/grafana-azure-sdk-go/v2/azcredentials"
"github.com/grafana/grafana-azure-sdk-go/v2/azsettings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHttpClient_AzureCredentials(t *testing.T) {
model := types.DatasourceInfo{
Credentials: &azcredentials.AzureManagedIdentityCredentials{},
}
jsonData, _ := json.Marshal(map[string]any{
"httpHeaderName1": "GrafanaHeader",
})
settings := &backend.DataSourceInstanceSettings{
JSONData: jsonData,
DecryptedSecureJSONData: map[string]string{
"httpHeaderValue1": "GrafanaValue",
},
}
azureSettings := &azsettings.AzureSettings{
Cloud: azsettings.AzurePublic,
}
provider := &fakeHttpClientProvider{}
t.Run("should have Azure middleware when scopes provided", func(t *testing.T) {
route := types.AzRoute{
Scopes: []string{"https://management.azure.com/.default"},
}
_, err := newHTTPClient(context.Background(), route, model, settings, azureSettings, provider)
require.NoError(t, err)
require.NotNil(t, provider.opts)
require.NotNil(t, provider.opts.Middlewares)
assert.Len(t, provider.opts.Middlewares, 1)
})
t.Run("should not have Azure middleware when scopes are not provided", func(t *testing.T) {
route := types.AzRoute{
Scopes: []string{},
}
_, err := newHTTPClient(context.Background(), route, model, settings, azureSettings, provider)
require.NoError(t, err)
assert.NotNil(t, provider.opts)
if provider.opts.Middlewares != nil {
assert.Len(t, provider.opts.Middlewares, 0)
}
})
t.Run("should combine custom azure and custom grafana headers", func(t *testing.T) {
route := types.AzRoute{
Headers: map[string]string{
"AzureHeader": "AzureValue",
},
}
res := http.Header{
"Grafanaheader": {"GrafanaValue"},
"Azureheader": {"AzureValue"},
}
_, err := newHTTPClient(context.Background(), route, model, settings, azureSettings, provider)
require.NoError(t, err)
assert.NotNil(t, provider.opts)
if provider.opts.Header != nil {
assert.Len(t, provider.opts.Header, 2)
assert.Equal(t, res, provider.opts.Header)
}
})
}
type fakeHttpClientProvider struct {
httpclient.Provider
opts httpclient.Options
}
func (p *fakeHttpClientProvider) New(opts ...httpclient.Options) (*http.Client, error) {
p.opts = opts[0]
return nil, nil
}
func (p *fakeHttpClientProvider) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) {
p.opts = opts[0]
return nil, nil
}
func (p *fakeHttpClientProvider) GetTLSConfig(opts ...httpclient.Options) (*tls.Config, error) {
p.opts = opts[0]
return nil, nil
}