Debian Codegraph @

exporters/otlp/otlplog/otlploghttp/config_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlploghttp
5
6 import (
7 "crypto/tls"
8 "crypto/x509"
9 "errors"
10 "fmt"
11 "net/http"
12 "net/url"
13 "testing"
14 "time"
15
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18
19 "go.opentelemetry.io/otel"
20 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/retry"
21 )
22
23 const (
24 weakCertificate = `
25 -----BEGIN CERTIFICATE-----
26 MIIBhzCCASygAwIBAgIRANHpHgAWeTnLZpTSxCKs0ggwCgYIKoZIzj0EAwIwEjEQ
27 MA4GA1UEChMHb3RlbC1nbzAeFw0yMTA0MDExMzU5MDNaFw0yMTA0MDExNDU5MDNa
28 MBIxEDAOBgNVBAoTB290ZWwtZ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAS9
29 nWSkmPCxShxnp43F+PrOtbGV7sNfkbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0Z
30 sJCLHGogQsYnWJBXUZOVo2MwYTAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYI
31 KwYBBQUHAwEwDAYDVR0TAQH/BAIwADAsBgNVHREEJTAjgglsb2NhbGhvc3SHEAAA
32 AAAAAAAAAAAAAAAAAAGHBH8AAAEwCgYIKoZIzj0EAwIDSQAwRgIhANwZVVKvfvQ/
33 1HXsTvgH+xTQswOwSSKYJ1cVHQhqK7ZbAiEAus8NxpTRnp5DiTMuyVmhVNPB+bVH
34 Lhnm4N/QDk5rek0=
35 -----END CERTIFICATE-----
36 `
37 weakPrivateKey = `
38 -----BEGIN PRIVATE KEY-----
39 MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgN8HEXiXhvByrJ1zK
40 SFT6Y2l2KqDWwWzKf+t4CyWrNKehRANCAAS9nWSkmPCxShxnp43F+PrOtbGV7sNf
41 kbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0ZsJCLHGogQsYnWJBXUZOV
42 -----END PRIVATE KEY-----
43 `
44 )
45
46 func newTLSConf(cert, key []byte) (*tls.Config, error) {
47 cp := x509.NewCertPool()
48 if ok := cp.AppendCertsFromPEM(cert); !ok {
49 return nil, errors.New("failed to append certificate to the cert pool")
50 }
51 crt, err := tls.X509KeyPair(cert, key)
52 if err != nil {
53 return nil, err
54 }
55 crts := []tls.Certificate{crt}
56 return &tls.Config{RootCAs: cp, Certificates: crts}, nil
57 }
58
59 func TestNewConfig(t *testing.T) {
60 orig := readFile
61 readFile = func() func(name string) ([]byte, error) {
62 index := map[string][]byte{
63 "cert_path": []byte(weakCertificate),
64 "key_path": []byte(weakPrivateKey),
65 "invalid_cert": []byte("invalid certificate file."),
66 "invalid_key": []byte("invalid key file."),
67 }
68 return func(name string) ([]byte, error) {
69 b, ok := index[name]
70 if !ok {
71 err := fmt.Errorf("file does not exist: %s", name)
72 return nil, err
73 }
74 return b, nil
75 }
76 }()
77 t.Cleanup(func() { readFile = orig })
78
79 tlsCfg, err := newTLSConf([]byte(weakCertificate), []byte(weakPrivateKey))
80 require.NoError(t, err, "testing TLS config")
81
82 headers := map[string]string{"a": "A"}
83 rc := retry.Config{}
84
85 testcases := []struct {
86 name string
87 options []Option
88 envars map[string]string
89 want config
90 errs []string
91 }{
92 {
93 name: "Defaults",
94 want: config{
95 endpoint: newSetting(defaultEndpoint),
96 path: newSetting(defaultPath),
97 timeout: newSetting(defaultTimeout),
98 retryCfg: newSetting(defaultRetryCfg),
99 },
100 },
101 {
102 name: "Options",
103 options: []Option{
104 WithEndpoint("test"),
105 WithURLPath("/path"),
106 WithInsecure(),
107 WithTLSClientConfig(tlsCfg),
108 WithCompression(GzipCompression),
109 WithHeaders(headers),
110 WithTimeout(time.Second),
111 WithRetry(RetryConfig(rc)),
112 // Do not test WithProxy. Requires func comparison.
113 },
114 want: config{
115 endpoint: newSetting("test"),
116 path: newSetting("/path"),
117 insecure: newSetting(true),
118 tlsCfg: newSetting(tlsCfg),
119 headers: newSetting(headers),
120 compression: newSetting(GzipCompression),
121 timeout: newSetting(time.Second),
122 retryCfg: newSetting(rc),
123 },
124 },
125 {
126 name: "WithEndpointURL",
127 options: []Option{
128 WithEndpointURL("http://test:8080/path"),
129 },
130 want: config{
131 endpoint: newSetting("test:8080"),
132 path: newSetting("/path"),
133 insecure: newSetting(true),
134 timeout: newSetting(defaultTimeout),
135 retryCfg: newSetting(defaultRetryCfg),
136 },
137 },
138 {
139 name: "EndpointPrecedence",
140 options: []Option{
141 WithEndpointURL("https://test:8080/path"),
142 WithEndpoint("not-test:9090"),
143 WithURLPath("/alt"),
144 WithInsecure(),
145 },
146 want: config{
147 endpoint: newSetting("not-test:9090"),
148 path: newSetting("/alt"),
149 insecure: newSetting(true),
150 timeout: newSetting(defaultTimeout),
151 retryCfg: newSetting(defaultRetryCfg),
152 },
153 },
154 {
155 name: "EndpointURLPrecedence",
156 options: []Option{
157 WithEndpoint("not-test:9090"),
158 WithURLPath("/alt"),
159 WithInsecure(),
160 WithEndpointURL("https://test:8080/path"),
161 },
162 want: config{
163 endpoint: newSetting("test:8080"),
164 path: newSetting("/path"),
165 insecure: newSetting(false),
166 timeout: newSetting(defaultTimeout),
167 retryCfg: newSetting(defaultRetryCfg),
168 },
169 },
170 {
171 name: "WithEndpointURL secure when Environment Endpoint is set insecure",
172 envars: map[string]string{
173 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "http://env.endpoint:8080/prefix",
174 },
175 options: []Option{
176 WithEndpointURL("https://test:8080/path"),
177 },
178 want: config{
179 endpoint: newSetting("test:8080"),
180 path: newSetting("/path"),
181 insecure: newSetting(false),
182 timeout: newSetting(defaultTimeout),
183 retryCfg: newSetting(defaultRetryCfg),
184 },
185 },
186 {
187 name: "WithEndpointURL secure when Environment insecure is set false",
188 envars: map[string]string{
189 "OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
190 },
191 options: []Option{
192 WithEndpointURL("https://test:8080/path"),
193 },
194 want: config{
195 endpoint: newSetting("test:8080"),
196 path: newSetting("/path"),
197 insecure: newSetting(false),
198 timeout: newSetting(defaultTimeout),
199 retryCfg: newSetting(defaultRetryCfg),
200 },
201 },
202 {
203 name: "LogEnvironmentVariables",
204 envars: map[string]string{
205 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
206 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "a=A",
207 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
208 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
209 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
210 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
211 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
212 },
213 want: config{
214 endpoint: newSetting("env.endpoint:8080"),
215 path: newSetting("/prefix"),
216 insecure: newSetting(false),
217 tlsCfg: newSetting(tlsCfg),
218 headers: newSetting(headers),
219 compression: newSetting(GzipCompression),
220 timeout: newSetting(15 * time.Second),
221 retryCfg: newSetting(defaultRetryCfg),
222 },
223 },
224 {
225 name: "LogEndpointEnvironmentVariablesDefaultPath",
226 envars: map[string]string{
227 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "http://env.endpoint",
228 },
229 want: config{
230 endpoint: newSetting("env.endpoint"),
231 path: newSetting("/"),
232 insecure: newSetting(true),
233 timeout: newSetting(defaultTimeout),
234 retryCfg: newSetting(defaultRetryCfg),
235 },
236 },
237 {
238 name: "OTLPEnvironmentVariables",
239 envars: map[string]string{
240 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint:8080/prefix",
241 "OTEL_EXPORTER_OTLP_HEADERS": "a=A",
242 "OTEL_EXPORTER_OTLP_COMPRESSION": "none",
243 "OTEL_EXPORTER_OTLP_TIMEOUT": "15000",
244 "OTEL_EXPORTER_OTLP_CERTIFICATE": "cert_path",
245 "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE": "cert_path",
246 "OTEL_EXPORTER_OTLP_CLIENT_KEY": "key_path",
247 },
248 want: config{
249 endpoint: newSetting("env.endpoint:8080"),
250 path: newSetting("/prefix/v1/logs"),
251 insecure: newSetting(true),
252 tlsCfg: newSetting(tlsCfg),
253 headers: newSetting(headers),
254 compression: newSetting(NoCompression),
255 timeout: newSetting(15 * time.Second),
256 retryCfg: newSetting(defaultRetryCfg),
257 },
258 },
259 {
260 name: "OTLPEndpointEnvironmentVariablesDefaultPath",
261 envars: map[string]string{
262 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint",
263 },
264 want: config{
265 endpoint: newSetting("env.endpoint"),
266 path: newSetting(defaultPath),
267 insecure: newSetting(true),
268 timeout: newSetting(defaultTimeout),
269 retryCfg: newSetting(defaultRetryCfg),
270 },
271 },
272 {
273 name: "EnvironmentVariablesPrecedence",
274 envars: map[string]string{
275 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://ignored:9090/alt",
276 "OTEL_EXPORTER_OTLP_HEADERS": "b=B",
277 "OTEL_EXPORTER_OTLP_COMPRESSION": "none",
278 "OTEL_EXPORTER_OTLP_TIMEOUT": "30000",
279 "OTEL_EXPORTER_OTLP_CERTIFICATE": "invalid_cert",
280 "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE": "invalid_cert",
281 "OTEL_EXPORTER_OTLP_CLIENT_KEY": "invalid_key",
282
283 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/path",
284 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "a=A",
285 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
286 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
287 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
288 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
289 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
290 },
291 want: config{
292 endpoint: newSetting("env.endpoint:8080"),
293 path: newSetting("/path"),
294 insecure: newSetting(false),
295 tlsCfg: newSetting(tlsCfg),
296 headers: newSetting(headers),
297 compression: newSetting(GzipCompression),
298 timeout: newSetting(15 * time.Second),
299 retryCfg: newSetting(defaultRetryCfg),
300 },
301 },
302 {
303 name: "OptionsPrecedence",
304 envars: map[string]string{
305 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://ignored:9090/alt",
306 "OTEL_EXPORTER_OTLP_HEADERS": "b=B",
307 "OTEL_EXPORTER_OTLP_COMPRESSION": "none",
308 "OTEL_EXPORTER_OTLP_TIMEOUT": "30000",
309 "OTEL_EXPORTER_OTLP_CERTIFICATE": "invalid_cert",
310 "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE": "invalid_cert",
311 "OTEL_EXPORTER_OTLP_CLIENT_KEY": "invalid_key",
312
313 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
314 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "a=A",
315 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
316 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
317 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
318 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
319 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
320 },
321 options: []Option{
322 WithEndpoint("test"),
323 WithEndpointURL("https://test2/path2"),
324 WithURLPath("/path"),
325 WithInsecure(),
326 WithTLSClientConfig(tlsCfg),
327 WithCompression(GzipCompression),
328 WithHeaders(headers),
329 WithTimeout(time.Second),
330 WithRetry(RetryConfig(rc)),
331 },
332 want: config{
333 endpoint: newSetting("test2"),
334 path: newSetting("/path"),
335 insecure: newSetting(true),
336 tlsCfg: newSetting(tlsCfg),
337 headers: newSetting(headers),
338 compression: newSetting(GzipCompression),
339 timeout: newSetting(time.Second),
340 retryCfg: newSetting(rc),
341 },
342 },
343 {
344 name: "InvalidEnvironmentVariables",
345 envars: map[string]string{
346 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "%invalid",
347 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "invalid key=value",
348 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "xz",
349 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "100 seconds",
350 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "invalid_cert",
351 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "invalid_cert",
352 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "invalid_key",
353 },
354 want: config{
355 endpoint: newSetting(defaultEndpoint),
356 path: newSetting(defaultPath),
357 timeout: newSetting(defaultTimeout),
358 retryCfg: newSetting(defaultRetryCfg),
359 },
360 errs: []string{
361 `invalid OTEL_EXPORTER_OTLP_LOGS_ENDPOINT value %invalid: parse "%invalid": invalid URL escape "%in"`,
362 `failed to load TLS:`,
363 `certificate not added`,
364 `tls: failed to find any PEM data in certificate input`,
365 `invalid OTEL_EXPORTER_OTLP_LOGS_HEADERS value invalid key=value: invalid header key: invalid key`,
366 `invalid OTEL_EXPORTER_OTLP_LOGS_COMPRESSION value xz: unknown compression: xz`,
367 `invalid OTEL_EXPORTER_OTLP_LOGS_TIMEOUT value 100 seconds: strconv.Atoi: parsing "100 seconds": invalid syntax`,
368 },
369 },
370 {
371 name: "with percent-encoded headers",
372 envars: map[string]string{
373 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
374 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "user%2Did=42,user%20name=alice%20smith",
375 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
376 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
377 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
378 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
379 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
380 },
381 want: config{
382 endpoint: newSetting("env.endpoint:8080"),
383 path: newSetting("/prefix"),
384 insecure: newSetting(false),
385 tlsCfg: newSetting(tlsCfg),
386 headers: newSetting(map[string]string{"user%2Did": "42", "user%20name": "alice smith"}),
387 compression: newSetting(GzipCompression),
388 timeout: newSetting(15 * time.Second),
389 retryCfg: newSetting(defaultRetryCfg),
390 },
391 },
392 {
393 name: "with invalid header key",
394 envars: map[string]string{
395 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
396 "OTEL_EXPORTER_OTLP_LOGS_HEADERS": "valid-key=value,invalid key=value",
397 "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
398 "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
399 "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
400 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
401 "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
402 },
403 want: config{
404 endpoint: newSetting("env.endpoint:8080"),
405 path: newSetting("/prefix"),
406 insecure: newSetting(false),
407 tlsCfg: newSetting(tlsCfg),
408 compression: newSetting(GzipCompression),
409 timeout: newSetting(15 * time.Second),
410 retryCfg: newSetting(defaultRetryCfg),
411 },
412 },
413 {
414 name: "OptionEndpointURLWithoutScheme",
415 options: []Option{
416 WithEndpointURL("//env.endpoint:8080/prefix"),
417 },
418 want: config{
419 endpoint: newSetting("env.endpoint:8080"),
420 path: newSetting("/prefix"),
421 retryCfg: newSetting(defaultRetryCfg),
422 timeout: newSetting(defaultTimeout),
423 },
424 },
425 {
426 name: "EnvEndpointWithoutScheme",
427 envars: map[string]string{
428 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
429 },
430 want: config{
431 endpoint: newSetting("env.endpoint:8080"),
432 path: newSetting("/prefix"),
433 retryCfg: newSetting(defaultRetryCfg),
434 timeout: newSetting(defaultTimeout),
435 },
436 },
437 {
438 name: "DefaultEndpointWithEnvInsecure",
439 envars: map[string]string{
440 "OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
441 },
442 want: config{
443 endpoint: newSetting(defaultEndpoint),
444 path: newSetting(defaultPath),
445 insecure: newSetting(true),
446 retryCfg: newSetting(defaultRetryCfg),
447 timeout: newSetting(defaultTimeout),
448 },
449 },
450 {
451 name: "EnvEndpointWithoutSchemeWithEnvInsecure",
452 envars: map[string]string{
453 "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
454 "OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
455 },
456 want: config{
457 endpoint: newSetting("env.endpoint:8080"),
458 path: newSetting("/prefix"),
459 insecure: newSetting(true),
460 retryCfg: newSetting(defaultRetryCfg),
461 timeout: newSetting(defaultTimeout),
462 },
463 },
464 {
465 name: "OptionEndpointURLWithoutSchemeWithEnvInsecure",
466 options: []Option{
467 WithEndpointURL("//env.endpoint:8080/prefix"),
468 },
469 envars: map[string]string{
470 "OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
471 },
472 want: config{
473 endpoint: newSetting("env.endpoint:8080"),
474 path: newSetting("/prefix"),
475 insecure: newSetting(true),
476 retryCfg: newSetting(defaultRetryCfg),
477 timeout: newSetting(defaultTimeout),
478 },
479 },
480 {
481 name: "OptionEndpointWithEnvInsecure",
482 options: []Option{
483 WithEndpoint("env.endpoint:8080"),
484 },
485 envars: map[string]string{
486 "OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
487 },
488 want: config{
489 endpoint: newSetting("env.endpoint:8080"),
490 path: newSetting(defaultPath),
491 insecure: newSetting(true),
492 retryCfg: newSetting(defaultRetryCfg),
493 timeout: newSetting(defaultTimeout),
494 },
495 },
496 }
497
498 for _, tc := range testcases {
499 t.Run(tc.name, func(t *testing.T) {
500 for key, value := range tc.envars {
501 t.Setenv(key, value)
502 }
503
504 var err error
505 t.Cleanup(func(orig otel.ErrorHandler) func() {
506 otel.SetErrorHandler(otel.ErrorHandlerFunc(func(e error) {
507 err = errors.Join(err, e)
508 }))
509 return func() { otel.SetErrorHandler(orig) }
510 }(otel.GetErrorHandler()))
511 c := newConfig(tc.options)
512
513 // Do not compare pointer values.
514 assertTLSConfig(t, tc.want.tlsCfg, c.tlsCfg)
515 var emptyTLS setting[*tls.Config]
516 c.tlsCfg, tc.want.tlsCfg = emptyTLS, emptyTLS
517
518 // Cannot compare funcs, see TestWithProxy.
519 c.proxy = setting[HTTPTransportProxyFunc]{}
520
521 assert.Equal(t, tc.want, c)
522
523 for _, errMsg := range tc.errs {
524 assert.ErrorContains(t, err, errMsg)
525 }
526 })
527 }
528 }
529
530 func assertTLSConfig(t *testing.T, want, got setting[*tls.Config]) {
531 t.Helper()
532
533 assert.Equal(t, want.Set, got.Set, "setting Set")
534 if !want.Set {
535 return
536 }
537
538 if want.Value == nil {
539 assert.Nil(t, got.Value, "*tls.Config")
540 return
541 }
542 require.NotNil(t, got.Value, "*tls.Config")
543
544 if want.Value.RootCAs == nil {
545 assert.Nil(t, got.Value.RootCAs, "*tls.Config.RootCAs")
546 } else if assert.NotNil(t, got.Value.RootCAs, "RootCAs") {
547 assert.True(t, want.Value.RootCAs.Equal(got.Value.RootCAs), "RootCAs equal")
548 }
549 assert.Equal(t, want.Value.Certificates, got.Value.Certificates, "Certificates")
550 }
551
552 func TestWithProxy(t *testing.T) {
553 proxy := func(*http.Request) (*url.URL, error) { return nil, nil }
554 opts := []Option{WithProxy(HTTPTransportProxyFunc(proxy))}
555 c := newConfig(opts)
556
557 assert.True(t, c.proxy.Set)
558 assert.NotNil(t, c.proxy.Value)
559 }
560
561 func TestConvHeaders(t *testing.T) {
562 tests := []struct {
563 name string
564 value string
565 want map[string]string
566 wantErr bool
567 }{
568 {
569 name: "simple test",
570 value: "userId=alice",
571 want: map[string]string{"userId": "alice"},
572 wantErr: false,
573 },
574 {
575 name: "simple test with spaces",
576 value: " userId = alice ",
577 want: map[string]string{"userId": "alice"},
578 wantErr: false,
579 },
580 {
581 name: "simple header conforms to RFC 3986 spec",
582 value: " userId = alice+test ",
583 want: map[string]string{"userId": "alice+test"},
584 wantErr: false,
585 },
586 {
587 name: "multiple headers encoded",
588 value: "userId=alice,serverNode=DF%3A28,isProduction=false",
589 want: map[string]string{
590 "userId": "alice",
591 "serverNode": "DF:28",
592 "isProduction": "false",
593 },
594 wantErr: false,
595 },
596 {
597 name: "multiple headers encoded per RFC 3986 spec",
598 value: "userId=alice+test,serverNode=DF%3A28,isProduction=false,namespace=localhost/test",
599 want: map[string]string{
600 "userId": "alice+test",
601 "serverNode": "DF:28",
602 "isProduction": "false",
603 "namespace": "localhost/test",
604 },
605 wantErr: false,
606 },
607 {
608 name: "invalid headers format",
609 value: "userId:alice",
610 want: map[string]string{},
611 wantErr: true,
612 },
613 {
614 name: "invalid key",
615 value: "%XX=missing,userId=alice",
616 want: map[string]string{
617 "%XX": "missing",
618 "userId": "alice",
619 },
620 wantErr: false,
621 },
622 {
623 name: "invalid value",
624 value: "missing=%XX,userId=alice",
625 want: map[string]string{
626 "userId": "alice",
627 },
628 wantErr: true,
629 },
630 }
631
632 for _, tt := range tests {
633 t.Run(tt.name, func(t *testing.T) {
634 keyValues, err := convHeaders(tt.value)
635 assert.Equal(t, tt.want, keyValues)
636
637 if tt.wantErr {
638 assert.Error(t, err, "expected an error but got nil")
639 } else {
640 assert.NoError(t, err, "expected no error but got one")
641 }
642 })
643 }
644 }