Debian Codegraph @

exporters/otlp/otlplog/otlploggrpc/config_test.go

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