Skip to content

Commit

Permalink
Add some basic tests for exported client functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jan 26, 2022
1 parent 8a70c8a commit bf38ed4
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"github.com/cli/go-gh/internal/httpmock"
"github.com/cli/go-gh/pkg/api"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -42,3 +44,87 @@ func TestRunError(t *testing.T) {
assert.Equal(t, "", stdOut.String())
assert.Equal(t, "process exited with error", stdErr.String())
}

func TestRESTClient(t *testing.T) {
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.REST("GET", "some/test/path"),
httpmock.StatusStringResponse(200, `{"message": "success"}`),
)

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

res := struct{ Message string }{}
err = client.Do("GET", "some/test/path", nil, &res)
assert.NoError(t, err)
assert.Equal(t, "success", res.Message)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestGQLClient(t *testing.T) {
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.GQL("QUERY"),
httpmock.StringResponse(`{"data":{"viewer":{"login":"hubot"}}}`),
)

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

vars := map[string]interface{}{"var": "test"}
res := struct{ Viewer struct{ Login string } }{}
err = client.Do("QUERY", vars, &res)
assert.NoError(t, err)
assert.Equal(t, "hubot", res.Viewer.Login)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestHTTPClient(t *testing.T) {
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.REST("GET", "some/test/path"),
httpmock.StatusStringResponse(200, `{"message": "success"}`),
)

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

res, err := client.Get("https://api.github.com/some/test/path")
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

0 comments on commit bf38ed4

Please sign in to comment.