@@ -915,3 +915,69 @@ func TestMaxRetries(t *testing.T) {
915915 })
916916 }
917917}
918+
919+ func TestCompatibilityHeader (t * testing.T ) {
920+ tests := []struct {
921+ name string
922+ compatibilityHeader bool
923+ bodyPresent bool
924+ expectsHeader []string
925+ }{
926+ {
927+ name : "Compatibility header disabled" ,
928+ compatibilityHeader : false ,
929+ bodyPresent : false ,
930+ expectsHeader : []string {"application/json" },
931+ },
932+ {
933+ name : "Compatibility header enabled" ,
934+ compatibilityHeader : true ,
935+ bodyPresent : false ,
936+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=7" },
937+ },
938+ {
939+ name : "Compatibility header enabled with body" ,
940+ compatibilityHeader : true ,
941+ bodyPresent : true ,
942+ expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=7" },
943+ },
944+ }
945+
946+ for _ , test := range tests {
947+ t .Run (test .name , func (t * testing.T ) {
948+ compatibilityHeader = test .compatibilityHeader
949+
950+ c , _ := New (Config {
951+ URLs : []* url.URL {{}},
952+ Transport : & mockTransp {
953+ RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
954+ if test .compatibilityHeader {
955+ if ! reflect .DeepEqual (req .Header ["Accept" ], test .expectsHeader ) {
956+ t .Errorf ("Compatibility header enabled but header is, not in request headers, got: %s, want: %s" , req .Header ["Accept" ], test .expectsHeader )
957+ }
958+ }
959+ if test .bodyPresent {
960+ if ! reflect .DeepEqual (req .Header ["Content-Type" ], test .expectsHeader ) {
961+ t .Errorf ("Compatibility header with Body enabled, not in request headers, got: %s, want: %s" , req .Header ["Content-Type" ], test .expectsHeader )
962+ }
963+ }
964+
965+ return & http.Response {
966+ StatusCode : http .StatusOK ,
967+ Status : "MOCK" ,
968+ }, nil
969+ },
970+ },
971+ })
972+
973+ req := & http.Request {URL : & url.URL {}, Header : make (http.Header )}
974+ if test .bodyPresent {
975+ req .Body = ioutil .NopCloser (strings .NewReader ("{}" ))
976+ }
977+
978+ _ , _ = c .Perform (req )
979+
980+ compatibilityHeader = false
981+ })
982+ }
983+ }
0 commit comments