Skip to content

Commit d5ce72d

Browse files
authored
Add support for HTTP API compatibility header (#266) (#273)
* Transport: Add compatibility header
1 parent eba806e commit d5ce72d

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

estransport/estransport.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,26 @@ import (
3030
"os"
3131
"regexp"
3232
"runtime"
33+
"strconv"
3334
"strings"
3435
"sync"
3536
"time"
3637

3738
"github.com/elastic/go-elasticsearch/v7/internal/version"
3839
)
3940

40-
// Version returns the package version as a string.
41-
//
42-
const Version = version.Client
41+
const (
42+
// Version returns the package version as a string.
43+
Version = version.Client
44+
45+
// esCompatHeader defines the env var for Compatibility header.
46+
esCompatHeader = "ELASTIC_CLIENT_APIVERSIONING"
47+
)
4348

4449
var (
4550
userAgent string
4651
metaHeader string
52+
compatibilityHeader bool
4753
reGoVersion = regexp.MustCompile(`go(\d+\.\d+\..+)`)
4854

4955
defaultMaxRetries = 3
@@ -53,6 +59,9 @@ var (
5359
func init() {
5460
userAgent = initUserAgent()
5561
metaHeader = initMetaHeader()
62+
63+
compatHeaderEnv := os.Getenv(esCompatHeader)
64+
compatibilityHeader, _ = strconv.ParseBool(compatHeaderEnv)
5665
}
5766

5867
// Interface defines the interface for HTTP client.
@@ -221,6 +230,14 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
221230
err error
222231
)
223232

233+
// Compatibility Header
234+
if compatibilityHeader {
235+
if req.Body != nil {
236+
req.Header.Set("Content-Type", "application/vnd.elasticsearch+json;compatible-with=7")
237+
}
238+
req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=7")
239+
}
240+
224241
// Record metrics, when enabled
225242
if c.metrics != nil {
226243
c.metrics.Lock()

estransport/estransport_internal_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)