Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client-go: add an Error() function on Request #115685

Merged
merged 1 commit into from Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion staging/src/k8s.io/client-go/rest/request.go
Expand Up @@ -481,7 +481,13 @@ func (r *Request) Body(obj interface{}) *Request {
return r
}

// URL returns the current working URL.
// Error returns any error encountered constructing the request, if any.
func (r *Request) Error() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test please, at least one covering the issue you just reported

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — one test to check that Error() returns exactly err, and another to check the scheme-related behaviour.

return r.err
}

// URL returns the current working URL. Check the result of Error() to ensure
// that the returned URL is valid.
func (r *Request) URL() *url.URL {
p := r.pathPrefix
if r.namespaceSet && len(r.namespace) > 0 {
Expand Down
20 changes: 20 additions & 0 deletions staging/src/k8s.io/client-go/rest/request_test.go
Expand Up @@ -269,6 +269,26 @@ func TestRequestVersionedParamsFromListOptions(t *testing.T) {
}
}

func TestRequestVersionedParamsWithInvalidScheme(t *testing.T) {
parameterCodec := runtime.NewParameterCodec(runtime.NewScheme())
r := (&Request{c: &RESTClient{content: ClientContentConfig{GroupVersion: v1.SchemeGroupVersion}}})
r.VersionedParams(&v1.PodExecOptions{Stdin: false, Stdout: true},
parameterCodec)

if r.Error() == nil {
t.Errorf("should have recorded an error: %#v", r.params)
}
}

func TestRequestError(t *testing.T) {
// Invalid body, see TestRequestBody()
r := (&Request{}).Body([]string{"test"})

if r.Error() != r.err {
t.Errorf("getter should be identical to reference: %#v %#v", r.Error(), r.err)
}
}

func TestRequestURI(t *testing.T) {
r := (&Request{}).Param("foo", "a")
r.Prefix("other")
Expand Down