Skip to content

Commit

Permalink
Add delete body function
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed May 4, 2024
1 parent e7e0c03 commit 7adfcfa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.6 (unreleased)

- Add DeleteBody() function

## 0.1.5

- Handle 429 responses including Retry-After header
Expand Down
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ func (client *Client) Delete(path string, mods ...func(*Req)) (Res, error) {
return client.Do(req)
}

// DeleteBody makes a DELETE request with a payload.
// Hint: Use the Body struct to easily create DELETE body data.
func (client *Client) DeleteBody(path, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("DELETE", "/dataservice"+path, strings.NewReader(data), mods...)
err := client.Authenticate()
if err != nil {
return Res{}, err
}
return client.Do(req)
}

// Post makes a POST request and returns a GJSON result.
// Hint: Use the Body struct to easily create POST body data.
func (client *Client) Post(path, data string, mods ...func(*Req)) (Res, error) {
Expand Down
30 changes: 25 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sdwan

import (
"errors"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -87,14 +87,14 @@ func TestClientGet(t *testing.T) {
Get("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Get("/url")
assert.Error(t, err)
}

// TestClientDeleteDn tests the Client::Delete method.
// TestClientDelete tests the Client::Delete method.
func TestClientDelete(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()
Expand All @@ -114,6 +114,26 @@ func TestClientDelete(t *testing.T) {
assert.Error(t, err)
}

// TestClientDeleteBody tests the Client::Delete method.
func TestClientDeleteBody(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()

// Success
gock.New(testURL).
Delete("/url").
Reply(200)
_, err := client.DeleteBody("/url", "")
assert.NoError(t, err)

// HTTP error
gock.New(testURL).
Delete("/url").
ReplyError(errors.New("fail"))
_, err = client.DeleteBody("/url", "")
assert.Error(t, err)
}

// TestClientPost tests the Client::Post method.
func TestClientPost(t *testing.T) {
defer gock.Off()
Expand Down Expand Up @@ -141,7 +161,7 @@ func TestClientPost(t *testing.T) {
Post("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Post("/url", "{}")
Expand Down Expand Up @@ -175,7 +195,7 @@ func TestClientPut(t *testing.T) {
Put("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Put("/url", "{}")
Expand Down

0 comments on commit 7adfcfa

Please sign in to comment.