Skip to content

Commit 4e64b67

Browse files
committed
Add test for content-type header override
1 parent df410c5 commit 4e64b67

File tree

1 file changed

+128
-30
lines changed

1 file changed

+128
-30
lines changed

elasticsearch_internal_test.go

Lines changed: 128 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
//go:build !integration
1819
// +build !integration
1920

2021
package elasticsearch
2122

2223
import (
2324
"bytes"
25+
"context"
2426
"crypto/tls"
2527
"crypto/x509"
2628
"encoding/base64"
@@ -35,6 +37,7 @@ import (
3537
"strings"
3638
"testing"
3739

40+
"github.com/elastic/go-elasticsearch/v7/esapi"
3841
"github.com/elastic/go-elasticsearch/v7/estransport"
3942
)
4043

@@ -474,7 +477,7 @@ func TestGenuineCheckInfo(t *testing.T) {
474477
name string
475478
info info
476479
wantErr bool
477-
err error
480+
err error
478481
}{
479482
{
480483
name: "Genuine Elasticsearch 7.14.0",
@@ -486,7 +489,7 @@ func TestGenuineCheckInfo(t *testing.T) {
486489
Tagline: "You Know, for Search",
487490
},
488491
wantErr: false,
489-
err: nil,
492+
err: nil,
490493
},
491494
{
492495
name: "Genuine Elasticsearch 6.15.1",
@@ -498,7 +501,7 @@ func TestGenuineCheckInfo(t *testing.T) {
498501
Tagline: "You Know, for Search",
499502
},
500503
wantErr: false,
501-
err: nil,
504+
err: nil,
502505
},
503506
{
504507
name: "Not so genuine Elasticsearch 7 major",
@@ -510,7 +513,7 @@ func TestGenuineCheckInfo(t *testing.T) {
510513
Tagline: "You Know, for Search",
511514
},
512515
wantErr: true,
513-
err: errors.New(unknownProduct),
516+
err: errors.New(unknownProduct),
514517
},
515518
{
516519
name: "Not so genuine Elasticsearch 6 major",
@@ -522,7 +525,7 @@ func TestGenuineCheckInfo(t *testing.T) {
522525
Tagline: "You Know, for Fun",
523526
},
524527
wantErr: true,
525-
err: errors.New(unknownProduct),
528+
err: errors.New(unknownProduct),
526529
},
527530
{
528531
name: "Way older Elasticsearch major",
@@ -534,7 +537,7 @@ func TestGenuineCheckInfo(t *testing.T) {
534537
Tagline: "You Know, for Fun",
535538
},
536539
wantErr: true,
537-
err: errors.New(unknownProduct),
540+
err: errors.New(unknownProduct),
538541
},
539542
{
540543
name: "Elasticsearch oss",
@@ -546,7 +549,7 @@ func TestGenuineCheckInfo(t *testing.T) {
546549
Tagline: "You Know, for Search",
547550
},
548551
wantErr: true,
549-
err: errors.New(unsupportedProduct),
552+
err: errors.New(unsupportedProduct),
550553
},
551554
}
552555
for _, tt := range tests {
@@ -632,7 +635,7 @@ func TestResponseCheckOnly(t *testing.T) {
632635
useResponseCheckOnly: true,
633636
response: &http.Response{
634637
StatusCode: http.StatusOK,
635-
Body: ioutil.NopCloser(strings.NewReader("{}")),
638+
Body: ioutil.NopCloser(strings.NewReader("{}")),
636639
},
637640
wantErr: true,
638641
},
@@ -646,42 +649,42 @@ func TestResponseCheckOnly(t *testing.T) {
646649
{
647650
name: "Valid request, 500 response",
648651
useResponseCheckOnly: false,
649-
response: &http.Response{
652+
response: &http.Response{
650653
StatusCode: http.StatusInternalServerError,
651-
Body: ioutil.NopCloser(strings.NewReader("")),
654+
Body: ioutil.NopCloser(strings.NewReader("")),
652655
},
653-
requestErr: nil,
654-
wantErr: true,
656+
requestErr: nil,
657+
wantErr: true,
655658
},
656659
{
657660
name: "Valid request, 404 response",
658661
useResponseCheckOnly: false,
659-
response: &http.Response{
662+
response: &http.Response{
660663
StatusCode: http.StatusNotFound,
661-
Body: ioutil.NopCloser(strings.NewReader("")),
664+
Body: ioutil.NopCloser(strings.NewReader("")),
662665
},
663-
requestErr: nil,
664-
wantErr: true,
666+
requestErr: nil,
667+
wantErr: true,
665668
},
666669
{
667670
name: "Valid request, 403 response",
668671
useResponseCheckOnly: false,
669-
response: &http.Response{
672+
response: &http.Response{
670673
StatusCode: http.StatusForbidden,
671-
Body: ioutil.NopCloser(strings.NewReader("")),
674+
Body: ioutil.NopCloser(strings.NewReader("")),
672675
},
673-
requestErr: nil,
674-
wantErr: false,
676+
requestErr: nil,
677+
wantErr: false,
675678
},
676679
{
677680
name: "Valid request, 401 response",
678681
useResponseCheckOnly: false,
679-
response: &http.Response{
682+
response: &http.Response{
680683
StatusCode: http.StatusUnauthorized,
681-
Body: ioutil.NopCloser(strings.NewReader("")),
684+
Body: ioutil.NopCloser(strings.NewReader("")),
682685
},
683-
requestErr: nil,
684-
wantErr: false,
686+
requestErr: nil,
687+
wantErr: false,
685688
},
686689
}
687690
for _, tt := range tests {
@@ -700,7 +703,6 @@ func TestResponseCheckOnly(t *testing.T) {
700703
}
701704
}
702705

703-
704706
func TestProductCheckError(t *testing.T) {
705707
var requestPaths []string
706708
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -737,7 +739,6 @@ func TestProductCheckError(t *testing.T) {
737739
}
738740
}
739741

740-
741742
func TestCompatibilityHeader(t *testing.T) {
742743
client, err := NewClient(Config{
743744
Transport: &mockTransp{RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -779,8 +780,6 @@ func TestCompatibilityHeader(t *testing.T) {
779780
)
780781
}
781782

782-
783-
784783
func TestFingerprint(t *testing.T) {
785784
body := []byte(`{"body": true"}"`)
786785
cert, err := tls.X509KeyPair([]byte(`-----BEGIN CERTIFICATE-----
@@ -844,7 +843,7 @@ tuSVaQmm5eqgaAxMamBXSyw1lir07byemyuEDg0mJ1rNUGsAY8P+LWr579gvKMme
844843
defer server.Close()
845844

846845
config := Config{
847-
Addresses: []string{server.URL},
846+
Addresses: []string{server.URL},
848847
DisableRetry: true,
849848
}
850849

@@ -872,4 +871,103 @@ tuSVaQmm5eqgaAxMamBXSyw1lir07byemyuEDg0mJ1rNUGsAY8P+LWr579gvKMme
872871
if !bytes.Equal(data, body) {
873872
t.Fatalf("unexpected payload returned: expected: %s, got: %s", body, data)
874873
}
875-
}
874+
}
875+
876+
func TestContentTypeOverride(t *testing.T) {
877+
t.Run("default JSON Content-Type", func(t *testing.T) {
878+
contentType := "application/json"
879+
880+
tp, _ := estransport.New(estransport.Config{
881+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
882+
Transport: &mockTransp{
883+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
884+
if request.URL.Path != "/" {
885+
h := request.Header.Get("Content-Type")
886+
if h != contentType {
887+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
888+
}
889+
}
890+
891+
return &http.Response{
892+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
893+
StatusCode: http.StatusOK,
894+
Status: "OK",
895+
Body: ioutil.NopCloser(strings.NewReader("")),
896+
}, nil
897+
},
898+
},
899+
})
900+
901+
c, _ := NewDefaultClient()
902+
c.Transport = tp
903+
904+
_, _ = c.Search(c.Search.WithBody(strings.NewReader("")))
905+
})
906+
t.Run("overriden CBOR Content-Type functional options style", func(t *testing.T) {
907+
contentType := "application/cbor"
908+
909+
tp, _ := estransport.New(estransport.Config{
910+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
911+
Transport: &mockTransp{
912+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
913+
if request.URL.Path != "/" {
914+
h := request.Header.Get("Content-Type")
915+
if h != contentType {
916+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
917+
}
918+
}
919+
920+
return &http.Response{
921+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
922+
StatusCode: http.StatusOK,
923+
Status: "OK",
924+
Body: ioutil.NopCloser(strings.NewReader("")),
925+
}, nil
926+
},
927+
},
928+
})
929+
930+
c, _ := NewDefaultClient()
931+
c.Transport = tp
932+
933+
_, _ = c.Search(
934+
c.Search.WithHeader(map[string]string{
935+
"Content-Type": contentType,
936+
}),
937+
c.Search.WithBody(strings.NewReader("")),
938+
)
939+
})
940+
t.Run("overriden CBOR Content-Type direct call style", func(t *testing.T) {
941+
contentType := "application/cbor"
942+
943+
tp, _ := estransport.New(estransport.Config{
944+
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
945+
Transport: &mockTransp{
946+
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
947+
if request.URL.Path != "/" {
948+
h := request.Header.Get("Content-Type")
949+
if h != contentType {
950+
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
951+
}
952+
}
953+
954+
return &http.Response{
955+
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
956+
StatusCode: http.StatusOK,
957+
Status: "OK",
958+
Body: ioutil.NopCloser(strings.NewReader("")),
959+
}, nil
960+
},
961+
},
962+
})
963+
964+
c, _ := NewDefaultClient()
965+
c.Transport = tp
966+
967+
search := esapi.SearchRequest{}
968+
search.Body = strings.NewReader("")
969+
search.Header = make(map[string][]string)
970+
search.Header.Set("Content-Type", contentType)
971+
search.Do(context.Background(), tp)
972+
})
973+
}

0 commit comments

Comments
 (0)