From 6ae8ecd28454ef9b1b9eed7712818e8583c0c338 Mon Sep 17 00:00:00 2001 From: Leslie Lauw Date: Mon, 29 Apr 2024 10:50:20 +0200 Subject: [PATCH 1/2] fix delete error response --- ewhs/webhooks.go | 4 --- ewhs/webhooks_test.go | 76 +++++++++++++++++++++++++++++++++++++++ test/testdata/webhooks.go | 1 + 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 ewhs/webhooks_test.go create mode 100644 test/testdata/webhooks.go diff --git a/ewhs/webhooks.go b/ewhs/webhooks.go index 09912e8..a5af942 100644 --- a/ewhs/webhooks.go +++ b/ewhs/webhooks.go @@ -85,9 +85,5 @@ func (os *WebhooksService) Delete(ctx context.Context, webhookID string) (webhoo return } - if err = json.Unmarshal(res.content, &webhook); err != nil { - return - } - return } diff --git a/ewhs/webhooks_test.go b/ewhs/webhooks_test.go new file mode 100644 index 0000000..3eb473c --- /dev/null +++ b/ewhs/webhooks_test.go @@ -0,0 +1,76 @@ +package ewhs + +import ( + "context" + "github.com/ewarehousing-solutions/ewhs-api-go/test/testdata" + "github.com/stretchr/testify/suite" + "net/http" + "testing" +) + +type webhooksServiceSuite struct{ suite.Suite } + +func (os *webhooksServiceSuite) TestWebhooksSuite_Delete() { + type args struct { + ctx context.Context + webhook string + } + cases := []struct { + name string + args args + wantErr bool + err error + pre func() + handler http.HandlerFunc + }{ + { + "delete webhooks returns 204 as expected.", + args{ + context.Background(), + "c9165f93-8301-4aaa-9f64-27f191c0c778", + }, + false, + nil, + func() { + tClient.WithAuthToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9") + }, + func(w http.ResponseWriter, r *http.Request) { + testHeader(os.T(), r, AuthHeader, "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9") + testHeader(os.T(), r, CustomerCodeHeader, "test_customer") + testHeader(os.T(), r, WmsCodeHeader, "test_wms") + testMethod(os.T(), r, "DELETE") + + if _, ok := r.Header[AuthHeader]; !ok { + w.WriteHeader(http.StatusUnauthorized) + } else { + w.WriteHeader(http.StatusNoContent) + } + _, _ = w.Write([]byte(testdata.DeleteWebhooksResponse)) + }, + }, + } + + for _, c := range cases { + setup() + defer teardown() + + os.T().Run(c.name, func(t *testing.T) { + c.pre() + tMux.HandleFunc("/webhooks/", c.handler) + + m, res, err := tClient.Webhooks.Delete(c.args.ctx, c.args.webhook) + if c.wantErr { + os.NotNil(err) + os.EqualError(err, c.err.Error()) + } else { + os.Nil(err) + os.IsType(&Webhook{}, m) + os.IsType(&http.Response{}, res.Response) + } + }) + } +} + +func TestWebhooksSuite(t *testing.T) { + suite.Run(t, new(webhooksServiceSuite)) +} diff --git a/test/testdata/webhooks.go b/test/testdata/webhooks.go new file mode 100644 index 0000000..69d29d3 --- /dev/null +++ b/test/testdata/webhooks.go @@ -0,0 +1 @@ +package testdata From bb23219c6e375879adc7b83eaa5fd92aabac2d3a Mon Sep 17 00:00:00 2001 From: Leslie Lauw Date: Mon, 29 Apr 2024 10:51:37 +0200 Subject: [PATCH 2/2] add response --- test/testdata/webhooks.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/testdata/webhooks.go b/test/testdata/webhooks.go index 69d29d3..541cc7e 100644 --- a/test/testdata/webhooks.go +++ b/test/testdata/webhooks.go @@ -1 +1,5 @@ package testdata + +// DeleteWebhooksResponse is a test data for webhooks response +// It returns 204 no content +const DeleteWebhooksResponse = ``