cli: set content length on operator api requests (#14634)

http.NewRequestWithContext will only set the right value for
Content-Length if the input is *bytes.Buffer, *bytes.Reader, or
*strings.Reader [0].

Since os.Stdin is an os.File, POST requests made with the `nomad
operator api` command would always have Content-Length set to -1, which
is interpreted as an unknown length by web servers.

[0]: https://pkg.go.dev/net/http#NewRequestWithContext
This commit is contained in:
Luiz Aoqui
2022-09-26 14:21:40 -04:00
committed by GitHub
parent 34e89210a7
commit fd7337c141
3 changed files with 67 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
@@ -170,3 +171,49 @@ func Test_pathToURL(t *testing.T) {
})
}
}
// TestOperatorAPICommand_ContentLength tests that requests have the proper
// ContentLength set.
//
// Don't run it in parallel as it modifies the package's Stdin variable.
func TestOperatorAPICommand_ContentLength(t *testing.T) {
contentLength := make(chan int, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
contentLength <- int(r.ContentLength)
}))
defer ts.Close()
// Setup a temp file to act as stdin.
input := []byte("test-input")
fakeStdin, err := os.CreateTemp("", "fake-stdin")
require.NoError(t, err)
defer os.Remove(fakeStdin.Name())
_, err = fakeStdin.Write(input)
require.NoError(t, err)
_, err = fakeStdin.Seek(0, 0)
require.NoError(t, err)
// Override the package's Stdin variable for testing.
Stdin = fakeStdin
defer func() { Stdin = os.Stdin }()
// Setup command.
buf := bytes.NewBuffer(nil)
ui := &cli.BasicUi{
ErrorWriter: buf,
Writer: buf,
}
cmd := &OperatorAPICommand{Meta: Meta{Ui: ui}}
// Assert that a request has the expected content length.
exitCode := cmd.Run([]string{"-address=" + ts.URL, "/v1/jobs"})
require.Zero(t, exitCode, buf.String())
select {
case l := <-contentLength:
require.Equal(t, len(input), l)
case <-time.After(10 * time.Second):
t.Fatalf("timed out waiting for request")
}
}