Skip to content

Commit

Permalink
Use gock instead of httpmock
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed May 9, 2022
1 parent 0ad179f commit 6b5956f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions gh_test.go
Expand Up @@ -106,6 +106,7 @@ func TestGQLClient(t *testing.T) {
}

func TestGQLClientError(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
Expand All @@ -116,19 +117,20 @@ func TestGQLClientError(t *testing.T) {
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.GQL("QUERY"),
httpmock.StringResponse(`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}`),
)
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Authorization", "token GH_TOKEN").
BodyString(`{"query":"QUERY","variables":null}`).
Reply(200).
JSON(`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}`)

client, err := GQLClient(&api.ClientOptions{Transport: http})
client, err := GQLClient(nil)
assert.NoError(t, err)

vars := map[string]interface{}{}
res := struct{ Organization struct{ Name string } }{}
err = client.Do("QUERY", vars, &res)
err = client.Do("QUERY", nil, &res)
assert.EqualError(t, err, "GQL error: Could not resolve to an Organization with the login of 'cli'.")
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
}

func TestHTTPClient(t *testing.T) {
Expand Down

0 comments on commit 6b5956f

Please sign in to comment.