From f4ce196a346735df13ee9ebf28b854ba82747edc Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sat, 23 Mar 2024 16:34:42 +0100 Subject: [PATCH] fix(pdfengines): return a 400 Bad Request if no metadata to write --- pkg/modules/pdfengines/routes.go | 3 +++ pkg/modules/pdfengines/routes_test.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/modules/pdfengines/routes.go b/pkg/modules/pdfengines/routes.go index 100dfe78..7ee4a278 100644 --- a/pkg/modules/pdfengines/routes.go +++ b/pkg/modules/pdfengines/routes.go @@ -238,6 +238,9 @@ func writeMetadataRoute(engine gotenberg.PdfEngine) api.Route { return fmt.Errorf("unmarshal metadata: %w", err) } } + if len(metadata) == 0 { + return errors.New("no metadata") + } return nil }). Validate() diff --git a/pkg/modules/pdfengines/routes_test.go b/pkg/modules/pdfengines/routes_test.go index fc16cde1..a3cdd940 100644 --- a/pkg/modules/pdfengines/routes_test.go +++ b/pkg/modules/pdfengines/routes_test.go @@ -591,6 +591,25 @@ func TestWriteMetadataHandler(t *testing.T) { expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, + { + scenario: "no metadata", + ctx: func() *api.ContextMock { + ctx := &api.ContextMock{Context: new(api.Context)} + ctx.SetFiles(map[string]string{ + "document.docx": "/document.docx", + }) + ctx.SetValues(map[string][]string{ + "metadata": { + "{}", + }, + }) + return ctx + }(), + expectError: true, + expectHttpError: true, + expectHttpStatus: http.StatusBadRequest, + expectOutputPathsCount: 0, + }, { scenario: "error from PDF engine", ctx: func() *api.ContextMock {