func sendRequest(opts *args.Options, req *http.Request, payload *[]byte) (string, error) { req.Header.Set("Content-Type", "application/json") // Print a curl representation of the request if verbose curl := args.CurlString(req, payload) if opts.Bool("verbose") { fmt.Printf("-- %s\n", curl) } client := &http.Client{} resp, err := client.Do(req) if err != nil { return "", errors.Wrap(err, "Client Error") } defer resp.Body.Close() // Read in the entire response body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", errors.Wrap(err, "ReadAll Error") } return string(body), nil }
package args_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/thrawn01/args" ) var _ = Describe("Options", func() { var opts *args.Options var log *TestLogger BeforeEach(func() { log = NewTestLogger() parser := args.NewParser() parser.SetLog(log) opts = parser.NewOptionsFromMap( map[string]interface{}{ "int": 1, "bool": true, "string": "one", "endpoints": map[string]interface{}{ "endpoint1": "host1", "endpoint2": "host2", "endpoint3": "host3", }, "deeply": map[string]interface{}{ "nested": map[string]interface{}{ "thing": "foo", }, "foo": "bar",
. "github.com/onsi/gomega" "github.com/thrawn01/args" "os" "testing" ) func TestArgs(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Args Parser") } var _ = Describe("ArgParser", func() { Describe("Options.Convert()", func() { It("Should convert options to integers", func() { opts := args.Options{"one": args.OptResult{Value: 1, Seen: false}} var result int opts.Convert("one", "int", func(value interface{}) { result = value.(int) }) Expect(result).To(Equal(1)) }) It("Should raise panic if unable to cast an option", func() { opts := args.Options{"one": args.OptResult{Value: "", Seen: false}} panicCaught := false result := 0 defer func() { msg := recover() Expect(msg).ToNot(BeNil())