From f8a3d28a40a3bc1ea6955c7c4907a4e0e56ecf98 Mon Sep 17 00:00:00 2001 From: Shwetha Gururaj Date: Wed, 19 Jun 2024 16:53:50 -0400 Subject: [PATCH 1/4] cfcli-55: Remove Content-Type header for download droplet --- api/cloudcontroller/ccv3/request.go | 2 +- api/cloudcontroller/ccv3/requester.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/api/cloudcontroller/ccv3/request.go b/api/cloudcontroller/ccv3/request.go index dc799fc59ec..46812f4f4ba 100644 --- a/api/cloudcontroller/ccv3/request.go +++ b/api/cloudcontroller/ccv3/request.go @@ -69,7 +69,7 @@ func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*c request.Header.Set("Accept", "application/json") } - if request.Header.Get("Content-Type") == "" { + if passedRequest.RequestName != internal.GetDropletBitsRequest && request.Header.Get("Content-Type") == "" { request.Header.Set("Content-Type", "application/json") } diff --git a/api/cloudcontroller/ccv3/requester.go b/api/cloudcontroller/ccv3/requester.go index f0982fc6293..a6269a71aa6 100644 --- a/api/cloudcontroller/ccv3/requester.go +++ b/api/cloudcontroller/ccv3/requester.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "runtime" + "strings" "code.cloudfoundry.org/cli/api/cloudcontroller" "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" @@ -160,6 +161,10 @@ func (requester *RealRequester) MakeRequestSendReceiveRaw( return nil, nil, err } + if strings.Contains(URL, "droplet") && strings.Contains(URL, "download") { + request.Header.Del("Content-Type") + } + response := cloudcontroller.Response{} err = requester.connection.Make(request, &response) From f266226adba56a2b3e69704be1b4092b30651083 Mon Sep 17 00:00:00 2001 From: Shwetha Gururaj Date: Thu, 20 Jun 2024 22:46:16 -0400 Subject: [PATCH 2/4] Add tests Fix lint target in Makefile --- Makefile | 10 ++++---- api/cloudcontroller/ccv3/request.go | 10 +++++++- api/cloudcontroller/ccv3/requester.go | 5 ---- api/cloudcontroller/ccv3/requester_test.go | 29 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index fdd2d96a4e6..adcefc83062 100644 --- a/Makefile +++ b/Makefile @@ -143,7 +143,7 @@ integration-tests-full-ci: install-test-deps integration-cleanup lint: format ## Runs all linters and formatters @echo "Running linters..." - golangci-lint run --exclude-dirs cf --exclude-dirs fixtures --exclude-dirs plugin --exclude-dirs command/plugin + golangci-lint run --skip-dirs cf --skip-dirs fixtures --skip-dirs plugin --skip-dirs command/plugin @echo "No lint errors!" # TODO: version specific tagging for all these builds @@ -163,10 +163,10 @@ out/cf-cli_linux_i686: $(GOSRC) $(REQUIRED_FOR_STATIC_BINARY) \ -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_i686 . -out/cf-cli_linux_x86-64: $(GOSRC) - CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ - $(REQUIRED_FOR_STATIC_BINARY) \ - -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_x86-64 . +# lint: $(GOSRC) +# CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ +# $(REQUIRED_FOR_STATIC_BINARY) \ +# -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_x86-64 . out/cf-cli_linux_arm64: $(GOSRC) CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build \ diff --git a/api/cloudcontroller/ccv3/request.go b/api/cloudcontroller/ccv3/request.go index 46812f4f4ba..65b988ac784 100644 --- a/api/cloudcontroller/ccv3/request.go +++ b/api/cloudcontroller/ccv3/request.go @@ -3,6 +3,7 @@ package ccv3 import ( "io" "net/http" + "strings" "code.cloudfoundry.org/cli/api/cloudcontroller" "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" @@ -57,6 +58,7 @@ func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*c } request.Header = http.Header{} + if passedRequest.Header != nil { request.Header = passedRequest.Header } @@ -69,9 +71,15 @@ func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*c request.Header.Set("Accept", "application/json") } - if passedRequest.RequestName != internal.GetDropletBitsRequest && request.Header.Get("Content-Type") == "" { + if !isDownloadDroplet(passedRequest.URL, passedRequest.RequestName) && request.Header.Get("Content-Type") == "" { request.Header.Set("Content-Type", "application/json") + } else if isDownloadDroplet(passedRequest.URL, passedRequest.RequestName) && request.Header.Get("Content-Type") != "" { + request.Header.Del("Content-Type") } return cloudcontroller.NewRequest(request, passedRequest.Body), nil } + +func isDownloadDroplet(URL string, requestName string) bool { + return (strings.Contains(URL, "droplet") && strings.Contains(URL, "download")) || (requestName == internal.GetDropletBitsRequest) +} diff --git a/api/cloudcontroller/ccv3/requester.go b/api/cloudcontroller/ccv3/requester.go index a6269a71aa6..f0982fc6293 100644 --- a/api/cloudcontroller/ccv3/requester.go +++ b/api/cloudcontroller/ccv3/requester.go @@ -7,7 +7,6 @@ import ( "io" "net/http" "runtime" - "strings" "code.cloudfoundry.org/cli/api/cloudcontroller" "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" @@ -161,10 +160,6 @@ func (requester *RealRequester) MakeRequestSendReceiveRaw( return nil, nil, err } - if strings.Contains(URL, "droplet") && strings.Contains(URL, "download") { - request.Header.Del("Content-Type") - } - response := cloudcontroller.Response{} err = requester.connection.Make(request, &response) diff --git a/api/cloudcontroller/ccv3/requester_test.go b/api/cloudcontroller/ccv3/requester_test.go index 9eb78324da7..e9afe08c362 100644 --- a/api/cloudcontroller/ccv3/requester_test.go +++ b/api/cloudcontroller/ccv3/requester_test.go @@ -1069,6 +1069,10 @@ var _ = Describe("shared request helpers", func() { CombineHandlers( VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/manifest"), VerifyHeaderKV("Accept", "application/x-yaml"), + func(w http.ResponseWriter, req *http.Request) { + // key := http.CanonicalHeaderKey("Content-Type") + Expect(req.Header).To(HaveKey("Content-Type"), "Header Content-Type is not present") + }, RespondWith( http.StatusOK, expectedResponseBody, @@ -1134,6 +1138,31 @@ var _ = Describe("shared request helpers", func() { }) }) }) + + Context("Download a droplet", func() { + BeforeEach(func() { + requestName = internal.GetDropletBitsRequest + uriParams = internal.Params{"droplet_guid": "some-droplet-guid"} + }) + + When("The server returns an unauthorized error", func() { + BeforeEach(func() { + server.AppendHandlers( + CombineHandlers( + VerifyRequest(http.MethodGet, "/v3/droplets/some-droplet-guid/download"), + func(w http.ResponseWriter, req *http.Request) { + Expect(req.Header).NotTo(HaveKey("Content-Type"), "Header Content-Type is present") + }, + RespondWith(http.StatusUnauthorized, "", http.Header{}), + ), + ) + }) + + It("fails", func() { + Expect(executeErr).To(HaveOccurred()) + }) + }) + }) }) Describe("MakeRequestSendRaw", func() { From 13aec8880f8e121e3a955b8c0dcf320425a01e89 Mon Sep 17 00:00:00 2001 From: Shwetha Gururaj Date: Thu, 20 Jun 2024 22:52:28 -0400 Subject: [PATCH 3/4] Remove commented code --- Makefile | 5 ----- api/cloudcontroller/ccv3/requester_test.go | 1 - 2 files changed, 6 deletions(-) diff --git a/Makefile b/Makefile index adcefc83062..9ad3f72e72c 100644 --- a/Makefile +++ b/Makefile @@ -162,11 +162,6 @@ out/cf-cli_linux_i686: $(GOSRC) CGO_ENABLED=0 GOARCH=386 GOOS=linux go build \ $(REQUIRED_FOR_STATIC_BINARY) \ -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_i686 . - -# lint: $(GOSRC) -# CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ -# $(REQUIRED_FOR_STATIC_BINARY) \ -# -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_x86-64 . out/cf-cli_linux_arm64: $(GOSRC) CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build \ diff --git a/api/cloudcontroller/ccv3/requester_test.go b/api/cloudcontroller/ccv3/requester_test.go index e9afe08c362..6b078ece01b 100644 --- a/api/cloudcontroller/ccv3/requester_test.go +++ b/api/cloudcontroller/ccv3/requester_test.go @@ -1070,7 +1070,6 @@ var _ = Describe("shared request helpers", func() { VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/manifest"), VerifyHeaderKV("Accept", "application/x-yaml"), func(w http.ResponseWriter, req *http.Request) { - // key := http.CanonicalHeaderKey("Content-Type") Expect(req.Header).To(HaveKey("Content-Type"), "Header Content-Type is not present") }, RespondWith( From 41dc0f712fb9ab995b5001703323a36c01e648b8 Mon Sep 17 00:00:00 2001 From: Shwetha Gururaj Date: Fri, 21 Jun 2024 09:47:32 -0400 Subject: [PATCH 4/4] Bring back out/cf-cli_linux_x86-64 target --- Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9ad3f72e72c..fdd2d96a4e6 100644 --- a/Makefile +++ b/Makefile @@ -143,7 +143,7 @@ integration-tests-full-ci: install-test-deps integration-cleanup lint: format ## Runs all linters and formatters @echo "Running linters..." - golangci-lint run --skip-dirs cf --skip-dirs fixtures --skip-dirs plugin --skip-dirs command/plugin + golangci-lint run --exclude-dirs cf --exclude-dirs fixtures --exclude-dirs plugin --exclude-dirs command/plugin @echo "No lint errors!" # TODO: version specific tagging for all these builds @@ -162,6 +162,11 @@ out/cf-cli_linux_i686: $(GOSRC) CGO_ENABLED=0 GOARCH=386 GOOS=linux go build \ $(REQUIRED_FOR_STATIC_BINARY) \ -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_i686 . + +out/cf-cli_linux_x86-64: $(GOSRC) + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ + $(REQUIRED_FOR_STATIC_BINARY) \ + -ldflags "$(LD_FLAGS_LINUX)" -o out/cf-cli_linux_x86-64 . out/cf-cli_linux_arm64: $(GOSRC) CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build \