Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: only set default content-type when not set #536

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions elasticsearch_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"crypto/x509"
"encoding/base64"
"errors"
"github.com/elastic/go-elasticsearch/v8/esapi"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -865,3 +866,96 @@ func TestNewTypedClient(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
}

func TestContentTypeOverride(t *testing.T) {
t.Run("default JSON Content-Type", func(t *testing.T) {
contentType := "application/json"

tp, _ := elastictransport.New(elastictransport.Config{
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
Transport: &mockTransp{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
h := request.Header.Get("Content-Type")
if h != contentType {
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
}

return &http.Response{
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
},
},
})

c, _ := NewDefaultClient()
c.Transport = tp

_, _ = c.Search(c.Search.WithBody(strings.NewReader("")))
})
t.Run("overriden CBOR Content-Type functional options style", func(t *testing.T) {
contentType := "application/cbor"

tp, _ := elastictransport.New(elastictransport.Config{
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
Transport: &mockTransp{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
h := request.Header.Get("Content-Type")
if h != contentType {
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
}

return &http.Response{
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
},
},
})

c, _ := NewDefaultClient()
c.Transport = tp

_, _ = c.Search(
c.Search.WithHeader(map[string]string{
"Content-Type": contentType,
}),
c.Search.WithBody(strings.NewReader("")),
)
})
t.Run("overriden CBOR Content-Type direct call style", func(t *testing.T) {
contentType := "application/cbor"

tp, _ := elastictransport.New(elastictransport.Config{
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
Transport: &mockTransp{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
h := request.Header.Get("Content-Type")
if h != contentType {
t.Fatalf("unexpected content-type, wanted %s, got: %s", contentType, h)
}

return &http.Response{
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
},
},
})

c, _ := NewDefaultClient()
c.Transport = tp

search := esapi.SearchRequest{}
search.Body = strings.NewReader("")
search.Header = make(map[string][]string)
search.Header.Set("Content-Type", contentType)
search.Do(context.Background(), tp)
})
}
15 changes: 6 additions & 9 deletions internal/build/cmd/generate/commands/gensource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ import (
)

// Generator represents the "gensource" generator.
//
type Generator struct {
b bytes.Buffer

Endpoint *Endpoint
}

// Output returns the generator output.
//
func (g *Generator) Output() (io.Reader, error) {
g.genHeader()
g.genConstructor()
Expand All @@ -52,7 +50,6 @@ func (g *Generator) Output() (io.Reader, error) {
}

// OutputFormatted returns a formatted generator output.
//
func (g *Generator) OutputFormatted() (io.Reader, error) {
out, err := g.Output()
if err != nil {
Expand Down Expand Up @@ -825,12 +822,6 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
req.URL.RawQuery = q.Encode()
}` + "\n\n")

if g.Endpoint.Body != nil {
g.w(`if r.Body != nil {
req.Header[headerContentType] = headerContentTypeJSON
}` + "\n\n")
}

g.w(`if len(r.Header) > 0 {
if len(req.Header) == 0 {
req.Header = r.Header
Expand All @@ -843,6 +834,12 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
}
}` + "\n\n")

if g.Endpoint.Body != nil {
g.w(`if r.Body != nil && req.Header.Get(headerContentType) == "" {
req.Header[headerContentType] = headerContentTypeJSON
}` + "\n\n")
}

g.w(`if ctx != nil {
req = req.WithContext(ctx)
}` + "\n\n")
Expand Down