@@ -32,6 +32,7 @@ import (
32
32
"os"
33
33
"reflect"
34
34
"regexp"
35
+ "strconv"
35
36
"strings"
36
37
"testing"
37
38
@@ -407,7 +408,7 @@ func TestResponseCheckOnly(t *testing.T) {
407
408
name : "Valid answer without header" ,
408
409
response : & http.Response {
409
410
StatusCode : http .StatusOK ,
410
- Body : ioutil .NopCloser (strings .NewReader ("{}" )),
411
+ Body : ioutil .NopCloser (strings .NewReader ("{}" )),
411
412
},
412
413
wantErr : true ,
413
414
},
@@ -423,7 +424,7 @@ func TestResponseCheckOnly(t *testing.T) {
423
424
name : "Valid answer without header and response check" ,
424
425
response : & http.Response {
425
426
StatusCode : http .StatusOK ,
426
- Body : ioutil .NopCloser (strings .NewReader ("{}" )),
427
+ Body : ioutil .NopCloser (strings .NewReader ("{}" )),
427
428
},
428
429
wantErr : true ,
429
430
},
@@ -558,3 +559,94 @@ func TestFingerprint(t *testing.T) {
558
559
t .Fatalf ("unexpected payload returned: expected: %s, got: %s" , body , data )
559
560
}
560
561
}
562
+
563
+ func TestCompatibilityHeader (t * testing.T ) {
564
+ tests := []struct {
565
+ name string
566
+ envVar bool
567
+ configVar bool
568
+ compatibilityHeader bool
569
+ bodyPresent bool
570
+ expectsHeader []string
571
+ }{
572
+ {
573
+ name : "Compatibility header disabled" ,
574
+ compatibilityHeader : false ,
575
+ bodyPresent : false ,
576
+ envVar : false ,
577
+ configVar : false ,
578
+ expectsHeader : []string {"application/json" },
579
+ },
580
+ {
581
+ name : "Compatibility header enabled with env var" ,
582
+ compatibilityHeader : true ,
583
+ bodyPresent : false ,
584
+ envVar : true ,
585
+ configVar : false ,
586
+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=8" },
587
+ },
588
+ {
589
+ name : "Compatibility header enabled with body with env var" ,
590
+ compatibilityHeader : true ,
591
+ bodyPresent : true ,
592
+ envVar : true ,
593
+ configVar : false ,
594
+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=8" },
595
+ },
596
+ {
597
+ name : "Compatibility header enabled in conf" ,
598
+ compatibilityHeader : true ,
599
+ bodyPresent : false ,
600
+ envVar : false ,
601
+ configVar : true ,
602
+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=8" },
603
+ },
604
+ {
605
+ name : "Compatibility header enabled with body in conf" ,
606
+ compatibilityHeader : true ,
607
+ bodyPresent : true ,
608
+ envVar : false ,
609
+ configVar : true ,
610
+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=8" },
611
+ },
612
+ }
613
+
614
+ for _ , test := range tests {
615
+ t .Run (test .name , func (t * testing.T ) {
616
+ t .Setenv (esCompatHeader , strconv .FormatBool (test .compatibilityHeader ))
617
+
618
+ c , _ := NewClient (Config {
619
+ EnableCompatibilityMode : test .configVar ,
620
+ Addresses : []string {},
621
+ Transport : & mockTransp {
622
+ RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
623
+ if test .compatibilityHeader {
624
+ if ! reflect .DeepEqual (req .Header ["Accept" ], test .expectsHeader ) {
625
+ t .Errorf ("Compatibility header enabled but header is, not in request headers, got: %s, want: %s" , req .Header ["Accept" ], test .expectsHeader )
626
+ }
627
+
628
+ if test .bodyPresent {
629
+ if ! reflect .DeepEqual (req .Header ["Content-Type" ], test .expectsHeader ) {
630
+ t .Errorf ("Compatibility header with Body enabled, not in request headers, got: %s, want: %s" , req .Header ["Content-Type" ], test .expectsHeader )
631
+ }
632
+ }
633
+ }
634
+
635
+ return & http.Response {
636
+ StatusCode : http .StatusOK ,
637
+ Status : "MOCK" ,
638
+ Body : ioutil .NopCloser (strings .NewReader ("{}" )),
639
+ }, nil
640
+ },
641
+ },
642
+ })
643
+
644
+ req := & http.Request {URL : & url.URL {}, Header : make (http.Header )}
645
+ if test .bodyPresent {
646
+ req .Body = ioutil .NopCloser (strings .NewReader ("{}" ))
647
+ }
648
+
649
+ _ , _ = c .Perform (req )
650
+ })
651
+ }
652
+ }
0 commit comments