fix: ensure http body is empty (#10396)
This commit is contained in:
parent
90563c9af3
commit
8f02056718
|
|
@ -172,7 +172,9 @@ func (h *HTTP) gatherURL(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer body.Close()
|
if body != nil {
|
||||||
|
defer body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest(h.Method, url, body)
|
request, err := http.NewRequest(h.Method, url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -246,6 +248,10 @@ func (h *HTTP) gatherURL(
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error) {
|
func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error) {
|
||||||
|
if body == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
var reader io.Reader = strings.NewReader(body)
|
var reader io.Reader = strings.NewReader(body)
|
||||||
if contentEncoding == "gzip" {
|
if contentEncoding == "gzip" {
|
||||||
rc, err := internal.CompressWithGzip(reader)
|
rc, err := internal.CompressWithGzip(reader)
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ func TestHTTPWithJSONFormat(t *testing.T) {
|
||||||
address := fakeServer.URL + "/endpoint"
|
address := fakeServer.URL + "/endpoint"
|
||||||
plugin := &httpplugin.HTTP{
|
plugin := &httpplugin.HTTP{
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
metricName := "metricName"
|
metricName := "metricName"
|
||||||
|
|
||||||
|
|
@ -74,6 +75,7 @@ func TestHTTPHeaders(t *testing.T) {
|
||||||
plugin := &httpplugin.HTTP{
|
plugin := &httpplugin.HTTP{
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
Headers: map[string]string{header: headerValue},
|
Headers: map[string]string{header: headerValue},
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
|
|
||||||
p, _ := parsers.NewParser(&parsers.Config{
|
p, _ := parsers.NewParser(&parsers.Config{
|
||||||
|
|
@ -96,6 +98,7 @@ func TestInvalidStatusCode(t *testing.T) {
|
||||||
address := fakeServer.URL + "/endpoint"
|
address := fakeServer.URL + "/endpoint"
|
||||||
plugin := &httpplugin.HTTP{
|
plugin := &httpplugin.HTTP{
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
|
|
||||||
metricName := "metricName"
|
metricName := "metricName"
|
||||||
|
|
@ -120,6 +123,7 @@ func TestSuccessStatusCodes(t *testing.T) {
|
||||||
plugin := &httpplugin.HTTP{
|
plugin := &httpplugin.HTTP{
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
SuccessStatusCodes: []int{200, 202},
|
SuccessStatusCodes: []int{200, 202},
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
|
|
||||||
metricName := "metricName"
|
metricName := "metricName"
|
||||||
|
|
@ -147,6 +151,7 @@ func TestMethod(t *testing.T) {
|
||||||
plugin := &httpplugin.HTTP{
|
plugin := &httpplugin.HTTP{
|
||||||
URLs: []string{fakeServer.URL},
|
URLs: []string{fakeServer.URL},
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
|
|
||||||
p, _ := parsers.NewParser(&parsers.Config{
|
p, _ := parsers.NewParser(&parsers.Config{
|
||||||
|
|
@ -182,6 +187,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
|
||||||
plugin: &httpplugin.HTTP{
|
plugin: &httpplugin.HTTP{
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
@ -196,6 +202,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: "test",
|
Body: "test",
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
@ -210,6 +217,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
|
||||||
URLs: []string{address},
|
URLs: []string{address},
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Body: "test",
|
Body: "test",
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
@ -225,6 +233,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Body: "test",
|
Body: "test",
|
||||||
ContentEncoding: "gzip",
|
ContentEncoding: "gzip",
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, r.Header.Get("Content-Encoding"), "gzip")
|
require.Equal(t, r.Header.Get("Content-Encoding"), "gzip")
|
||||||
|
|
@ -278,6 +287,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
|
||||||
name: "no credentials",
|
name: "no credentials",
|
||||||
plugin: &httpplugin.HTTP{
|
plugin: &httpplugin.HTTP{
|
||||||
URLs: []string{u.String()},
|
URLs: []string{u.String()},
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
require.Len(t, r.Header["Authorization"], 0)
|
require.Len(t, r.Header["Authorization"], 0)
|
||||||
|
|
@ -296,6 +306,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
|
||||||
Scopes: []string{"urn:opc:idm:__myscopes__"},
|
Scopes: []string{"urn:opc:idm:__myscopes__"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Log: testutil.Logger{},
|
||||||
},
|
},
|
||||||
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue