Skip to content

Commit

Permalink
feat: Check not retryable status codes
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal committed Dec 19, 2024
1 parent a35d5f4 commit 7b03888
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package webhook
import (
"bytes"
"context"
"fmt"
"net/http"
"os"

"github.com/hashicorp/go-retryablehttp"
Expand All @@ -26,6 +28,8 @@ import (

var client = &retryablehttp.Client{}

var errHTTPNotOK = errors.New("http result not OK")

func SetHTTPClient(c *retryablehttp.Client) {
client = c
}
Expand Down Expand Up @@ -80,5 +84,11 @@ func SendWebHook(ctx context.Context, obj *template.MessageType) error {
}
defer resp.Body.Close()

log.Infof("response status: %s", resp.Status)

if resp.StatusCode != http.StatusOK {
return errors.Wrap(errHTTPNotOK, fmt.Sprintf("StatusCode=%d", resp.StatusCode))
}

return nil
}
15 changes: 15 additions & 0 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import (
var retryableRequstCount = 0

var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/-/400" {
w.WriteHeader(http.StatusBadRequest)

return
}

if r.RequestURI == "/test-retryable" {
retryableRequstCount++

Expand Down Expand Up @@ -121,6 +127,15 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel
},
HTTPClient: retryClientDefault,
},
{
Name: "TestRetryableCustomStatusCodes",
Args: map[string]string{
"webhook.url": ts.URL + "/-/400",
},
HTTPClient: retryClientDefault,
Error: true,
ErrorMessage: "http result not OK",
},
{
Name: "ValidHookAndTemplate",
Args: map[string]string{
Expand Down

0 comments on commit 7b03888

Please sign in to comment.