Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions internal/portal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (c *PortalClient) Get(path string, body []byte) (resp *http.Response, err e

func (c *PortalClient) GetBody(path string) (body []byte, status int, err error) {
resp, err := c.Get(path, []byte{})
if err != nil || resp == nil {
err = fmt.Errorf("GET failed: %w", err)
return
}
defer func() { _ = resp.Body.Close() }()
status = resp.StatusCode

Expand Down
28 changes: 25 additions & 3 deletions internal/portal/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package portal_test
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -36,8 +37,13 @@ var _ = Describe("PortalClient", func() {
getUrl url.URL
getResponse []byte
product portal.Product
apiKey string
apiKeyErr error
)
BeforeEach(func() {
apiKey = "fake-api-key"
apiKeyErr = nil

product = portal.CodesphereProduct
mockEnv = env.NewMockEnv(GinkgoT())
mockHttpClient = portal.NewMockHttpClient(GinkgoT())
Expand All @@ -48,9 +54,10 @@ var _ = Describe("PortalClient", func() {
}
status = http.StatusOK
apiUrl = "fake-portal.com"

})
JustBeforeEach(func() {
mockEnv.EXPECT().GetOmsPortalApi().Return(apiUrl)
mockEnv.EXPECT().GetOmsPortalApiKey().Return("fake-api-key", nil)
mockEnv.EXPECT().GetOmsPortalApiKey().Return(apiKey, apiKeyErr).Maybe()
})
AfterEach(func() {
mockEnv.AssertExpectations(GinkgoT())
Expand All @@ -66,7 +73,7 @@ var _ = Describe("PortalClient", func() {
StatusCode: status,
Body: io.NopCloser(bytes.NewReader(getResponse)),
}, nil
})
}).Maybe()
})

Context("when path starts with a /", func() {
Expand All @@ -86,6 +93,21 @@ var _ = Describe("PortalClient", func() {
Expect(getUrl.String()).To(Equal("fake-portal.com/api/fake"))
})
})

Context("when OMS_PORTAL_API_KEY is unset", func() {
BeforeEach(func() {
apiKey = ""
apiKeyErr = errors.New("fake-error")
})

It("Returns an error", func() {
_, status, err := client.GetBody("/api/fake")
Expect(status).To(Equal(status))
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(MatchRegexp(".*fake-error"))
Expect(getUrl.String()).To(Equal("fake-portal.com/api/fake"))
})
})
})

Describe("ListCodespherePackages", func() {
Expand Down