cleanup req.Context() and ResponseWrapper

Kubernetes-commit: 968adfa99362f733ef82f4aabb34a59dbbd6e56a
This commit is contained in:
Mike Danese
2020-01-27 18:52:27 -08:00
committed by Kubernetes Publisher
parent b136e9eb2b
commit 09009e85ca
12 changed files with 39 additions and 49 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package rest
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
@@ -79,7 +80,7 @@ func TestDoRequestSuccess(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := c.Get().Prefix("test").Do().Raw()
body, err := c.Get().Prefix("test").Do(context.Background()).Raw()
testParam := TestParam{actualError: err, expectingError: false, expCreated: true,
expStatus: status, testBody: true, testBodyErrorIsNotNil: false}
@@ -107,7 +108,7 @@ func TestDoRequestFailed(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = c.Get().Do().Error()
err = c.Get().Do(context.Background()).Error()
if err == nil {
t.Errorf("unexpected non-error")
}
@@ -146,7 +147,7 @@ func TestDoRawRequestFailed(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := c.Get().Do().Raw()
body, err := c.Get().Do(context.Background()).Raw()
if err == nil || body == nil {
t.Errorf("unexpected non-error: %#v", body)
@@ -170,7 +171,7 @@ func TestDoRequestCreated(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Prefix("test").Do().WasCreated(&created).Raw()
body, err := c.Get().Prefix("test").Do(context.Background()).WasCreated(&created).Raw()
testParam := TestParam{actualError: err, expectingError: false, expCreated: true,
expStatus: status, testBody: false}
@@ -185,7 +186,7 @@ func TestDoRequestNotCreated(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Prefix("test").Do().WasCreated(&created).Raw()
body, err := c.Get().Prefix("test").Do(context.Background()).WasCreated(&created).Raw()
testParam := TestParam{actualError: err, expectingError: false, expCreated: false,
expStatus: expectedStatus, testBody: false}
validate(testParam, t, body, fakeHandler)
@@ -200,7 +201,7 @@ func TestDoRequestAcceptedNoContentReturned(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Prefix("test").Do().WasCreated(&created).Raw()
body, err := c.Get().Prefix("test").Do(context.Background()).WasCreated(&created).Raw()
testParam := TestParam{actualError: err, expectingError: false, expCreated: false,
testBody: false}
validate(testParam, t, body, fakeHandler)
@@ -214,7 +215,7 @@ func TestBadRequest(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Prefix("test").Do().WasCreated(&created).Raw()
body, err := c.Get().Prefix("test").Do(context.Background()).WasCreated(&created).Raw()
testParam := TestParam{actualError: err, expectingError: true, expCreated: false,
testBody: true}
validate(testParam, t, body, fakeHandler)

View File

@@ -94,9 +94,9 @@ func TestRequestSetsHeaders(t *testing.T) {
r.c.Client = server
// Check if all "issue" methods are setting headers.
_ = r.Do()
_, _ = r.Watch()
_, _ = r.Stream()
_ = r.Do(context.Background())
_, _ = r.Watch(context.Background())
_, _ = r.Stream(context.Background())
}
func TestRequestWithErrorWontChange(t *testing.T) {
@@ -1059,7 +1059,7 @@ func TestRequestWatch(t *testing.T) {
for _, testCase := range testCases {
t.Run("", func(t *testing.T) {
testCase.Request.backoff = &NoBackoff{}
watch, err := testCase.Request.Watch()
watch, err := testCase.Request.Watch(context.Background())
hasErr := err != nil
if hasErr != testCase.Err {
t.Fatalf("expected %t, got %t: %v", testCase.Err, hasErr, err)
@@ -1162,7 +1162,7 @@ func TestRequestStream(t *testing.T) {
}
for i, testCase := range testCases {
testCase.Request.backoff = &NoBackoff{}
body, err := testCase.Request.Stream()
body, err := testCase.Request.Stream(context.Background())
hasErr := err != nil
if hasErr != testCase.Err {
t.Errorf("%d: expected %t, got %t: %v", i, testCase.Err, hasErr, err)
@@ -1240,7 +1240,7 @@ func TestRequestDo(t *testing.T) {
}
for i, testCase := range testCases {
testCase.Request.backoff = &NoBackoff{}
body, err := testCase.Request.Do().Raw()
body, err := testCase.Request.Do(context.Background()).Raw()
hasErr := err != nil
if hasErr != testCase.Err {
t.Errorf("%d: expected %t, got %t: %v", i, testCase.Err, hasErr, err)
@@ -1272,7 +1272,7 @@ func TestDoRequestNewWay(t *testing.T) {
Suffix("baz").
Timeout(time.Second).
Body([]byte(reqBody)).
Do().Get()
Do(context.Background()).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
@@ -1323,7 +1323,7 @@ func TestBackoffLifecycle(t *testing.T) {
t.Errorf("Backoff is %v instead of %v", thisBackoff, sec)
}
now := clock.Now()
request.DoRaw()
request.DoRaw(context.Background())
elapsed := clock.Since(now)
if clock.Since(now) != thisBackoff {
t.Errorf("CalculatedBackoff not honored by clock: Expected time of %v, but got %v ", thisBackoff, elapsed)
@@ -1372,7 +1372,7 @@ func TestCheckRetryClosesBody(t *testing.T) {
Suffix("baz").
Timeout(time.Second).
Body([]byte(strings.Repeat("abcd", 1000))).
DoRaw()
DoRaw(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v %#v", err, err)
}
@@ -1405,7 +1405,7 @@ func TestConnectionResetByPeerIsRetried(t *testing.T) {
backoff: backoff,
}
// We expect two retries of "connection reset by peer" and the success.
_, err := req.Do().Raw()
_, err := req.Do(context.Background()).Raw()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
@@ -1445,7 +1445,7 @@ func TestCheckRetryHandles429And5xx(t *testing.T) {
Suffix("baz").
Timeout(time.Second).
Body([]byte(strings.Repeat("abcd", 1000))).
DoRaw()
DoRaw(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v %#v", err, err)
}
@@ -1481,7 +1481,7 @@ func BenchmarkCheckRetryClosesBody(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := requests[i].DoRaw(); err != nil {
if _, err := requests[i].DoRaw(context.Background()); err != nil {
b.Fatalf("Unexpected error (%d/%d): %v", i, b.N, err)
}
}
@@ -1510,7 +1510,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
Prefix("foo").
Timeout(time.Second).
Body(bytes.NewBuffer(reqBodyExpected)).
Do().Get()
Do(context.Background()).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
@@ -1549,7 +1549,7 @@ func TestDoRequestNewWayObj(t *testing.T) {
Resource("foo").
Timeout(time.Second).
Body(reqObj).
Do().Get()
Do(context.Background()).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
@@ -1603,7 +1603,7 @@ func TestDoRequestNewWayFile(t *testing.T) {
Prefix("foo/bar", "baz").
Timeout(time.Second).
Body(file.Name()).
Do().WasCreated(&wasCreated).Get()
Do(context.Background()).WasCreated(&wasCreated).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
@@ -1648,7 +1648,7 @@ func TestWasCreated(t *testing.T) {
Prefix("foo/bar", "baz").
Timeout(time.Second).
Body(reqBodyExpected).
Do().WasCreated(&wasCreated).Get()
Do(context.Background()).WasCreated(&wasCreated).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
@@ -1831,7 +1831,7 @@ func TestWatch(t *testing.T) {
defer testServer.Close()
s := testRESTClient(t, testServer)
watching, err := s.Get().Prefix("path/to/watch/thing").Watch()
watching, err := s.Get().Prefix("path/to/watch/thing").Watch(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
@@ -1891,7 +1891,7 @@ func TestWatchNonDefaultContentType(t *testing.T) {
contentConfig := defaultContentConfig()
contentConfig.ContentType = "application/vnd.kubernetes.protobuf"
s := testRESTClientWithConfig(t, testServer, contentConfig)
watching, err := s.Get().Prefix("path/to/watch/thing").Watch()
watching, err := s.Get().Prefix("path/to/watch/thing").Watch(context.Background())
if err != nil {
t.Fatalf("Unexpected error")
}
@@ -1948,7 +1948,7 @@ func TestWatchUnknownContentType(t *testing.T) {
defer testServer.Close()
s := testRESTClient(t, testServer)
_, err := s.Get().Prefix("path/to/watch/thing").Watch()
_, err := s.Get().Prefix("path/to/watch/thing").Watch(context.Background())
if err == nil {
t.Fatalf("Expected to fail due to lack of known stream serialization for content type")
}
@@ -1970,7 +1970,7 @@ func TestStream(t *testing.T) {
defer testServer.Close()
s := testRESTClient(t, testServer)
readCloser, err := s.Get().Prefix("path/to/stream/thing").Stream()
readCloser, err := s.Get().Prefix("path/to/stream/thing").Stream(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -2028,9 +2028,8 @@ func TestDoContext(t *testing.T) {
c := testRESTClient(t, testServer)
_, err := c.Verb("GET").
Context(ctx).
Prefix("foo").
DoRaw()
DoRaw(ctx)
if err == nil {
t.Fatal("Expected context cancellation error")
}