Skip to content

Commit

Permalink
distribution: TestPullSchema2Config fix test response
Browse files Browse the repository at this point in the history
The test was depending on the client constructing an error based on the
http-status code, and the client not reading the response body if the
response was not a JSON response.

This fix;

- adds the correct content-type headers in the response
- includes error-messages in the response
- adds additional tests to cover both the plain (non-JSON) and JSON
  error responses, as well as an empty response.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 27, 2023
1 parent a9fcb77 commit 2c89640
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions distribution/pull_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ func TestPullSchema2Config(t *testing.T) {
name: "unauthorized",
handler: func(callCount int, w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
// FIXME: current distribution client does not handle plain-text error-responses, so this response is ignored.
_, _ = w.Write([]byte("you need to be authenticated"))
},
expectError: "unauthorized: authentication required",
expectAttempts: 1,
},
{
name: "unauthorized JSON",
handler: func(callCount int, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(` { "errors": [{"code": "UNAUTHORIZED", "message": "you need to be authenticated", "detail": "more detail"}]}`))
},
expectError: "unauthorized: you need to be authenticated",
expectAttempts: 1,
},
{
name: "unauthorized JSON no body",
handler: func(callCount int, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
},
expectError: "unauthorized: authentication required",
expectAttempts: 1,
Expand Down

0 comments on commit 2c89640

Please sign in to comment.