-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
http_test.go
174 lines (158 loc) · 5.66 KB
/
http_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package clientutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/clientutil"
import (
"crypto/tls"
"net/http"
"net/url"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
)
var (
buildInfo = component.BuildInfo{
Command: "otelcontribcol",
Version: "1.0",
}
)
func TestNewHTTPClient(t *testing.T) {
hcsEmpty := confighttp.ClientConfig{}
client1 := NewHTTPClient(hcsEmpty)
defaultTransport := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 5,
IdleConnTimeout: 45 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: false,
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
if diff := cmp.Diff(
defaultTransport,
client1.Transport.(*http.Transport),
cmpopts.IgnoreUnexported(http.Transport{}, tls.Config{}),
cmpopts.IgnoreFields(http.Transport{}, "Proxy", "DialContext")); diff != "" {
t.Errorf("Mismatched transports -want +got %s", diff)
}
assert.Equal(t, time.Duration(0), client1.Timeout)
idleConnTimeout := 30 * time.Second
maxIdleConn := 300
maxIdleConnPerHost := 150
maxConnPerHost := 250
hcs := confighttp.ClientConfig{
ReadBufferSize: 100,
WriteBufferSize: 200,
Timeout: 10 * time.Second,
IdleConnTimeout: &idleConnTimeout,
MaxIdleConns: &maxIdleConn,
MaxIdleConnsPerHost: &maxIdleConnPerHost,
MaxConnsPerHost: &maxConnPerHost,
DisableKeepAlives: true,
TLSSetting: configtls.ClientConfig{InsecureSkipVerify: true},
ProxyURL: "proxy",
// The rest are ignored
Endpoint: "endpoint",
Compression: configcompression.TypeSnappy,
HTTP2ReadIdleTimeout: 15 * time.Second,
HTTP2PingTimeout: 20 * time.Second,
}
client2 := NewHTTPClient(hcs)
expectedTransport := &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ReadBufferSize: 100,
WriteBufferSize: 200,
MaxIdleConns: maxIdleConn,
MaxIdleConnsPerHost: maxIdleConnPerHost,
MaxConnsPerHost: maxConnPerHost,
IdleConnTimeout: idleConnTimeout,
DisableKeepAlives: true,
ForceAttemptHTTP2: false,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if diff := cmp.Diff(
expectedTransport,
client2.Transport.(*http.Transport),
cmpopts.IgnoreUnexported(http.Transport{}, tls.Config{}),
cmpopts.IgnoreFields(http.Transport{}, "Proxy", "DialContext")); diff != "" {
t.Errorf("Mismatched transports -want +got %s", diff)
}
assert.Equal(t, 10*time.Second, client2.Timeout)
// Checking that the client config can receive ProxyUrl and
// it will be passed to the http client.
hcsForC3 := confighttp.ClientConfig{
ReadBufferSize: 100,
WriteBufferSize: 200,
Timeout: 10 * time.Second,
IdleConnTimeout: &idleConnTimeout,
MaxIdleConns: &maxIdleConn,
MaxIdleConnsPerHost: &maxIdleConnPerHost,
MaxConnsPerHost: &maxConnPerHost,
DisableKeepAlives: true,
TLSSetting: configtls.ClientConfig{InsecureSkipVerify: true},
ProxyURL: "http://datadog-proxy.myorganization.com:3128",
// The rest are ignored
Endpoint: "endpoint",
Compression: configcompression.TypeSnappy,
HTTP2ReadIdleTimeout: 15 * time.Second,
HTTP2PingTimeout: 20 * time.Second,
}
ddURL, _ := url.Parse("https://datadoghq.com")
parsedProxy, _ := url.Parse("http://datadog-proxy.myorganization.com:3128")
client3 := NewHTTPClient(hcsForC3)
tr3 := client3.Transport.(*http.Transport)
url3, _ := tr3.Proxy(&http.Request{
URL: ddURL,
})
assert.Equal(t, url3, parsedProxy)
// Checking that the client config can receive ProxyUrl to override the
// environment variable.
t.Setenv("HTTPS_PROXY", "http://datadog-proxy-from-env.myorganization.com:3128")
client4 := NewHTTPClient(hcsForC3)
tr4 := client4.Transport.(*http.Transport)
url4, _ := tr4.Proxy(&http.Request{
URL: ddURL,
})
assert.Equal(t, url4, parsedProxy)
// Checking that in the absence of ProxyUrl in the client config, the
// environment variable is used for the http proxy.
hcsForC5 := confighttp.ClientConfig{
ReadBufferSize: 100,
WriteBufferSize: 200,
Timeout: 10 * time.Second,
IdleConnTimeout: &idleConnTimeout,
MaxIdleConns: &maxIdleConn,
MaxIdleConnsPerHost: &maxIdleConnPerHost,
MaxConnsPerHost: &maxConnPerHost,
DisableKeepAlives: true,
TLSSetting: configtls.ClientConfig{InsecureSkipVerify: true},
// The rest are ignored
Endpoint: "endpoint",
Compression: configcompression.TypeSnappy,
HTTP2ReadIdleTimeout: 15 * time.Second,
HTTP2PingTimeout: 20 * time.Second,
}
parsedEnvProxy, _ := url.Parse("http://datadog-proxy-from-env.myorganization.com:3128")
client5 := NewHTTPClient(hcsForC5)
tr5 := client5.Transport.(*http.Transport)
url5, _ := tr5.Proxy(&http.Request{
URL: ddURL,
})
assert.Equal(t, url5, parsedEnvProxy)
}
func TestUserAgent(t *testing.T) {
assert.Equal(t, "otelcontribcol/1.0", UserAgent(buildInfo))
}
func TestDDHeaders(t *testing.T) {
header := http.Header{}
apiKey := "apikey"
SetDDHeaders(header, buildInfo, apiKey)
assert.Equal(t, header.Get("DD-Api-Key"), apiKey)
assert.Equal(t, "otelcontribcol/1.0", header.Get("USer-Agent"))
}