mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
adding tests for page ranges
This commit is contained in:
@@ -24,6 +24,7 @@ func TestArgKeys(t *testing.T) {
|
||||
MarginLeftArgKey,
|
||||
MarginRightArgKey,
|
||||
LandscapeArgKey,
|
||||
PageRangesArgKey,
|
||||
}
|
||||
assert.Equal(t, expected, ArgKeys())
|
||||
}
|
||||
|
||||
@@ -161,15 +161,15 @@ func (p chromePrinter) Print(destination string) error {
|
||||
printToPdfArgs.SetPageRanges(p.opts.PageRanges)
|
||||
}
|
||||
// print the page to PDF.
|
||||
// TODO catch page range error?
|
||||
print, err := targetClient.Page.PrintToPDF(
|
||||
ctx,
|
||||
printToPdfArgs,
|
||||
)
|
||||
if err != nil {
|
||||
// TODO: find a way to check it in the handlers.
|
||||
if strings.Contains(err.Error(), "Page range syntax error") {
|
||||
return xerror.Invalid(
|
||||
"",
|
||||
op,
|
||||
fmt.Sprintf("'%s' is not a valid Google Chrome page ranges", p.opts.PageRanges),
|
||||
err,
|
||||
)
|
||||
|
||||
@@ -38,6 +38,26 @@ func TestHTMLPrinter(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// options with a page ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "1"
|
||||
p = NewHTMLPrinter(logger, fpath, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as options have
|
||||
// a wrong page ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "foo"
|
||||
p = NewHTMLPrinter(logger, fpath, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
test.AssertError(t, err)
|
||||
assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as context.Context
|
||||
// should timeout.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
|
||||
@@ -40,6 +40,28 @@ func TestMarkdownPrinter(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// options with a page ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "1"
|
||||
p, err = NewMarkdownPrinter(logger, fpath, opts)
|
||||
assert.Nil(t, err)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as options have
|
||||
// a wrong page ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "foo"
|
||||
p, err = NewMarkdownPrinter(logger, fpath, opts)
|
||||
assert.Nil(t, err)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
test.AssertError(t, err)
|
||||
assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as context.Context
|
||||
// should timeout.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
|
||||
@@ -91,9 +91,6 @@ func (p officePrinter) Print(destination string) error {
|
||||
func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error {
|
||||
const op string = "printer.unoconv"
|
||||
resolver := func() error {
|
||||
hasPageRanges := func() bool {
|
||||
return p.opts.PageRanges != "" && len(p.fpaths) == 1
|
||||
}
|
||||
port, err := freeport.GetFreePort()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -109,14 +106,15 @@ func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) e
|
||||
if p.opts.Landscape {
|
||||
args = append(args, "--printer", "PaperOrientation=landscape")
|
||||
}
|
||||
if hasPageRanges() {
|
||||
if p.opts.PageRanges != "" {
|
||||
args = append(args, "--export", fmt.Sprintf("PageRange=%s", p.opts.PageRanges))
|
||||
}
|
||||
args = append(args, "--output", destination, fpath)
|
||||
if err := xexec.Run(ctx, p.logger, "unoconv", args...); err != nil {
|
||||
if hasPageRanges() && strings.Contains(err.Error(), "exit status 5") {
|
||||
// TODO: find a way to check it in the handlers.
|
||||
if p.opts.PageRanges != "" && strings.Contains(err.Error(), "exit status 5") {
|
||||
return xerror.Invalid(
|
||||
"",
|
||||
op,
|
||||
fmt.Sprintf("'%s' is not a valid LibreOffice page ranges", p.opts.PageRanges),
|
||||
err,
|
||||
)
|
||||
|
||||
@@ -46,6 +46,26 @@ func TestOfficePrinter(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// options with page ranges.
|
||||
opts = DefaultOfficePrinterOptions(config)
|
||||
opts.PageRanges = "1-1"
|
||||
p = NewOfficePrinter(logger, []string{fpaths[0]}, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as options have
|
||||
// a wrong page ranges.
|
||||
opts = DefaultOfficePrinterOptions(config)
|
||||
opts.PageRanges = "foo"
|
||||
p = NewOfficePrinter(logger, []string{fpaths[0]}, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
test.AssertError(t, err)
|
||||
assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as context.Context
|
||||
// should timeout.
|
||||
opts = DefaultOfficePrinterOptions(config)
|
||||
|
||||
@@ -38,6 +38,26 @@ func TestURLPrinter(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// options with a pages ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "1"
|
||||
p = NewURLPrinter(logger, URL, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
assert.Nil(t, err)
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as options have
|
||||
// a wrong page ranges.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
opts.PageRanges = "foo"
|
||||
p = NewURLPrinter(logger, URL, opts)
|
||||
dest = test.GenerateDestination()
|
||||
err = p.Print(dest)
|
||||
test.AssertError(t, err)
|
||||
assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
|
||||
err = os.RemoveAll(dest)
|
||||
assert.Nil(t, err)
|
||||
// should not be OK as context.Context
|
||||
// should timeout.
|
||||
opts = DefaultChromePrinterOptions(config)
|
||||
|
||||
Reference in New Issue
Block a user