func createClient(c *cli.Context) *wercker.Client { endpoint := c.GlobalString("endpoint") config := &wercker.Config{ Endpoint: endpoint, } if c.GlobalBool("anonymous") { config.Credentials = credentials.Anonymous() } else { token := c.GlobalString("token") if token != "" { config.Credentials = credentials.Token(token) } } client := wercker.NewClient(config) return client }
func TestClientMakeRequestGET200Token(t *testing.T) { result := []byte(`{"key": "some key","value":"some value"}`) tokenSet := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") == "Bearer secret_token" { tokenSet = true } w.WriteHeader(200) w.Write(result) })) defer ts.Close() config := &Config{Endpoint: ts.URL, Credentials: credentials.Token("secret_token")} client := NewClient(config) body, err := client.makeRequest("GET", "/", nil) require.NoError(t, err, "") assert.True(t, tokenSet, "") assert.Equal(t, result, body, "") }