Skip to content

Commit 2d57f4c

Browse files
committed
Include TestConvHeaders for err assertion. Update percent-encoded headers test case
1 parent 84dccd6 commit 2d57f4c

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

exporters/otlp/otlplog/otlploggrpc/config_test.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func TestNewConfig(t *testing.T) {
8080
require.NoError(t, err, "testing TLS config")
8181

8282
headers := map[string]string{"a": "A"}
83-
percentDecodedHeaders := map[string]string{"user%2Did": "42", "user%20name": "alice smith"}
8483
rc := retry.Config{}
8584

8685
dialOptions := []grpc.DialOption{grpc.WithUserAgent("test-agent")}
@@ -453,7 +452,7 @@ func TestNewConfig(t *testing.T) {
453452
endpoint: newSetting("env.endpoint:8080"),
454453
insecure: newSetting(false),
455454
tlsCfg: newSetting(tlsCfg),
456-
headers: newSetting(percentDecodedHeaders),
455+
headers: newSetting(map[string]string{"user%2Did": "42", "user%20name": "alice smith"}),
457456
compression: newSetting(GzipCompression),
458457
timeout: newSetting(15 * time.Second),
459458
retryCfg: newSetting(defaultRetryCfg),
@@ -536,24 +535,28 @@ func assertTLSConfig(t *testing.T, want, got setting[*tls.Config]) {
536535

537536
func TestConvHeaders(t *testing.T) {
538537
tests := []struct {
539-
name string
540-
value string
541-
want map[string]string
538+
name string
539+
value string
540+
want map[string]string
541+
wantErr bool
542542
}{
543543
{
544-
name: "simple test",
545-
value: "userId=alice",
546-
want: map[string]string{"userId": "alice"},
544+
name: "simple test",
545+
value: "userId=alice",
546+
want: map[string]string{"userId": "alice"},
547+
wantErr: false,
547548
},
548549
{
549-
name: "simple test with spaces",
550-
value: " userId = alice ",
551-
want: map[string]string{"userId": "alice"},
550+
name: "simple test with spaces",
551+
value: " userId = alice ",
552+
want: map[string]string{"userId": "alice"},
553+
wantErr: false,
552554
},
553555
{
554-
name: "simple header conforms to RFC 3986 spec",
555-
value: " userId = alice+test ",
556-
want: map[string]string{"userId": "alice+test"},
556+
name: "simple header conforms to RFC 3986 spec",
557+
value: " userId = alice+test ",
558+
want: map[string]string{"userId": "alice+test"},
559+
wantErr: false,
557560
},
558561
{
559562
name: "multiple headers encoded",
@@ -563,6 +566,7 @@ func TestConvHeaders(t *testing.T) {
563566
"serverNode": "DF:28",
564567
"isProduction": "false",
565568
},
569+
wantErr: false,
566570
},
567571
{
568572
name: "multiple headers encoded per RFC 3986 spec",
@@ -573,11 +577,13 @@ func TestConvHeaders(t *testing.T) {
573577
"isProduction": "false",
574578
"namespace": "localhost/test",
575579
},
580+
wantErr: false,
576581
},
577582
{
578-
name: "invalid headers format",
579-
value: "userId:alice",
580-
want: map[string]string{},
583+
name: "invalid headers format",
584+
value: "userId:alice",
585+
want: map[string]string{},
586+
wantErr: true,
581587
},
582588
{
583589
name: "invalid key",
@@ -586,20 +592,28 @@ func TestConvHeaders(t *testing.T) {
586592
"%XX": "missing",
587593
"userId": "alice",
588594
},
595+
wantErr: false,
589596
},
590597
{
591598
name: "invalid value",
592599
value: "missing=%XX,userId=alice",
593600
want: map[string]string{
594601
"userId": "alice",
595602
},
603+
wantErr: true,
596604
},
597605
}
598606

599607
for _, tt := range tests {
600608
t.Run(tt.name, func(t *testing.T) {
601-
keyValues, _ := convHeaders(tt.value)
609+
keyValues, err := convHeaders(tt.value)
602610
assert.Equal(t, tt.want, keyValues)
611+
612+
if tt.wantErr {
613+
assert.Error(t, err, "expected an error but got nil")
614+
} else {
615+
assert.NoError(t, err, "expected no error but got one")
616+
}
603617
})
604618
}
605619
}

exporters/otlp/otlplog/otlploghttp/config_test.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func TestNewConfig(t *testing.T) {
8080
require.NoError(t, err, "testing TLS config")
8181

8282
headers := map[string]string{"a": "A"}
83-
percentDecodedHeaders := map[string]string{"user%2Did": "42", "user%20name": "alice smith"}
8483
rc := retry.Config{}
8584

8685
testcases := []struct {
@@ -384,7 +383,7 @@ func TestNewConfig(t *testing.T) {
384383
path: newSetting("/prefix"),
385384
insecure: newSetting(false),
386385
tlsCfg: newSetting(tlsCfg),
387-
headers: newSetting(percentDecodedHeaders),
386+
headers: newSetting(map[string]string{"user%2Did": "42", "user%20name": "alice smith"}),
388387
compression: newSetting(GzipCompression),
389388
timeout: newSetting(15 * time.Second),
390389
retryCfg: newSetting(defaultRetryCfg),
@@ -480,24 +479,28 @@ func TestWithProxy(t *testing.T) {
480479

481480
func TestConvHeaders(t *testing.T) {
482481
tests := []struct {
483-
name string
484-
value string
485-
want map[string]string
482+
name string
483+
value string
484+
want map[string]string
485+
wantErr bool
486486
}{
487487
{
488-
name: "simple test",
489-
value: "userId=alice",
490-
want: map[string]string{"userId": "alice"},
488+
name: "simple test",
489+
value: "userId=alice",
490+
want: map[string]string{"userId": "alice"},
491+
wantErr: false,
491492
},
492493
{
493-
name: "simple test with spaces",
494-
value: " userId = alice ",
495-
want: map[string]string{"userId": "alice"},
494+
name: "simple test with spaces",
495+
value: " userId = alice ",
496+
want: map[string]string{"userId": "alice"},
497+
wantErr: false,
496498
},
497499
{
498-
name: "simple header conforms to RFC 3986 spec",
499-
value: " userId = alice+test ",
500-
want: map[string]string{"userId": "alice+test"},
500+
name: "simple header conforms to RFC 3986 spec",
501+
value: " userId = alice+test ",
502+
want: map[string]string{"userId": "alice+test"},
503+
wantErr: false,
501504
},
502505
{
503506
name: "multiple headers encoded",
@@ -507,6 +510,7 @@ func TestConvHeaders(t *testing.T) {
507510
"serverNode": "DF:28",
508511
"isProduction": "false",
509512
},
513+
wantErr: false,
510514
},
511515
{
512516
name: "multiple headers encoded per RFC 3986 spec",
@@ -517,11 +521,13 @@ func TestConvHeaders(t *testing.T) {
517521
"isProduction": "false",
518522
"namespace": "localhost/test",
519523
},
524+
wantErr: false,
520525
},
521526
{
522-
name: "invalid headers format",
523-
value: "userId:alice",
524-
want: map[string]string{},
527+
name: "invalid headers format",
528+
value: "userId:alice",
529+
want: map[string]string{},
530+
wantErr: true,
525531
},
526532
{
527533
name: "invalid key",
@@ -530,20 +536,28 @@ func TestConvHeaders(t *testing.T) {
530536
"%XX": "missing",
531537
"userId": "alice",
532538
},
539+
wantErr: false,
533540
},
534541
{
535542
name: "invalid value",
536543
value: "missing=%XX,userId=alice",
537544
want: map[string]string{
538545
"userId": "alice",
539546
},
547+
wantErr: true,
540548
},
541549
}
542550

543551
for _, tt := range tests {
544552
t.Run(tt.name, func(t *testing.T) {
545-
keyValues, _ := convHeaders(tt.value)
553+
keyValues, err := convHeaders(tt.value)
546554
assert.Equal(t, tt.want, keyValues)
555+
556+
if tt.wantErr {
557+
assert.Error(t, err, "expected an error but got nil")
558+
} else {
559+
assert.NoError(t, err, "expected no error but got one")
560+
}
547561
})
548562
}
549563
}

0 commit comments

Comments
 (0)