mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-14 03:12:14 +01:00
feat: add more PDF formats, drastically improve LibreOffice long-running instance management
This commit is contained in:
@@ -3,213 +3,178 @@ package pdfengines
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestNewMultiPDFEngines(t *testing.T) {
|
||||
engine1 := &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
engine2 := &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
|
||||
multi := newMultiPDFEngines(engine1, engine2)
|
||||
|
||||
if len(multi.engines) != 2 {
|
||||
t.Fatalf("expected %d engines but got %d", 2, len(multi.engines))
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(engine1, multi.engines[0]) {
|
||||
t.Errorf("expected %v, but got: %v", engine1, multi.engines[0])
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(engine2, multi.engines[1]) {
|
||||
t.Errorf("expected %v, but got: %v", engine2, multi.engines[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiPDFEngines_Merge(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
ctx context.Context
|
||||
engines []gotenberg.PDFEngine
|
||||
expectErr bool
|
||||
tests := []struct {
|
||||
name string
|
||||
engine *multiPDFEngines
|
||||
ctx context.Context
|
||||
expectMergeErr bool
|
||||
}{
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
name: "nominal behavior",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
},
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
name: "at least one engine does not return an error",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
},
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
name: "all engines return an error",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return errors.New("bar")
|
||||
},
|
||||
},
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
expectMergeErr: true,
|
||||
},
|
||||
{
|
||||
name: "context expired",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
),
|
||||
ctx: func() context.Context {
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
defer cancel()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectMergeErr: true,
|
||||
},
|
||||
} {
|
||||
multi := newMultiPDFEngines(tc.engines...)
|
||||
err := multi.Merge(tc.ctx, nil, nil, "")
|
||||
}
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.engine.Merge(tc.ctx, zap.NewNop(), nil, "")
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
if tc.expectMergeErr && err == nil {
|
||||
t.Errorf("expected engine.Merge() error, but got none")
|
||||
}
|
||||
|
||||
if !tc.expectMergeErr && err != nil {
|
||||
t.Errorf("expected no error from engine.Merge(), but got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiPDFEngines_Convert(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
ctx context.Context
|
||||
engines []gotenberg.PDFEngine
|
||||
expectErr bool
|
||||
tests := []struct {
|
||||
name string
|
||||
engine *multiPDFEngines
|
||||
ctx context.Context
|
||||
expectConvertErr bool
|
||||
}{
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
name: "nominal behavior",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
},
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
name: "at least one engine does not return an error",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
},
|
||||
{
|
||||
ctx: context.TODO(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
name: "all engines return an error",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("bar")
|
||||
},
|
||||
},
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
},
|
||||
),
|
||||
ctx: context.Background(),
|
||||
expectConvertErr: true,
|
||||
},
|
||||
{
|
||||
name: "context expired",
|
||||
engine: newMultiPDFEngines(
|
||||
gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
),
|
||||
ctx: func() context.Context {
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
defer cancel()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
return []gotenberg.PDFEngine{
|
||||
ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectConvertErr: true,
|
||||
},
|
||||
} {
|
||||
multi := newMultiPDFEngines(tc.engines...)
|
||||
err := multi.Convert(tc.ctx, nil, "", "", "")
|
||||
}
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.engine.Convert(tc.ctx, zap.NewNop(), "", "", "")
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
if tc.expectConvertErr && err == nil {
|
||||
t.Errorf("expected engine.Convert() error, but got none")
|
||||
}
|
||||
|
||||
if !tc.expectConvertErr && err != nil {
|
||||
t.Errorf("expected no error from engine.Convert(), but got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,18 @@ func (mod *PDFEngines) Provision(ctx *gotenberg.Context) error {
|
||||
names := flags.MustStringSlice("pdfengines-engines")
|
||||
mod.disableRoutes = flags.MustBool("pdfengines-disable-routes")
|
||||
|
||||
loggerProvider, err := ctx.Module(new(gotenberg.LoggerProvider))
|
||||
if err != nil {
|
||||
return fmt.Errorf("get logger provider: %w", err)
|
||||
}
|
||||
|
||||
logger, err := loggerProvider.(gotenberg.LoggerProvider).Logger(mod)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get logger: %w", err)
|
||||
}
|
||||
|
||||
logger = logger.Named("pdfengines")
|
||||
|
||||
engines, err := ctx.Modules(new(gotenberg.PDFEngine))
|
||||
if err != nil {
|
||||
return fmt.Errorf("get PDF engines: %w", err)
|
||||
@@ -68,6 +80,13 @@ func (mod *PDFEngines) Provision(ctx *gotenberg.Context) error {
|
||||
// Selection from user.
|
||||
mod.names = names
|
||||
|
||||
for i, name := range names {
|
||||
logger.Warn("unoconv-pdfengine is deprecated; prefer uno-pdfengine instead")
|
||||
if name == "unoconv-pdfengine" {
|
||||
mod.names[i] = "uno-pdfengine"
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package pdfengines
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -11,38 +10,7 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ProtoModule struct {
|
||||
descriptor func() gotenberg.ModuleDescriptor
|
||||
}
|
||||
|
||||
func (mod ProtoModule) Descriptor() gotenberg.ModuleDescriptor {
|
||||
return mod.descriptor()
|
||||
}
|
||||
|
||||
type ProtoValidator struct {
|
||||
ProtoModule
|
||||
validate func() error
|
||||
}
|
||||
|
||||
func (mod ProtoValidator) Validate() error {
|
||||
return mod.validate()
|
||||
}
|
||||
|
||||
type ProtoPDFEngine struct {
|
||||
ProtoValidator
|
||||
merge func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error
|
||||
convert func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error
|
||||
}
|
||||
|
||||
func (mod ProtoPDFEngine) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return mod.merge(ctx, logger, inputPaths, outputPath)
|
||||
}
|
||||
|
||||
func (mod ProtoPDFEngine) Convert(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return mod.convert(ctx, logger, format, inputPath, outputPath)
|
||||
}
|
||||
|
||||
func TestPDFEngine_Descriptor(t *testing.T) {
|
||||
func TestPDFEngines_Descriptor(t *testing.T) {
|
||||
descriptor := PDFEngines{}.Descriptor()
|
||||
|
||||
actual := reflect.TypeOf(descriptor.New())
|
||||
@@ -53,71 +21,98 @@ func TestPDFEngine_Descriptor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngine_Provision(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
ctx *gotenberg.Context
|
||||
expectNames []string
|
||||
expectEnginesCount int
|
||||
expectErr bool
|
||||
func TestPDFEngines_Provision(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx *gotenberg.Context
|
||||
expectPDFEngineNames []string
|
||||
expectProvisionErr bool
|
||||
}{
|
||||
{
|
||||
name: "no selection from user",
|
||||
ctx: func() *gotenberg.Context {
|
||||
engine := struct{ ProtoPDFEngine }{}
|
||||
engine.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine }}
|
||||
provider := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.LoggerProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LoggerMock = func(mod gotenberg.Module) (*zap.Logger, error) {
|
||||
return zap.NewNop(), nil
|
||||
}
|
||||
|
||||
engine := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.ValidatorMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "bar", New: func() gotenberg.Module { return engine }}
|
||||
}
|
||||
engine.ValidateMock = func() error {
|
||||
return nil
|
||||
}
|
||||
engine.validate = func() error { return errors.New("foo") }
|
||||
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(PDFEngines).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
engine.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectPDFEngineNames: []string{"bar"},
|
||||
},
|
||||
{
|
||||
name: "selection from user",
|
||||
ctx: func() *gotenberg.Context {
|
||||
engine := struct{ ProtoPDFEngine }{}
|
||||
engine.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine }}
|
||||
provider := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.LoggerProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LoggerMock = func(mod gotenberg.Module) (*zap.Logger, error) {
|
||||
return zap.NewNop(), nil
|
||||
}
|
||||
engine.validate = func() error { return nil }
|
||||
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(PDFEngines).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
engine.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectNames: []string{"foo"},
|
||||
expectEnginesCount: 1,
|
||||
},
|
||||
{
|
||||
ctx: func() *gotenberg.Context {
|
||||
engine1 := struct{ ProtoPDFEngine }{}
|
||||
engine1.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
engine1 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.ValidatorMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine1.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "a", New: func() gotenberg.Module { return engine1 }}
|
||||
}
|
||||
engine1.validate = func() error { return nil }
|
||||
engine1.ValidateMock = func() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
engine2 := struct{ ProtoPDFEngine }{}
|
||||
engine2.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
engine2 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.ValidatorMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine2.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "b", New: func() gotenberg.Module { return engine2 }}
|
||||
}
|
||||
engine2.validate = func() error { return nil }
|
||||
engine2.ValidateMock = func() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
fs := new(PDFEngines).Descriptor().FlagSet
|
||||
err := fs.Parse([]string{"--pdfengines-engines=b", "--pdfengines-engines=a"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error but got: %v", err)
|
||||
t.Fatalf("expected no error from fs.Parse(), but got: %v", err)
|
||||
}
|
||||
|
||||
return gotenberg.NewContext(
|
||||
@@ -125,33 +120,47 @@ func TestPDFEngine_Provision(t *testing.T) {
|
||||
FlagSet: fs,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
engine1.Descriptor(),
|
||||
engine2.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectNames: []string{"b", "a"},
|
||||
expectEnginesCount: 2,
|
||||
expectPDFEngineNames: []string{"b", "a"},
|
||||
},
|
||||
{
|
||||
name: "user select deprecated unoconv-pdfengine",
|
||||
ctx: func() *gotenberg.Context {
|
||||
engine1 := struct{ ProtoPDFEngine }{}
|
||||
engine1.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "a", New: func() gotenberg.Module { return engine1 }}
|
||||
provider := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.LoggerProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LoggerMock = func(mod gotenberg.Module) (*zap.Logger, error) {
|
||||
return zap.NewNop(), nil
|
||||
}
|
||||
engine1.validate = func() error { return nil }
|
||||
|
||||
engine2 := struct{ ProtoPDFEngine }{}
|
||||
engine2.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "b", New: func() gotenberg.Module { return engine2 }}
|
||||
engine := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.ValidatorMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "uno-pdfengine", New: func() gotenberg.Module { return engine }}
|
||||
}
|
||||
engine.ValidateMock = func() error {
|
||||
return nil
|
||||
}
|
||||
engine2.validate = func() error { return nil }
|
||||
|
||||
fs := new(PDFEngines).Descriptor().FlagSet
|
||||
err := fs.Parse([]string{"--pdfengines-engines=b"})
|
||||
err := fs.Parse([]string{"--pdfengines-engines=unoconv-pdfengine"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected error but got: %v", err)
|
||||
t.Fatalf("expected no error from fs.Parse(), but got: %v", err)
|
||||
}
|
||||
|
||||
return gotenberg.NewContext(
|
||||
@@ -159,56 +168,136 @@ func TestPDFEngine_Provision(t *testing.T) {
|
||||
FlagSet: fs,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
engine1.Descriptor(),
|
||||
engine2.Descriptor(),
|
||||
provider.Descriptor(),
|
||||
engine.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectNames: []string{"b"},
|
||||
expectEnginesCount: 2,
|
||||
expectPDFEngineNames: []string{"uno-pdfengine"},
|
||||
},
|
||||
} {
|
||||
mod := new(PDFEngines)
|
||||
err := mod.Provision(tc.ctx)
|
||||
{
|
||||
name: "no logger provider",
|
||||
ctx: func() *gotenberg.Context {
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(PDFEngines).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{},
|
||||
)
|
||||
}(),
|
||||
expectProvisionErr: true,
|
||||
},
|
||||
{
|
||||
name: "no logger from logger provider",
|
||||
ctx: func() *gotenberg.Context {
|
||||
provider := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.LoggerProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LoggerMock = func(mod gotenberg.Module) (*zap.Logger, error) {
|
||||
return nil, errors.New("foo")
|
||||
}
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(PDFEngines).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectProvisionErr: true,
|
||||
},
|
||||
{
|
||||
name: "no valid PDF engines",
|
||||
ctx: func() *gotenberg.Context {
|
||||
provider := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.LoggerProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LoggerMock = func(mod gotenberg.Module) (*zap.Logger, error) {
|
||||
return zap.NewNop(), nil
|
||||
}
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
engine := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.ValidatorMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "bar", New: func() gotenberg.Module { return engine }}
|
||||
}
|
||||
engine.ValidateMock = func() error {
|
||||
return errors.New("foo")
|
||||
}
|
||||
|
||||
if len(tc.expectNames) != len(mod.names) {
|
||||
t.Errorf("test %d: expected %d names but got %d", i, len(tc.expectNames), len(mod.names))
|
||||
}
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(PDFEngines).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
engine.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectProvisionErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
if tc.expectEnginesCount != len(mod.engines) {
|
||||
t.Errorf("test %d: expected %d engines but got %d", i, tc.expectEnginesCount, len(mod.engines))
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mod := new(PDFEngines)
|
||||
err := mod.Provision(tc.ctx)
|
||||
|
||||
for index, name := range mod.names {
|
||||
if name != tc.expectNames[index] {
|
||||
t.Errorf("test %d: expected name at index %d to be %s, but got: %s", i, index, name, tc.expectNames[index])
|
||||
if tc.expectProvisionErr && err == nil {
|
||||
t.Fatal("expected mod.Provision() error, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
if !tc.expectProvisionErr && err != nil {
|
||||
t.Fatalf("expected no error from mod.Provision(), but got: %v", err)
|
||||
}
|
||||
|
||||
if len(tc.expectPDFEngineNames) != len(mod.names) {
|
||||
t.Errorf("expected %d names but got %d", len(tc.expectPDFEngineNames), len(mod.names))
|
||||
}
|
||||
|
||||
for index, name := range mod.names {
|
||||
if name != tc.expectPDFEngineNames[index] {
|
||||
t.Errorf("expected name at index %d to be %s, but got: %s", index, name, tc.expectPDFEngineNames[index])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngine_Validate(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
names []string
|
||||
engines []gotenberg.PDFEngine
|
||||
expectErr bool
|
||||
func TestPDFEngines_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
names []string
|
||||
engines []gotenberg.PDFEngine
|
||||
expectValidateErr bool
|
||||
}{
|
||||
{
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "existing PDF engine",
|
||||
names: []string{"foo"},
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
engine := struct{ ProtoPDFEngine }{}
|
||||
engine.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
engine := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine }}
|
||||
}
|
||||
|
||||
@@ -218,15 +307,22 @@ func TestPDFEngine_Validate(t *testing.T) {
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "non-existing bar PDF engine",
|
||||
names: []string{"foo", "bar", "baz"},
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
engine1 := struct{ ProtoPDFEngine }{}
|
||||
engine1.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
engine1 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine1.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine1 }}
|
||||
}
|
||||
|
||||
engine2 := struct{ ProtoPDFEngine }{}
|
||||
engine2.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
engine2 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine2.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "baz", New: func() gotenberg.Module { return engine2 }}
|
||||
}
|
||||
|
||||
@@ -235,21 +331,31 @@ func TestPDFEngine_Validate(t *testing.T) {
|
||||
engine2,
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectValidateErr: true,
|
||||
},
|
||||
} {
|
||||
mod := new(PDFEngines)
|
||||
mod.names = tc.names
|
||||
mod.engines = tc.engines
|
||||
err := mod.Validate()
|
||||
{
|
||||
name: "no PDF engine",
|
||||
expectValidateErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mod := PDFEngines{
|
||||
names: tc.names,
|
||||
engines: tc.engines,
|
||||
}
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
err := mod.Validate()
|
||||
|
||||
if tc.expectValidateErr && err == nil {
|
||||
t.Errorf("expected mod.Validate() error, but got none")
|
||||
}
|
||||
|
||||
if !tc.expectValidateErr && err != nil {
|
||||
t.Errorf("expected no error from mod.Validate(), but got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,76 +365,81 @@ func TestPDFEngines_SystemMessages(t *testing.T) {
|
||||
|
||||
messages := mod.SystemMessages()
|
||||
if len(messages) != 1 {
|
||||
t.Errorf("expected one and only one message but got %d", len(messages))
|
||||
t.Errorf("expected one and only one message from mod.SystemMessages(), but got %d", len(messages))
|
||||
}
|
||||
|
||||
expect := strings.Join(mod.names[:], " ")
|
||||
if messages[0] != expect {
|
||||
t.Errorf("expected message '%s' but got '%s'", expect, messages[0])
|
||||
t.Errorf("expected message '%s' from mod.SystemMessages(), but got '%s'", expect, messages[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngine_PDFEngine(t *testing.T) {
|
||||
mod := new(PDFEngines)
|
||||
mod.names = []string{"foo", "bar"}
|
||||
mod.engines = func() []gotenberg.PDFEngine {
|
||||
engine1 := struct{ ProtoPDFEngine }{}
|
||||
engine1.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine1 }}
|
||||
}
|
||||
func TestPDFEngines_PDFEngine(t *testing.T) {
|
||||
mod := PDFEngines{
|
||||
names: []string{"foo", "bar"},
|
||||
engines: func() []gotenberg.PDFEngine {
|
||||
engine1 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine1.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine1 }}
|
||||
}
|
||||
|
||||
engine2 := struct{ ProtoPDFEngine }{}
|
||||
engine2.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "bar", New: func() gotenberg.Module { return engine2 }}
|
||||
}
|
||||
engine2 := struct {
|
||||
gotenberg.ModuleMock
|
||||
gotenberg.PDFEngineMock
|
||||
}{}
|
||||
engine2.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "bar", New: func() gotenberg.Module { return engine2 }}
|
||||
}
|
||||
|
||||
return []gotenberg.PDFEngine{
|
||||
engine1,
|
||||
engine2,
|
||||
}
|
||||
}()
|
||||
return []gotenberg.PDFEngine{
|
||||
engine1,
|
||||
engine2,
|
||||
}
|
||||
}(),
|
||||
}
|
||||
|
||||
_, err := mod.PDFEngine()
|
||||
if err != nil {
|
||||
t.Errorf("expected no error but got: %v", err)
|
||||
t.Errorf("expected no error from mod.PDFEngine, but got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngine_Routes(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
expectRoutes int
|
||||
disableRoutes bool
|
||||
func TestPDFEngines_Routes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mod PDFEngines
|
||||
expectRoutesCount int
|
||||
}{
|
||||
{
|
||||
expectRoutes: 2,
|
||||
name: "route not disabled",
|
||||
mod: PDFEngines{
|
||||
engines: []gotenberg.PDFEngine{
|
||||
gotenberg.PDFEngineMock{},
|
||||
},
|
||||
},
|
||||
expectRoutesCount: 2,
|
||||
},
|
||||
{
|
||||
disableRoutes: true,
|
||||
name: "route disabled",
|
||||
mod: PDFEngines{
|
||||
disableRoutes: true,
|
||||
},
|
||||
},
|
||||
} {
|
||||
mod := new(PDFEngines)
|
||||
mod.engines = []gotenberg.PDFEngine{
|
||||
struct{ ProtoPDFEngine }{},
|
||||
}
|
||||
mod.disableRoutes = tc.disableRoutes
|
||||
}
|
||||
|
||||
routes, err := mod.Routes()
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
routes, err := tc.mod.Routes()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error from mod.Routes(), but got: %v", err)
|
||||
}
|
||||
|
||||
if tc.expectRoutes != len(routes) {
|
||||
t.Errorf("test %d: expected %d routes but got %d", i, tc.expectRoutes, len(routes))
|
||||
}
|
||||
if tc.expectRoutesCount != len(routes) {
|
||||
t.Errorf("expected %d routes from mod.Routes(), but got %d", tc.expectRoutesCount, len(routes))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*ProtoModule)(nil)
|
||||
_ gotenberg.Validator = (*ProtoValidator)(nil)
|
||||
_ gotenberg.Module = (*ProtoValidator)(nil)
|
||||
_ gotenberg.PDFEngine = (*ProtoPDFEngine)(nil)
|
||||
_ gotenberg.Module = (*ProtoPDFEngine)(nil)
|
||||
_ gotenberg.Validator = (*ProtoPDFEngine)(nil)
|
||||
)
|
||||
|
||||
@@ -13,7 +13,8 @@ import (
|
||||
)
|
||||
|
||||
func TestMergeHandler(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx *api.MockContext
|
||||
engine gotenberg.PDFEngine
|
||||
expectErr bool
|
||||
@@ -22,34 +23,75 @@ func TestMergeHandler(t *testing.T) {
|
||||
expectOutputPathsCount int
|
||||
}{
|
||||
{
|
||||
name: "nominal behavior",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectOutputPathsCount: 1,
|
||||
},
|
||||
{
|
||||
name: "invalid form data: no PDF",
|
||||
ctx: &api.MockContext{Context: &api.Context{}},
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "merge fail",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "nominal behavior with a PDF format",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
|
||||
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, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectOutputPathsCount: 1,
|
||||
},
|
||||
{
|
||||
name: "convert to PDF format fail",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
@@ -61,153 +103,106 @@ func TestMergeHandler(t *testing.T) {
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
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, format, inputPath, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid PDF format",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return gotenberg.ErrPDFFormatNotAvailable
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
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, format, inputPath, outputPath string) error {
|
||||
return gotenberg.ErrPDFFormatNotAvailable
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "cannot add output paths",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetCancelled(true)
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c := echo.New().NewContext(nil, nil)
|
||||
c.Set("context", tc.ctx.Context)
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectOutputPathsCount: 1,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
err := mergeRoute(tc.engine).Handler(c)
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
merge: func(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectOutputPathsCount: 1,
|
||||
},
|
||||
} {
|
||||
c := echo.New().NewContext(nil, nil)
|
||||
c.Set("context", tc.ctx.Context)
|
||||
|
||||
err := mergeRoute(tc.engine).Handler(c)
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
|
||||
var httpErr api.HTTPError
|
||||
isHTTPErr := errors.As(err, &httpErr)
|
||||
|
||||
if tc.expectHTTPErr && !isHTTPErr {
|
||||
t.Errorf("test %d: expected HTTP error but got: %v", i, err)
|
||||
}
|
||||
|
||||
if !tc.expectHTTPErr && isHTTPErr {
|
||||
t.Errorf("test %d: expected no HTTP error but got one: %v", i, httpErr)
|
||||
}
|
||||
|
||||
if err != nil && tc.expectHTTPErr && isHTTPErr {
|
||||
status, _ := httpErr.HTTPError()
|
||||
if status != tc.expectHTTPStatus {
|
||||
t.Errorf("test %d: expected %d HTTP status code but got %d", i, tc.expectHTTPStatus, status)
|
||||
if tc.expectErr && err == nil {
|
||||
t.Fatal("expected error from merge handler, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) {
|
||||
t.Errorf("test %d: expected %d output paths but got %d", i, tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
|
||||
}
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Fatalf("expected no error from merge handler, but got: %v", err)
|
||||
}
|
||||
|
||||
var httpErr api.HTTPError
|
||||
isHTTPErr := errors.As(err, &httpErr)
|
||||
|
||||
if tc.expectHTTPErr && !isHTTPErr {
|
||||
t.Errorf("expected HTTP error from merge handler, but got: %v", err)
|
||||
}
|
||||
|
||||
if !tc.expectHTTPErr && isHTTPErr {
|
||||
t.Errorf("expected no HTTP error from merge handler, but got one: %v", httpErr)
|
||||
}
|
||||
|
||||
if err != nil && tc.expectHTTPErr && isHTTPErr {
|
||||
status, _ := httpErr.HTTPError()
|
||||
if status != tc.expectHTTPStatus {
|
||||
t.Errorf("expected %d HTTP status code from merge handler, but got %d", tc.expectHTTPStatus, status)
|
||||
}
|
||||
}
|
||||
|
||||
if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) {
|
||||
t.Errorf("expected %d output paths from merge handler, but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertHandler(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx *api.MockContext
|
||||
engine gotenberg.PDFEngine
|
||||
expectErr bool
|
||||
@@ -216,132 +211,109 @@ func TestConvertHandler(t *testing.T) {
|
||||
expectOutputPathsCount int
|
||||
}{
|
||||
{
|
||||
ctx: &api.MockContext{Context: &api.Context{}},
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "nominal behavior",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return gotenberg.ErrPDFFormatNotAvailable
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetCancelled(true)
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
"foo",
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectOutputPathsCount: 1,
|
||||
},
|
||||
{
|
||||
name: "nominal behavior, but with 3 PDFs",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
"bar.pdf": "/bar/bar.pdf",
|
||||
"baz.pdf": "/baz/baz.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectOutputPathsCount: 3,
|
||||
},
|
||||
{
|
||||
name: "invalid form data: no PDF",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "invalid form data: no PDF format",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "convert to PDF format fail",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "PDF format not available",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
"bar.pdf": "/foo/bar.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
@@ -351,49 +323,76 @@ func TestConvertHandler(t *testing.T) {
|
||||
|
||||
return ctx
|
||||
}(),
|
||||
engine: func() gotenberg.PDFEngine {
|
||||
return &ProtoPDFEngine{
|
||||
convert: func(_ context.Context, _ *zap.Logger, _, _, _ string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}(),
|
||||
expectOutputPathsCount: 2,
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return gotenberg.ErrPDFFormatNotAvailable
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
expectHTTPErr: true,
|
||||
expectHTTPStatus: http.StatusBadRequest,
|
||||
},
|
||||
} {
|
||||
c := echo.New().NewContext(nil, nil)
|
||||
c.Set("context", tc.ctx.Context)
|
||||
{
|
||||
name: "cannot add output paths",
|
||||
ctx: func() *api.MockContext {
|
||||
ctx := &api.MockContext{Context: &api.Context{}}
|
||||
ctx.SetFiles(map[string]string{
|
||||
"foo.pdf": "/foo/foo.pdf",
|
||||
})
|
||||
ctx.SetValues(map[string][]string{
|
||||
"pdfFormat": {
|
||||
gotenberg.FormatPDFA1a,
|
||||
},
|
||||
})
|
||||
ctx.SetCancelled(true)
|
||||
|
||||
err := convertRoute(tc.engine).Handler(c)
|
||||
return ctx
|
||||
}(),
|
||||
engine: gotenberg.PDFEngineMock{
|
||||
ConvertMock: func(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c := echo.New().NewContext(nil, nil)
|
||||
c.Set("context", tc.ctx.Context)
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
err := convertRoute(tc.engine).Handler(c)
|
||||
|
||||
var httpErr api.HTTPError
|
||||
isHTTPErr := errors.As(err, &httpErr)
|
||||
|
||||
if tc.expectHTTPErr && !isHTTPErr {
|
||||
t.Errorf("test %d: expected HTTP error but got: %v", i, err)
|
||||
}
|
||||
|
||||
if !tc.expectHTTPErr && isHTTPErr {
|
||||
t.Errorf("test %d: expected no HTTP error but got one: %v", i, httpErr)
|
||||
}
|
||||
|
||||
if err != nil && tc.expectHTTPErr && isHTTPErr {
|
||||
status, _ := httpErr.HTTPError()
|
||||
if status != tc.expectHTTPStatus {
|
||||
t.Errorf("test %d: expected %d HTTP status code but got %d", i, tc.expectHTTPStatus, status)
|
||||
if tc.expectErr && err == nil {
|
||||
t.Fatal("expected error from convert handler, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) {
|
||||
t.Errorf("test %d: expected %d output paths but got %d", i, tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
|
||||
}
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Fatalf("expected no error from convert handler, but got: %v", err)
|
||||
}
|
||||
|
||||
var httpErr api.HTTPError
|
||||
isHTTPErr := errors.As(err, &httpErr)
|
||||
|
||||
if tc.expectHTTPErr && !isHTTPErr {
|
||||
t.Errorf("expected HTTP error from convert handler, but got: %v", err)
|
||||
}
|
||||
|
||||
if !tc.expectHTTPErr && isHTTPErr {
|
||||
t.Errorf("expected no HTTP error from convert handler, but got one: %v", httpErr)
|
||||
}
|
||||
|
||||
if err != nil && tc.expectHTTPErr && isHTTPErr {
|
||||
status, _ := httpErr.HTTPError()
|
||||
if status != tc.expectHTTPStatus {
|
||||
t.Errorf("expected %d HTTP status code from convert handler, but got %d", tc.expectHTTPStatus, status)
|
||||
}
|
||||
}
|
||||
|
||||
if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) {
|
||||
t.Errorf("expected %d output paths from convert handler, but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user