Skip to content

Commit

Permalink
Merge pull request #616 from CircleCI-Public/fix-host
Browse files Browse the repository at this point in the history
Fix issue where config.Endpoint was being passed as a url prefix instead of config.Host
  • Loading branch information
kelvinkfli authored Aug 5, 2021
2 parents ba9cd22 + bcddf04 commit 6ec121d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ func ListOrbCategories(cl *graphql.Client) (*OrbCategoriesForListing, error) {
// FollowProject initiates an API request to follow a specific project on
// CircleCI. Project slugs are case-sensitive.
func FollowProject(config settings.Config, vcs string, owner string, projectName string) (FollowedProject, error) {
requestPath := fmt.Sprintf("%s/api/v1.1/project/%s/%s/%s/follow", config.Endpoint, vcs, owner, projectName)
requestPath := fmt.Sprintf("%s/api/v1.1/project/%s/%s/%s/follow", config.Host, vcs, owner, projectName)
r, err := http.NewRequest(http.MethodPost, requestPath, nil)
if err != nil {
return FollowedProject{}, err
Expand Down
70 changes: 70 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package api

import (
"fmt"
"net/http"
"strings"
"testing"

"github.com/CircleCI-Public/circleci-cli/mock"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

func TestOrbVersionRef(t *testing.T) {
Expand Down Expand Up @@ -29,3 +38,64 @@ func TestOrbVersionRef(t *testing.T) {
t.Errorf("Expected %s, got %s", expected, orbRef)
}
}

func TestFollowProject(t *testing.T) {
table := []struct {
label string
transportFn func(r *http.Request) (*http.Response, error)
expFollowedProject FollowedProject
expErr string
}{
{
label: "fails when http client returns an error",
transportFn: func(r *http.Request) (*http.Response, error) {
return nil, errors.New("test error")
},
expErr: "test error",
},
{
label: "fails when json decoding fails",
transportFn: func(r *http.Request) (*http.Response, error) {
return mock.NewHTTPResponse(200, "{/"), nil
},
expErr: "invalid character '/'",
},
{
label: "returns a followed project succesfully",
transportFn: func(r *http.Request) (*http.Response, error) {
if r.URL.String() != "https://circleci.com/api/v1.1/project/github/test-user/test-project/follow" {
panic(fmt.Sprintf("unexpected url: %s", r.URL.String()))
}
return mock.NewHTTPResponse(200, `{"message": "test-message", "followed": true}`), nil
},
expFollowedProject: FollowedProject{
Message: "test-message",
Followed: true,
},
},
}

for _, ts := range table {
t.Run(ts.label, func(t *testing.T) {
httpClient := mock.NewHTTPClient(ts.transportFn)
config := settings.Config{
Host: "https://circleci.com",
HTTPClient: httpClient,
}

fp, err := FollowProject(config, "github", "test-user", "test-project")
if err != nil {
if ts.expErr == "" || !strings.Contains(err.Error(), ts.expErr) {
t.Fatalf("unexpected error following project: %s", err.Error())
}
return
}

if ts.expErr != "" {
t.Fatalf("unexpected nil error - expected: %s", ts.expErr)
}

assert.Check(t, cmp.DeepEqual(fp, ts.expFollowedProject))
})
}
}
28 changes: 28 additions & 0 deletions mock/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mock

import (
"io/ioutil"
"net/http"
"strings"
)

type transport struct {
f func(*http.Request) (*http.Response, error)
}

func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
return t.f(r)
}

func NewHTTPClient(f func(*http.Request) (*http.Response, error)) *http.Client {
return &http.Client{
Transport: &transport{f: f},
}
}

func NewHTTPResponse(code int, body string) *http.Response {
return &http.Response{
StatusCode: code,
Body: ioutil.NopCloser(strings.NewReader(body)),
}
}

0 comments on commit 6ec121d

Please sign in to comment.