Skip to content

Commit

Permalink
Remove Content-Type header while downloading droplets [v8] (#2975)
Browse files Browse the repository at this point in the history
Remove Content-Type header for download droplet
  • Loading branch information
gururajsh authored Jun 25, 2024
1 parent 56666f0 commit 01d27c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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

0 comments on commit 01d27c5

Please sign in to comment.