From a9fcb775e561294a20b83ce8ed2da4fe8f9ac3ce Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Sep 2023 13:57:48 +0200 Subject: [PATCH 1/2] integration-cli: fix getTestTokenService not sending header This utility was setting the content-type header after WriteHeader was called, and the header was not sent because of that. Signed-off-by: Sebastiaan van Stijn --- integration-cli/docker_cli_push_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-cli/docker_cli_push_test.go b/integration-cli/docker_cli_push_test.go index 29065e5a70a98..4b0c317a114b5 100644 --- a/integration-cli/docker_cli_push_test.go +++ b/integration-cli/docker_cli_push_test.go @@ -235,13 +235,13 @@ func getTestTokenService(status int, body string, retries int) *httptest.Server return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() if retries > 0 { - w.WriteHeader(http.StatusServiceUnavailable) w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusServiceUnavailable) w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`)) retries-- } else { - w.WriteHeader(status) w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) w.Write([]byte(body)) } mu.Unlock() From 2c89640ab936ac27e8f4254784b6ac8acfd8b793 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Sep 2023 14:03:59 +0200 Subject: [PATCH 2/2] distribution: TestPullSchema2Config fix test response 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 --- distribution/pull_v2_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/distribution/pull_v2_test.go b/distribution/pull_v2_test.go index 010c90bd25c48..8c32a7fb579d4 100644 --- a/distribution/pull_v2_test.go +++ b/distribution/pull_v2_test.go @@ -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,