Skip to content

Commit 97b6bdb

Browse files
committed
Add test for content-type header override
1 parent 89c151f commit 97b6bdb

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

elasticsearch_internal_test.go

+94
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"crypto/x509"
2828
"encoding/base64"
2929
"errors"
30+
"github.com/elastic/go-elasticsearch/v8/esapi"
3031
"io/ioutil"
3132
"net/http"
3233
"net/http/httptest"
@@ -865,3 +866,96 @@ func TestNewTypedClient(t *testing.T) {
865866
t.Fatalf("unexpected error: %s", err)
866867
}
867868
}
869+
870+
func TestContentTypeOverride(t *testing.T) {
871+
t.Run("default JSON Content-Type", func(t *testing.T) {
872+
contentType := "application/json"
873+
874+
tp, _ := elastictransport.New(elastictransport.Config{
875+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
876+
Transport: &mockTransp{
877+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
878+
h := request.Header.Get("Content-Type")
879+
if h != contentType {
880+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
881+
}
882+
883+
return &http.Response{
884+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
885+
StatusCode: http.StatusOK,
886+
Status: "OK",
887+
Body: ioutil.NopCloser(strings.NewReader("")),
888+
}, nil
889+
},
890+
},
891+
})
892+
893+
c, _ := NewDefaultClient()
894+
c.Transport = tp
895+
896+
_, _ = c.Search(c.Search.WithBody(strings.NewReader("")))
897+
})
898+
t.Run("overriden CBOR Content-Type functional options style", func(t *testing.T) {
899+
contentType := "application/cbor"
900+
901+
tp, _ := elastictransport.New(elastictransport.Config{
902+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
903+
Transport: &mockTransp{
904+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
905+
h := request.Header.Get("Content-Type")
906+
if h != contentType {
907+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
908+
}
909+
910+
return &http.Response{
911+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
912+
StatusCode: http.StatusOK,
913+
Status: "OK",
914+
Body: ioutil.NopCloser(strings.NewReader("")),
915+
}, nil
916+
},
917+
},
918+
})
919+
920+
c, _ := NewDefaultClient()
921+
c.Transport = tp
922+
923+
_, _ = c.Search(
924+
c.Search.WithHeader(map[string]string{
925+
"Content-Type": contentType,
926+
}),
927+
c.Search.WithBody(strings.NewReader("")),
928+
)
929+
})
930+
t.Run("overriden CBOR Content-Type direct call style", func(t *testing.T) {
931+
contentType := "application/cbor"
932+
933+
tp, _ := elastictransport.New(elastictransport.Config{
934+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
935+
Transport: &mockTransp{
936+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
937+
h := request.Header.Get("Content-Type")
938+
if h != contentType {
939+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
940+
}
941+
942+
return &http.Response{
943+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
944+
StatusCode: http.StatusOK,
945+
Status: "OK",
946+
Body: ioutil.NopCloser(strings.NewReader("")),
947+
}, nil
948+
},
949+
},
950+
})
951+
952+
c, _ := NewDefaultClient()
953+
c.Transport = tp
954+
955+
search := esapi.SearchRequest{}
956+
search.Body = strings.NewReader("")
957+
search.Header = make(map[string][]string)
958+
search.Header.Set("Content-Type", contentType)
959+
search.Do(context.Background(), tp)
960+
})
961+
}

0 commit comments

Comments
 (0)