Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Fix lint target in Makefile
  • Loading branch information
gururajsh committed Jun 21, 2024
1 parent 27108ff commit 7e24bc9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
10 changes: 5 additions & 5 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 @@ -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 \
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 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)
}
5 changes: 0 additions & 5 deletions api/cloudcontroller/ccv3/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"runtime"
"strings"

"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions api/cloudcontroller/ccv3/requester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7e24bc9

Please sign in to comment.