Merge pull request #12737 from davidlanouette/12718

client v2: check for empty request from the context
This commit is contained in:
Gyuho Lee 2021-03-01 23:57:58 -08:00 committed by GitHub
commit 102096ade2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -521,15 +521,22 @@ type simpleHTTPClient struct {
headerTimeout time.Duration
}
// ErrNoRequest indicates that the HTTPRequest object could not be found
// or was nil. No processing could continue.
var ErrNoRequest = 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, ErrNoRequest
}
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 != ErrNoRequest {
t.Fatalf("expected non-nil error, got nil")
}
}
func TestSimpleHTTPClientDoCancelContext(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}