-
Notifications
You must be signed in to change notification settings - Fork 0
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
PMM-13658 fix updates error response #9
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package update | ||
|
||
import ( | ||
"errors" | ||
"github.com/containrrr/watchtower/pkg/types" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please format imports |
||
|
||
func TestHandleError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
error error | ||
expectedStatus int | ||
}{ | ||
{ | ||
name: "no error returns OK", | ||
error: nil, | ||
expectedStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "validation error returns message", | ||
error: types.NewValidationError("no new image available"), | ||
expectedStatus: http.StatusPreconditionFailed, | ||
}, | ||
{ | ||
name: "generic server error", | ||
error: errors.New("server error"), | ||
expectedStatus: http.StatusInternalServerError, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_ = httptest.NewRequest(http.MethodGet, "/v1/update", nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No actually, was a spill over |
||
rr := httptest.NewRecorder() | ||
|
||
handleError(rr, tt.error) | ||
|
||
if status := rr.Result().StatusCode; status != tt.expectedStatus { | ||
t.Errorf("the handler wrote status code %d, expected: %d", status, tt.expectedStatus) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errors.Is
was only working with sentinel errors (e.g errors created witherrors.New
) so it was failing in this case.