Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Decoupling functionality to make unit testing easier in the future #13

Merged
merged 8 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,37 @@ Install by `brew install athul/tap/pwcli`
# Usages

Putting Simply: **Just pass the URL to the request method**
## Basic
- GET : `pwcli get <url> `
- POST: `pwcli post <url> `
- PATCH: `pwcli patch <url>`
- PUT : `pwcli put <url>`
- DELETE: `pwcli delete <url>`

- GET : `pwcli get <url> -t/--token <token> -u <username for basic auth> -p <password for basic auth>`
- POST: `pwcli post <url> < -t/-u/-p > -c/--content type <content type> -b/--body <body>`
- PATCH: `pwcli patch <url> < -t/-u/-p > -c/--content type <content type> -b/--body <body>`
- PUT : `pwcli put <url> < -t/-u/-p > -c/--content type <content type> -b/--body <body>`
- DELETE: `pwcli delete <url> < -t/-u/-p > -c/--content type <content type> -b/--body <body>`
Example for a POST request:
`pwcli post https://reqres.in/api/users/2 -c js -b '{"name": "morp","job": "zion resident"}`

**Content Types can be of**
`html` : `text/html`
`js` : `application/json`,
`xml` : `application/xml`
`plain` : `text/plain`,

#### Extra
### Extra

**SEND**: This can be used to test multiple endpoints from the `postwoman-collection.json` file. The output will only be the `statuscode`.
RUN: `pwcli send <PATH to postwoman collection.json>`
OUTPUT:
Example : `pwcli send <PATH to postwoman collection.json>`
o/p:
![](/assets/send.png)


### There are 3 Authentication Flags
*(optional)*
- `-t` or `--token` for a Bearer Token for Authentication
- `-u` for the `Username` in Basic Auth
- `-p` for the `password` in Basic Auth
### There are 2 flags especially for the data management requests like POST,PUT,PATCH and DELETE
- `-c` or `--ctype` for the *Content Type*

- `-b` or `--body` for the Data Body, this can be of json, html or plain text based on the request.
> Enclose the body in Single Quotes(\')

**Content Types can be of**
`html` : `text/html`
`js` : `application/json`
`xml` : `application/xml`
`plain` : `text/plain`
28 changes: 22 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func main() {
app.Usage = color.HiYellowString("Test API endpoints without the hassle")
app.Description = color.HiBlueString("Made with <3 by Postwoman Team")

var out string

getFlags := []cli.Flag{
cli.StringFlag{
Name: "token, t",
Expand Down Expand Up @@ -62,46 +64,58 @@ func main() {
Usage: "Send a GET request",
Flags: getFlags,
Action: func(c *cli.Context) error {
return mets.Getbasic(c)
var err error
out, err = mets.Getbasic(c)
return err
},
},
{
Name: "post",
Usage: "Send a POST Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
return mets.Postbasic(c)
var err error
out, err = mets.BasicRequestWithBody(c, "POST")
return err
},
},
{
Name: "put",
Usage: "Send a PUT Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
return mets.Putbasic(c)
var err error
out, err = mets.BasicRequestWithBody(c, "PUT")
return err
},
},
{
Name: "patch",
Usage: "Send a PATCH Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
return mets.Patchbasic(c)
var err error
out, err = mets.BasicRequestWithBody(c, "PATCH")
return err
},
},
{
Name: "delete",
Usage: "Send a DELETE Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
return mets.Deletebasic(c)
var err error
out, err = mets.BasicRequestWithBody(c, "DELETE")
return err
},
},
{
Name: "send",
Usage: "Test all the Endpoints in the Postwoman Collection.json",
Action: func(c *cli.Context) error {
return mets.ReadCollection(c)
var err error
out, err = mets.ReadCollection(c)
return err
},
},
}
Expand All @@ -117,5 +131,7 @@ func main() {
if err != nil {
l := log.New(os.Stderr, "", 0)
l.Println(color.HiRedString("\n%s", err.Error()))
os.Exit(1)
}
fmt.Println(out)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ require (
github.com/tidwall/pretty v1.0.1
github.com/urfave/cli v1.22.2
github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf
go.uber.org/multierr v1.4.0
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
)
38 changes: 38 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/pretty v1.0.1 h1:WE4RBSZ1x6McVVC8S/Md+Qse8YUv6HRObAx6ke00NY8=
Expand All @@ -25,15 +37,41 @@ github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf h1:VA200mPTYh9FWY8zKX5ctXCtNk78HUez8ecTdsQGhoo=
github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.4.0 h1:f3WCSC2KzAcBXGATIxAB1E2XuCpNU255wNKZ505qi3E=
go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
27 changes: 13 additions & 14 deletions methods/put.go → methods/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import (
"github.com/urfave/cli"
)

//Putbasic sends a basic PUT request
func Putbasic(c *cli.Context) error {
// BasicRequestWithBody sends put|patch|post|delete requests
func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
url, err := checkURL(c.Args().Get(0))
if err != nil {
return err
return "", err
}

var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr))
if err != nil {
return "", fmt.Errorf("Error creating request: %s", err.Error())
}

req.Header.Set("Content-Type", Contenttypes[c.String("ctype")])
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
Expand All @@ -27,18 +31,13 @@ func Putbasic(c *cli.Context) error {
pw := c.String("p")
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}

client := getHTTPClient()
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error sending request: %s", err.Error())
return "", fmt.Errorf("Error sending request: %s", err.Error())
}
defer resp.Body.Close()

s, err := formatresp(resp)
if err != nil {
return err
}

fmt.Println(s)
return nil
return formatresp(resp)
}
13 changes: 13 additions & 0 deletions methods/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package methods

import (
"net/http"
"time"
)

// The HTTP client can be modified via params and cli flags later
func getHTTPClient() *http.Client {
return &http.Client{
Timeout: 10 * time.Second,
}
}
19 changes: 19 additions & 0 deletions methods/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package methods

import (
"testing"
"time"
)

func Test_getHTTPClient(t *testing.T) {
t.Run("default http client", func(t *testing.T) {
client := getHTTPClient()
if client == nil {
t.Error("got nil http client")
}
dur := 10 * time.Second
if client.Timeout != dur {
t.Errorf("timeout doesn't match what's expected: %v", client.Timeout)
}
})
}
44 changes: 0 additions & 44 deletions methods/delete.go

This file was deleted.

3 changes: 3 additions & 0 deletions methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func formatresp(resp *http.Response) (string, error) {
if err != nil {
return "", fmt.Errorf("Error reading response body: %s", err.Error())
}

for key, value := range resp.Header {
c.Print(key, " : ")
magenta.Print(value, "\n")
}

str := string(body)
if strings.Contains(heads, "json") {
retbody = color.HiYellowString("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(pretty.Color(pretty.Pretty(body), nil)))
Expand All @@ -39,6 +41,7 @@ func formatresp(resp *http.Response) (string, error) {
}
retbody = color.HiYellowString("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", s)
}

return retbody, nil
}

Expand Down
Loading