fix: use readers over closers in http input (#11083)
This commit is contained in:
parent
6b697db11e
commit
dad330c3e1
|
|
@ -1,9 +1,11 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -95,9 +97,6 @@ func (h *HTTP) gatherURL(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
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 {
|
||||||
|
|
@ -175,7 +174,7 @@ func (h *HTTP) gatherURL(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error) {
|
func makeRequestBodyReader(contentEncoding, body string) (io.Reader, error) {
|
||||||
if body == "" {
|
if body == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
@ -186,10 +185,14 @@ func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return rc, nil
|
data, err := ioutil.ReadAll(rc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bytes.NewReader(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return io.NopCloser(reader), nil
|
return reader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,40 @@ func TestHTTPHeaders(t *testing.T) {
|
||||||
require.NoError(t, acc.GatherError(plugin.Gather))
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHTTPContentLengthHeader(t *testing.T) {
|
||||||
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/endpoint" {
|
||||||
|
if r.Header.Get("Content-Length") != "" {
|
||||||
|
_, _ = w.Write([]byte(simpleJSON))
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer fakeServer.Close()
|
||||||
|
|
||||||
|
address := fakeServer.URL + "/endpoint"
|
||||||
|
plugin := &httpplugin.HTTP{
|
||||||
|
URLs: []string{address},
|
||||||
|
Headers: map[string]string{},
|
||||||
|
Body: "{}",
|
||||||
|
Log: testutil.Logger{},
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
||||||
|
return parsers.NewParser(&parsers.Config{
|
||||||
|
DataFormat: "json",
|
||||||
|
MetricName: "metricName",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
var acc testutil.Accumulator
|
||||||
|
require.NoError(t, plugin.Init())
|
||||||
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
||||||
|
}
|
||||||
|
|
||||||
func TestInvalidStatusCode(t *testing.T) {
|
func TestInvalidStatusCode(t *testing.T) {
|
||||||
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue