mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
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:
parent
d06d93d5b1
commit
7d02ce2073
@ -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
|
||||
|
@ -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}
|
||||
|
Loading…
x
Reference in New Issue
Block a user