package pdfengines import ( "context" "errors" "net/http" "net/http/httptest" "slices" "strings" "testing" "github.com/labstack/echo/v4" "go.uber.org/zap" "github.com/gotenberg/gotenberg/v8/pkg/gotenberg" "github.com/gotenberg/gotenberg/v8/pkg/modules/api" ) func TestMergeHandler(t *testing.T) { for _, tc := range []struct { scenario string ctx *api.ContextMock engine gotenberg.PdfEngine expectError bool expectHttpError bool expectHttpStatus int expectOutputPathsCount int }{ { scenario: "missing at least one mandatory file", ctx: &api.ContextMock{Context: new(api.Context)}, expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "invalid metadata form field", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "metadata": { "foo", }, }) return ctx }(), expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "PDF engine merge error", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) return ctx }(), engine: &gotenberg.PdfEngineMock{ MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { return errors.New("foo") }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "PDF engine convert error", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { return nil }, ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return errors.New("foo") }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "PDF engine write metadata error", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { return nil }, WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return errors.New("foo") }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "cannot add output paths", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetCancelled(true) return ctx }(), engine: &gotenberg.PdfEngineMock{ MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { return nil }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "success", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { return nil }, ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return nil }, WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return nil }, }, expectError: false, expectHttpError: false, expectOutputPathsCount: 1, }, } { t.Run(tc.scenario, func(t *testing.T) { tc.ctx.SetLogger(zap.NewNop()) c := echo.New().NewContext(nil, nil) c.Set("context", tc.ctx.Context) err := mergeRoute(tc.engine).Handler(c) if tc.expectError && err == nil { t.Fatal("expected error but got none", err) } if !tc.expectError && err != nil { t.Fatalf("expected no error but got: %v", err) } var httpErr api.HttpError isHttpError := errors.As(err, &httpErr) if tc.expectHttpError && !isHttpError { t.Errorf("expected an HTTP error but got: %v", err) } if !tc.expectHttpError && isHttpError { t.Errorf("expected no HTTP error but got one: %v", httpErr) } if err != nil && tc.expectHttpError && isHttpError { status, _ := httpErr.HttpError() if status != tc.expectHttpStatus { t.Errorf("expected %d as HTTP status code but got %d", tc.expectHttpStatus, status) } } if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) { t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths())) } }) } } func TestConvertHandler(t *testing.T) { for _, tc := range []struct { scenario string ctx *api.ContextMock engine gotenberg.PdfEngine expectError bool expectHttpError bool expectHttpStatus int expectOutputPathsCount int expectOutputPaths []string }{ { scenario: "missing at least one mandatory file", ctx: &api.ContextMock{Context: new(api.Context)}, expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "no PDF formats", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) return ctx }(), expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "error from PDF engine", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return errors.New("foo") }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "cannot add output paths", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, }) ctx.SetCancelled(true) return ctx }(), engine: &gotenberg.PdfEngineMock{ ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return nil }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "success with PDF/A & PDF/UA form fields (single file)", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, "pdfua": { "true", }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return nil }, }, expectError: false, expectHttpError: false, expectOutputPathsCount: 1, }, { scenario: "cannot rename many files", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, "pdfua": { "true", }, }) ctx.SetPathRename(&gotenberg.PathRenameMock{RenameMock: func(oldpath, newpath string) error { return errors.New("cannot rename") }}) return ctx }(), engine: &gotenberg.PdfEngineMock{ ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return nil }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "success with PDF/A & PDF/UA form fields (many files)", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", "file2.pdf": "/file2.pdf", }) ctx.SetValues(map[string][]string{ "pdfa": { gotenberg.PdfA1b, }, "pdfua": { "true", }, }) ctx.SetPathRename(&gotenberg.PathRenameMock{RenameMock: func(oldpath, newpath string) error { return nil }}) return ctx }(), engine: &gotenberg.PdfEngineMock{ ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { return nil }, }, expectError: false, expectHttpError: false, expectOutputPathsCount: 2, expectOutputPaths: []string{"/file.pdf", "/file2.pdf"}, }, } { t.Run(tc.scenario, func(t *testing.T) { tc.ctx.SetLogger(zap.NewNop()) c := echo.New().NewContext(nil, nil) c.Set("context", tc.ctx.Context) err := convertRoute(tc.engine).Handler(c) if tc.expectError && err == nil { t.Fatal("expected error but got none", err) } if !tc.expectError && err != nil { t.Fatalf("expected no error but got: %v", err) } var httpErr api.HttpError isHttpError := errors.As(err, &httpErr) if tc.expectHttpError && !isHttpError { t.Errorf("expected an HTTP error but got: %v", err) } if !tc.expectHttpError && isHttpError { t.Errorf("expected no HTTP error but got one: %v", httpErr) } if err != nil && tc.expectHttpError && isHttpError { status, _ := httpErr.HttpError() if status != tc.expectHttpStatus { t.Errorf("expected %d as HTTP status code but got %d", tc.expectHttpStatus, status) } } if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) { t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths())) } for _, path := range tc.expectOutputPaths { if !slices.Contains(tc.ctx.OutputPaths(), path) { t.Errorf("expected '%s' in output paths %v", path, tc.ctx.OutputPaths()) } } }) } } func TestReadMetadataHandler(t *testing.T) { for _, tc := range []struct { scenario string ctx *api.ContextMock engine gotenberg.PdfEngine expectError bool expectedError error expectHttpError bool expectHttpStatus int expectedJson string }{ { scenario: "missing at least one mandatory file", ctx: &api.ContextMock{Context: new(api.Context)}, expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, }, { scenario: "error from PDF engine", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) return ctx }(), engine: &gotenberg.PdfEngineMock{ ReadMetadataMock: func(ctx context.Context, logger *zap.Logger, inputPath string) (map[string]interface{}, error) { return nil, errors.New("foo") }, }, expectError: true, expectHttpError: false, }, { scenario: "success", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) return ctx }(), engine: &gotenberg.PdfEngineMock{ ReadMetadataMock: func(ctx context.Context, logger *zap.Logger, inputPath string) (map[string]interface{}, error) { return map[string]interface{}{ "foo": "bar", "bar": "foo", }, nil }, }, expectError: true, expectedError: api.ErrNoOutputFile, expectHttpError: false, expectedJson: `{"file.pdf":{"bar":"foo","foo":"bar"}}`, }, } { t.Run(tc.scenario, func(t *testing.T) { tc.ctx.SetLogger(zap.NewNop()) req := httptest.NewRequest(http.MethodPost, "/forms/pdfengines/metadata/read", nil) rec := httptest.NewRecorder() c := echo.New().NewContext(req, rec) c.Set("context", tc.ctx.Context) err := readMetadataRoute(tc.engine).Handler(c) if tc.expectError && err == nil { t.Fatal("expected error but got none", err) } if !tc.expectError && err != nil { t.Fatalf("expected no error but got: %v", err) } var httpErr api.HttpError isHttpError := errors.As(err, &httpErr) if tc.expectHttpError && !isHttpError { t.Errorf("expected an HTTP error but got: %v", err) } if !tc.expectHttpError && isHttpError { t.Errorf("expected no HTTP error but got one: %v", httpErr) } if tc.expectedError != nil && !errors.Is(err, tc.expectedError) { t.Fatalf("expected error %v but got: %v", tc.expectedError, err) } if err != nil && tc.expectHttpError && isHttpError { status, _ := httpErr.HttpError() if status != tc.expectHttpStatus { t.Errorf("expected %d as HTTP status code but got %d", tc.expectHttpStatus, status) } } if tc.expectedJson != "" && tc.expectedJson != strings.TrimSpace(rec.Body.String()) { t.Errorf("expected '%s' as HTTP response but got '%s'", tc.expectedJson, strings.TrimSpace(rec.Body.String())) } }) } } func TestWriteMetadataHandler(t *testing.T) { for _, tc := range []struct { scenario string ctx *api.ContextMock engine gotenberg.PdfEngine expectError bool expectHttpError bool expectHttpStatus int expectOutputPathsCount int expectOutputPaths []string }{ { scenario: "missing at least one mandatory file", ctx: &api.ContextMock{Context: new(api.Context)}, expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "no metadata form field", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) return ctx }(), expectError: true, expectHttpError: true, expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, { scenario: "invalid metadata form field", 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": { "foo", }, }) return ctx }(), expectError: true, expectHttpError: true, 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 { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return errors.New("foo") }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "cannot add output paths", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, }) ctx.SetCancelled(true) return ctx }(), engine: &gotenberg.PdfEngineMock{ WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return nil }, }, expectError: true, expectHttpError: false, expectOutputPathsCount: 0, }, { scenario: "success", ctx: func() *api.ContextMock { ctx := &api.ContextMock{Context: new(api.Context)} ctx.SetFiles(map[string]string{ "file.pdf": "/file.pdf", }) ctx.SetValues(map[string][]string{ "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, }) return ctx }(), engine: &gotenberg.PdfEngineMock{ WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return nil }, }, expectError: false, expectHttpError: false, expectOutputPathsCount: 1, }, } { t.Run(tc.scenario, func(t *testing.T) { tc.ctx.SetLogger(zap.NewNop()) c := echo.New().NewContext(nil, nil) c.Set("context", tc.ctx.Context) err := writeMetadataRoute(tc.engine).Handler(c) if tc.expectError && err == nil { t.Fatal("expected error but got none", err) } if !tc.expectError && err != nil { t.Fatalf("expected no error but got: %v", err) } var httpErr api.HttpError isHttpError := errors.As(err, &httpErr) if tc.expectHttpError && !isHttpError { t.Errorf("expected an HTTP error but got: %v", err) } if !tc.expectHttpError && isHttpError { t.Errorf("expected no HTTP error but got one: %v", httpErr) } if err != nil && tc.expectHttpError && isHttpError { status, _ := httpErr.HttpError() if status != tc.expectHttpStatus { t.Errorf("expected %d as HTTP status code but got %d", tc.expectHttpStatus, status) } } if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) { t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths())) } for _, path := range tc.expectOutputPaths { if !slices.Contains(tc.ctx.OutputPaths(), path) { t.Errorf("expected '%s' in output paths %v", path, tc.ctx.OutputPaths()) } } }) } }