Skip to content

Commit

Permalink
Replace FakeHTTP with httpmock which is now compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed May 7, 2020
1 parent 256d319 commit 0a4d4ee
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 210 deletions.
8 changes: 5 additions & 3 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"reflect"
"testing"

"github.com/cli/cli/pkg/httpmock"
)

func eq(t *testing.T, got interface{}, expected interface{}) {
Expand All @@ -15,7 +17,7 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
}

func TestGraphQL(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(
ReplaceTripper(http),
AddHeader("Authorization", "token OTOKEN"),
Expand All @@ -40,7 +42,7 @@ func TestGraphQL(t *testing.T) {
}

func TestGraphQLError(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

response := struct{}{}
Expand All @@ -52,7 +54,7 @@ func TestGraphQLError(t *testing.T) {
}

func TestRESTGetDelete(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}

client := NewClient(
ReplaceTripper(http),
Expand Down
120 changes: 0 additions & 120 deletions api/fake_http.go

This file was deleted.

3 changes: 2 additions & 1 deletion api/queries_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"testing"

"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/httpmock"
)

func TestIssueList(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`
Expand Down
14 changes: 0 additions & 14 deletions api/queries_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,6 @@ func RepoNetwork(client *Client, repos []ghrepo.Interface) (RepoNetworkResult, e
return result, nil
}

func RepoNetworkStubResponse(owner, repo, permission string) string {
return fmt.Sprintf(`
{ "data": { "repo_000": {
"id": "REPOID",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "master"
},
"viewerPermission": "%s"
} } }
`, repo, owner, permission)
}

// repositoryV3 is the repository result from GitHub API v3
type repositoryV3 struct {
NodeID string
Expand Down
4 changes: 3 additions & 1 deletion api/queries_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/cli/cli/pkg/httpmock"
)

func Test_RepoCreate(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`{}`))
Expand Down
5 changes: 2 additions & 3 deletions command/pr_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"testing"

"github.com/cli/cli/api"
"github.com/cli/cli/context"
"github.com/cli/cli/git"
"github.com/cli/cli/pkg/httpmock"
Expand Down Expand Up @@ -409,9 +408,9 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {

func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
initBlankContext("", "OWNER/REPO", "feature")
http := initMockHTTP()
http := initFakeHTTP()
defer http.Verify(t)
http.Register(httpmock.GraphQL(`\bviewerPermission\b`), httpmock.StringResponse(api.RepoNetworkStubResponse("OWNER", "REPO", "WRITE")))
http.Register(httpmock.GraphQL(`\bviewerPermission\b`), httpmock.StringResponse(httpmock.RepoNetworkStubResponse("OWNER", "REPO", "master", "WRITE")))
http.Register(httpmock.GraphQL(`\bforks\(`), httpmock.StringResponse(`
{ "data": { "repository": { "forks": { "nodes": [
] } } } }
Expand Down
10 changes: 1 addition & 9 deletions command/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,7 @@ func initBlankContext(cfg, repo, branch string) {
}
}

func initFakeHTTP() *api.FakeHTTP {
http := &api.FakeHTTP{}
apiClientForContext = func(context.Context) (*api.Client, error) {
return api.NewClient(api.ReplaceTripper(http)), nil
}
return http
}

func initMockHTTP() *httpmock.Registry {
func initFakeHTTP() *httpmock.Registry {
http := &httpmock.Registry{}
apiClientForContext = func(context.Context) (*api.Client, error) {
return api.NewClient(api.ReplaceTripper(http)), nil
Expand Down
5 changes: 3 additions & 2 deletions context/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cli/cli/api"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/httpmock"
)

func eq(t *testing.T, got interface{}, expected interface{}) {
Expand Down Expand Up @@ -70,7 +71,7 @@ func Test_translateRemotes(t *testing.T) {
}

func Test_resolvedRemotes_triangularSetup(t *testing.T) {
http := &api.FakeHTTP{}
http := &httpmock.Registry{}
apiClient := api.NewClient(api.ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`
Expand Down Expand Up @@ -137,7 +138,7 @@ func Test_resolvedRemotes_triangularSetup(t *testing.T) {
}

func Test_resolvedRemotes_forkLookup(t *testing.T) {
http := &api.FakeHTTP{}
http := &httpmock.Registry{}
apiClient := api.NewClient(api.ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`
Expand Down
89 changes: 89 additions & 0 deletions pkg/httpmock/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package httpmock

import (
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
)

// TODO: clean up methods in this file when there are no more callers

func (r *Registry) StubResponse(status int, body io.Reader) {
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
return httpResponse(status, body), nil
})
}

func (r *Registry) StubWithFixture(status int, fixtureFileName string) func() {
fixturePath := path.Join("../test/fixtures/", fixtureFileName)
fixtureFile, err := os.Open(fixturePath)
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
return httpResponse(200, fixtureFile), nil
})
return func() {
if err == nil {
fixtureFile.Close()
}
}
}

func (r *Registry) StubRepoResponse(owner, repo string) {
r.StubRepoResponseWithPermission(owner, repo, "WRITE")
}

func (r *Registry) StubRepoResponseWithPermission(owner, repo, permission string) {
r.Register(MatchAny, StringResponse(RepoNetworkStubResponse(owner, repo, "master", permission)))
}

func (r *Registry) StubRepoResponseWithDefaultBranch(owner, repo, defaultBranch string) {
r.Register(MatchAny, StringResponse(RepoNetworkStubResponse(owner, repo, defaultBranch, "WRITE")))
}

func (r *Registry) StubForkedRepoResponse(ownRepo, parentRepo string) {
r.Register(MatchAny, StringResponse(RepoNetworkStubForkResponse(ownRepo, parentRepo)))
}

func RepoNetworkStubResponse(owner, repo, defaultBranch, permission string) string {
return fmt.Sprintf(`
{ "data": { "repo_000": {
"id": "REPOID",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "%s"
},
"viewerPermission": "%s"
} } }
`, repo, owner, defaultBranch, permission)
}

func RepoNetworkStubForkResponse(forkFullName, parentFullName string) string {
forkRepo := strings.SplitN(forkFullName, "/", 2)
parentRepo := strings.SplitN(parentFullName, "/", 2)
return fmt.Sprintf(`
{ "data": { "repo_000": {
"id": "REPOID2",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "master"
},
"viewerPermission": "ADMIN",
"parent": {
"id": "REPOID1",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "master"
},
"viewerPermission": "READ"
}
} } }
`, forkRepo[1], forkRepo[0], parentRepo[1], parentRepo[0])
}
Loading

0 comments on commit 0a4d4ee

Please sign in to comment.