From 96032f4d7d7344b85430e06654e4397927da0c84 Mon Sep 17 00:00:00 2001 From: Antoine Gelloz Date: Thu, 1 Dec 2022 16:04:38 +0100 Subject: [PATCH] chore: add test header for /test --- pkg/attempt.go | 3 ++- pkg/server/test.go | 2 +- pkg/worker/messages/worker.go | 2 +- pkg/worker/retries/worker.go | 2 +- test/server_test.go | 1 + 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/attempt.go b/pkg/attempt.go index aa83a9f..5c5aa78 100644 --- a/pkg/attempt.go +++ b/pkg/attempt.go @@ -34,7 +34,7 @@ type Attempt struct { NextRetryAfter time.Time `json:"nextRetryAfter,omitempty" bson:"nextRetryAfter,omitempty"` } -func MakeAttempt(ctx context.Context, httpClient *http.Client, schedule []time.Duration, id string, attemptNb int, cfg Config, payload []byte) (Attempt, error) { +func MakeAttempt(ctx context.Context, httpClient *http.Client, schedule []time.Duration, id string, attemptNb int, cfg Config, payload []byte, isTest bool) (Attempt, error) { req, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.Endpoint, bytes.NewBuffer(payload)) if err != nil { return Attempt{}, errors.Wrap(err, "http.NewRequestWithContext") @@ -52,6 +52,7 @@ func MakeAttempt(ctx context.Context, httpClient *http.Client, schedule []time.D req.Header.Set("formance-webhook-id", id) req.Header.Set("formance-webhook-timestamp", fmt.Sprintf("%d", timestamp)) req.Header.Set("formance-webhook-signature", signature) + req.Header.Set("formance-webhook-test", fmt.Sprintf("%v", isTest)) resp, err := httpClient.Do(req) if err != nil { diff --git a/pkg/server/test.go b/pkg/server/test.go index ef62270..6de1459 100644 --- a/pkg/server/test.go +++ b/pkg/server/test.go @@ -23,7 +23,7 @@ func (h *serverHandler) testOneConfigHandle(w http.ResponseWriter, r *http.Reque } sharedlogging.GetLogger(r.Context()).Infof("GET %s/%s%s", PathConfigs, id, PathTest) attempt, err := webhooks.MakeAttempt(r.Context(), h.httpClient, nil, - uuid.NewString(), 0, cfgs[0], []byte(`{"data":"test"}`)) + uuid.NewString(), 0, cfgs[0], []byte(`{"data":"test"}`), true) if err != nil { sharedlogging.GetLogger(r.Context()).Errorf("GET %s/%s%s: %s", PathConfigs, id, PathTest, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) diff --git a/pkg/worker/messages/worker.go b/pkg/worker/messages/worker.go index 917e7e3..223319e 100644 --- a/pkg/worker/messages/worker.go +++ b/pkg/worker/messages/worker.go @@ -163,7 +163,7 @@ func (w *WorkerMessages) processMessage(ctx context.Context, msgValue []byte) er } attempt, err := webhooks.MakeAttempt(ctx, w.httpClient, w.retriesSchedule, - uuid.NewString(), 0, cfg, data) + uuid.NewString(), 0, cfg, data, false) if err != nil { return errors.Wrap(err, "sending webhook") } diff --git a/pkg/worker/retries/worker.go b/pkg/worker/retries/worker.go index 5392724..b2c3da8 100644 --- a/pkg/worker/retries/worker.go +++ b/pkg/worker/retries/worker.go @@ -106,7 +106,7 @@ func (w *WorkerRetries) attemptRetries(ctx context.Context, errChan chan error) newAttemptNb := atts[0].RetryAttempt + 1 attempt, err := webhooks.MakeAttempt(ctx, w.httpClient, w.retriesSchedule, - id, newAttemptNb, atts[0].Config, []byte(atts[0].Payload)) + id, newAttemptNb, atts[0].Config, []byte(atts[0].Payload), false) if err != nil { errChan <- errors.Wrap(err, "webhooks.MakeAttempt") continue diff --git a/test/server_test.go b/test/server_test.go index 3320d8b..56b06b5 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -210,6 +210,7 @@ func TestServer(t *testing.T) { attempt, ok := decodeSingleResponse[webhooks.Attempt](t, resBody) assert.Equal(t, true, ok) assert.Equal(t, webhooks.StatusAttemptSuccess, attempt.Status) + assert.Equal(t, `{"data":"test"}`, attempt.Payload) requestServer(t, http.MethodDelete, server.PathConfigs+"/"+c.ID, http.StatusOK) })