fix(webhook/client): error on http status codes >= 400

This commit is contained in:
Dorian de Koning
2022-11-25 09:49:25 +01:00
committed by Julien Neuhart
parent 1f784ad44c
commit 1de0f4c4fa
2 changed files with 34 additions and 2 deletions
+5
View File
@@ -3,6 +3,7 @@ package webhook
import (
"fmt"
"io"
"net/http"
"strconv"
"time"
@@ -78,6 +79,10 @@ func (c client) send(body io.Reader, headers map[string]string, erroed bool) err
return fmt.Errorf("send '%s' request to '%s': %w", method, URL, err)
}
if resp.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("send '%s' request to '%s': got status: '%s'", method, URL, resp.Status)
}
defer func() {
err := resp.Body.Close()
if err != nil {
+29 -2
View File
@@ -339,6 +339,7 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) {
expectWebhookFilename string
expectWebhookErrorStatus int
expectWebhookErrorMessage string
returnedError *echo.HTTPError
}{
{
request: buildMultipartFormDataRequest(),
@@ -363,8 +364,8 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) {
}(),
expectWebhookContentType: echo.MIMEApplicationJSONCharsetUTF8,
expectWebhookMethod: http.MethodPost,
expectWebhookErrorStatus: http.StatusInternalServerError,
expectWebhookErrorMessage: http.StatusText(http.StatusInternalServerError),
expectWebhookErrorStatus: http.StatusBadRequest,
expectWebhookErrorMessage: http.StatusText(http.StatusBadRequest),
},
{
request: func() *http.Request {
@@ -387,6 +388,27 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) {
expectWebhookFilename: "foo",
expectWebhookExtraHTTPHeaders: map[string]string{"foo": "bar"},
},
{
request: func() *http.Request {
req := buildMultipartFormDataRequest()
req.Header.Set("Gotenberg-Output-Filename", "foo")
return req
}(),
mod: buildWebhookModule(),
next: func() echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Get("context").(*api.Context)
return ctx.AddOutputPaths("/tests/test/testdata/api/sample3.pdf")
}
}(),
returnedError: echo.ErrInternalServerError,
// Even though an error is returned the expected response is still a pdf, since the webhook error is only logged
expectWebhookContentType: "application/pdf",
expectWebhookMethod: http.MethodPost,
expectWebhookFilename: "foo",
},
} {
func() {
srv := echo.New()
@@ -500,6 +522,11 @@ func TestWebhookMiddlewareAsynchronousProcess(t *testing.T) {
}
errChan <- nil
if tc.returnedError != nil {
return tc.returnedError
}
return nil
}
}(),