Skip to content

Commit

Permalink
Add test for query command
Browse files Browse the repository at this point in the history
Make a request by piping a query to STDIN and check that the response is
rendered as JSON. I used tab indentation in the query and response
because my editor wanted to and there was a risk of mixing spaces/tabs
in future changes. The example GraphQL query and response are stolen
from here:

- https://graphql.org/learn/queries/
  • Loading branch information
dcarley authored and Zachary Scott committed Jun 15, 2018
1 parent 8f5c6a1 commit 6322995
Show file tree
Hide file tree
Showing 23 changed files with 12,442 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions cmd/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cmd_test

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
)

var _ = Describe("Query", func() {
var (
server *ghttp.Server
token string
tempHome string
stdin bytes.Buffer
command *exec.Cmd
)

BeforeEach(func() {
server = ghttp.NewServer()

var err error
tempHome, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())

token = "mytoken"
command = exec.Command(pathCLI, "query",
"-t", token,
"-e", server.URL(),
)
command.Stdin = &stdin
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
fmt.Sprintf("USERPROFILE=%s", tempHome), // windows
)
})

AfterEach(func() {
server.Close()
Expect(os.RemoveAll(tempHome)).To(Succeed())
})

Describe("query provided to STDIN", func() {
var responseData string

BeforeEach(func() {
query := `query {
hero {
name
friends {
name
}
}
}
`
responseData = `{
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
`
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/"),
ghttp.VerifyHeader(http.Header{
"Authorization": []string{token},
}),
ghttp.RespondWith(http.StatusOK, `{"data": `+responseData+`}`),
),
)

_, err := stdin.WriteString(query)
Expect(err).ToNot(HaveOccurred())
})

It("should make request and return result as JSON", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
session.Wait()
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchJSON(responseData))
Eventually(session).Should(gexec.Exit(0))
})
})
})
3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/golang/protobuf/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6322995

Please sign in to comment.