client v2: check for empty request from the context

If the simpleHTTPClient.Do is called and the context has a nil request, return an error early.

Fixes #12718

Signed-off-by: David Lanouette <David.Lanouette@GMail.com>
This commit is contained in:
David Lanouette 2021-03-01 16:46:30 -05:00
parent d06d93d5b1
commit 7d02ce2073
2 changed files with 26 additions and 1 deletions

View File

@ -521,15 +521,22 @@ type simpleHTTPClient struct {
headerTimeout time.Duration
}
// NoRequestError indicates that the HTTPRequest object could not be found
// or was nil. No processing could continue.
var NoRequestError = errors.New("No HTTPRequest was available")
func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
req := act.HTTPRequest(c.endpoint)
if req == nil {
return nil, nil, NoRequestError
}
if err := printcURL(req); err != nil {
return nil, nil, err
}
isWait := false
if req != nil && req.URL != nil {
if req.URL != nil {
ws := req.URL.Query().Get("wait")
if len(ws) != 0 {
var err error

View File

@ -158,6 +158,24 @@ func TestSimpleHTTPClientDoError(t *testing.T) {
}
}
type nilAction struct{}
func (a *nilAction) HTTPRequest(url.URL) *http.Request {
return nil
}
func TestSimpleHTTPClientDoNilRequest(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}
tr.errchan <- errors.New("fixture")
_, _, err := c.Do(context.Background(), &nilAction{})
if err != NoRequestError {
t.Fatalf("expected non-nil error, got nil")
}
}
func TestSimpleHTTPClientDoCancelContext(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}