Update gist create test

- use testify
- verify that the HTTP request is a POST to `/gists`
- more precise POST params matching in assertion
This commit is contained in:
Mislav Marohnić 2020-07-17 17:10:47 +02:00
parent 449fccf811
commit d90c387e04

View file

@ -1,12 +1,12 @@
package command
import (
"bytes"
"encoding/json"
"io/ioutil"
"strings"
"testing"
"github.com/cli/cli/pkg/httpmock"
"github.com/stretchr/testify/assert"
)
@ -14,33 +14,34 @@ func TestGistCreate(t *testing.T) {
initBlankContext("", "OWNER/REPO", "trunk")
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
http.Register(httpmock.REST("POST", "gists"), httpmock.StringResponse(`
{
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d"
}
`))
output, err := RunCommand(`gist create "../test/fixtures/gistCreate.json" -d "Gist description" --public`)
eq(t, err, nil)
assert.NoError(t, err)
bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
reqBody := struct {
Description string
Public bool
Files map[string]struct {
Content string
}
}{}
reqBody := make(map[string]interface{})
err = json.Unmarshal(bodyBytes, &reqBody)
if err != nil {
t.Fatalf("unexpected json error: %s", err)
t.Fatalf("error decoding JSON: %v", err)
}
eq(t, reqBody.Description, "Gist description")
eq(t, reqBody.Public, true)
eq(t, reqBody.Files["gistCreate.json"].Content, "{}")
eq(t, output.String(), "https://gist.github.com/aa5a315d61ae9438b18d\n")
expectParams := map[string]interface{}{
"description": "Gist description",
"files": map[string]interface{}{
"gistCreate.json": map[string]interface{}{
"content": "{}",
},
},
"public": true,
}
assert.Equal(t, expectParams, reqBody)
assert.Equal(t, "https://gist.github.com/aa5a315d61ae9438b18d\n", output.String())
}
func TestGistCreate_stdin(t *testing.T) {