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

Remove Content-Type header while downloading droplets [v8] #2975

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 .

out/cf-cli_linux_x86-64: $(GOSRC)
a-b marked this conversation as resolved.
Show resolved Hide resolved
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 \
Expand Down
10 changes: 9 additions & 1 deletion api/cloudcontroller/ccv3/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*c
}

request.Header = http.Header{}

if passedRequest.Header != nil {
request.Header = passedRequest.Header
}
Expand All @@ -69,9 +71,15 @@ func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*c
request.Header.Set("Accept", "application/json")
}

if 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)
}
28 changes: 28 additions & 0 deletions api/cloudcontroller/ccv3/requester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,9 @@ 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) {
Expect(req.Header).To(HaveKey("Content-Type"), "Header Content-Type is not present")
},
RespondWith(
http.StatusOK,
expectedResponseBody,
Expand Down Expand Up @@ -1134,6 +1137,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() {
Expand Down
Loading