fix: ensure http body is empty (#10396)

This commit is contained in:
Joshua Powers 2022-01-11 09:22:49 -07:00 committed by GitHub
parent 90563c9af3
commit 8f02056718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -172,7 +172,9 @@ func (h *HTTP) gatherURL(
if err != nil {
return err
}
defer body.Close()
if body != nil {
defer body.Close()
}
request, err := http.NewRequest(h.Method, url, body)
if err != nil {
@ -246,6 +248,10 @@ func (h *HTTP) gatherURL(
}
func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error) {
if body == "" {
return nil, nil
}
var reader io.Reader = strings.NewReader(body)
if contentEncoding == "gzip" {
rc, err := internal.CompressWithGzip(reader)

View File

@ -31,6 +31,7 @@ func TestHTTPWithJSONFormat(t *testing.T) {
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
Log: testutil.Logger{},
}
metricName := "metricName"
@ -74,6 +75,7 @@ func TestHTTPHeaders(t *testing.T) {
plugin := &httpplugin.HTTP{
URLs: []string{address},
Headers: map[string]string{header: headerValue},
Log: testutil.Logger{},
}
p, _ := parsers.NewParser(&parsers.Config{
@ -96,6 +98,7 @@ func TestInvalidStatusCode(t *testing.T) {
address := fakeServer.URL + "/endpoint"
plugin := &httpplugin.HTTP{
URLs: []string{address},
Log: testutil.Logger{},
}
metricName := "metricName"
@ -120,6 +123,7 @@ func TestSuccessStatusCodes(t *testing.T) {
plugin := &httpplugin.HTTP{
URLs: []string{address},
SuccessStatusCodes: []int{200, 202},
Log: testutil.Logger{},
}
metricName := "metricName"
@ -147,6 +151,7 @@ func TestMethod(t *testing.T) {
plugin := &httpplugin.HTTP{
URLs: []string{fakeServer.URL},
Method: "POST",
Log: testutil.Logger{},
}
p, _ := parsers.NewParser(&parsers.Config{
@ -182,6 +187,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
plugin: &httpplugin.HTTP{
Method: "POST",
URLs: []string{address},
Log: testutil.Logger{},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
@ -196,6 +202,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
URLs: []string{address},
Method: "POST",
Body: "test",
Log: testutil.Logger{},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
@ -210,6 +217,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
URLs: []string{address},
Method: "GET",
Body: "test",
Log: testutil.Logger{},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
@ -225,6 +233,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
Method: "GET",
Body: "test",
ContentEncoding: "gzip",
Log: testutil.Logger{},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
require.Equal(t, r.Header.Get("Content-Encoding"), "gzip")
@ -278,6 +287,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
name: "no credentials",
plugin: &httpplugin.HTTP{
URLs: []string{u.String()},
Log: testutil.Logger{},
},
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
require.Len(t, r.Header["Authorization"], 0)
@ -296,6 +306,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
Scopes: []string{"urn:opc:idm:__myscopes__"},
},
},
Log: testutil.Logger{},
},
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)