-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
23 changed files
with
12,442 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.