Skip to content

Commit

Permalink
178270494 client (#4)
Browse files Browse the repository at this point in the history
* add common client to be used by other packages

* update fakes
  • Loading branch information
billiford authored May 24, 2021
1 parent a412fda commit 28ae864
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package arcade

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)

const (
defaultURL = `http://localhost:1982`
)

//go:generate counterfeiter . Client
type Client interface {
Token(string) (string, error)
}

// NewDefaultClient creates a new instance of client with an API Key
// that calls the default URL endpoint.
func NewDefaultClient(apiKey string) Client {
return NewClient(defaultURL, apiKey)
}

// NewClient creates a new instance of client with a defined API Key
// and URL endpoint.
func NewClient(url, apiKey string) Client {
return &client{
apiKey: apiKey,
url: url,
}
}

type client struct {
apiKey string
url string
}

// Token returns a token for a given provider.
func (c *client) Token(tokenProvider string) (string, error) {
req, err := http.NewRequest(http.MethodGet, c.url+"/tokens", nil)
if err != nil {
return "", err
}

q := url.Values{}
q.Add("provider", tokenProvider)
req.URL.RawQuery = q.Encode()

req.Header.Add("Api-Key", c.apiKey)

res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()

if res.StatusCode < 200 || res.StatusCode > 399 {
return "", fmt.Errorf("error getting token: %s", res.Status)
}

var response struct {
Token string `json:"token"`
}

b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}

err = json.Unmarshal(b, &response)
if err != nil {
return "", err
}

return response.Token, nil
}
122 changes: 122 additions & 0 deletions pkg/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package arcade_test

import (
"net/http"

. "github.com/homedepot/arcade/pkg"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)

var _ = Describe("Client", func() {
var (
server *ghttp.Server
client Client
err error
token string
provider string
)

BeforeEach(func() {
provider = "google"
server = ghttp.NewServer()
client = NewClient(server.URL(), "test-api-key")
})

AfterEach(func() {
server.Close()
})

Describe("#NewDefaultClient", func() {
BeforeEach(func() {
client = NewDefaultClient("test-api-key")
})

It("succeeds", func() {
})
})

Describe("#Token", func() {
JustBeforeEach(func() {
token, err = client.Token(provider)
})

When("the uri is invalid", func() {
BeforeEach(func() {
client = NewClient("::haha", "test-api-key")
})

It("returns an error", func() {
Expect(err).ToNot(BeNil())
})
})

When("the server is not reachable", func() {
BeforeEach(func() {
server.Close()
})

It("returns an error", func() {
Expect(err).ToNot(BeNil())
})
})

When("the response is not 2XX", func() {
BeforeEach(func() {
server.AppendHandlers(
ghttp.RespondWith(http.StatusInternalServerError, nil),
)
})

It("returns an error", func() {
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("error getting token: 500 Internal Server Error"))
})
})

When("the server returns bad data", func() {
BeforeEach(func() {
server.AppendHandlers(
ghttp.RespondWith(http.StatusOK, ";{["),
)
})

It("returns an error", func() {
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("invalid character ';' looking for beginning of value"))
})
})

When("provider is rancher", func() {
BeforeEach(func() {
provider = "rancher"
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyHeaderKV("api-key", "test-api-key"),
ghttp.VerifyRequest(http.MethodGet, "/tokens", "provider=rancher"),
ghttp.RespondWith(http.StatusOK, `{"token":"some.bearer.token"}`),
))
})

It("succeeds", func() {
Expect(err).To(BeNil())
Expect(token).To(Equal("some.bearer.token"))
})
})

When("it succeeds", func() {
BeforeEach(func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyHeaderKV("api-key", "test-api-key"),
ghttp.VerifyRequest(http.MethodGet, "/tokens", "provider=google"),
ghttp.RespondWith(http.StatusOK, `{"token":"some.bearer.token"}`),
))
})

It("succeeds", func() {
Expect(err).To(BeNil())
Expect(token).To(Equal("some.bearer.token"))
})
})
})
})
13 changes: 13 additions & 0 deletions pkg/pkg_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package arcade_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestPkg(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Pkg Suite")
}
116 changes: 116 additions & 0 deletions pkg/pkgfakes/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 28ae864

Please sign in to comment.