forked from billiford/arcade
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add common client to be used by other packages * update fakes
- Loading branch information
Showing
4 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.