mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
adding resource package tests
This commit is contained in:
@@ -30,17 +30,17 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
|
|||||||
return printer.ChromePrinterOptions{}, err
|
return printer.ChromePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
headerHTML, footerHTML,
|
headerHTML, footerHTML,
|
||||||
err := resource.HeaderFooterContents(r)
|
err := resource.HeaderFooterContents(r, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return printer.ChromePrinterOptions{}, err
|
return printer.ChromePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
paperWidth, paperHeight,
|
paperWidth, paperHeight,
|
||||||
err := resource.PaperSizeArgs(r)
|
err := resource.PaperSizeArgs(r, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return printer.ChromePrinterOptions{}, err
|
return printer.ChromePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
marginTop, marginBottom, marginLeft, marginRight,
|
marginTop, marginBottom, marginLeft, marginRight,
|
||||||
err := resource.MarginArgs(r)
|
err := resource.MarginArgs(r, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return printer.ChromePrinterOptions{}, err
|
return printer.ChromePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package resource
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xassert"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/xassert"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
||||||
)
|
)
|
||||||
@@ -125,31 +126,28 @@ PaperSizeArgs is a helper for retrieving
|
|||||||
the "paperWidth" and "paperHeight" arguments
|
the "paperWidth" and "paperHeight" arguments
|
||||||
as float64.
|
as float64.
|
||||||
*/
|
*/
|
||||||
func PaperSizeArgs(r Resource) (float64, float64, error) {
|
func PaperSizeArgs(r Resource, config conf.Config) (float64, float64, error) {
|
||||||
const (
|
const op string = "resource.PaperSizeArgs"
|
||||||
op string = "resource.PaperSizeArgs"
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
defaultPaperWidth float64 = 8.27
|
|
||||||
defaultPaperHeight float64 = 11.7
|
|
||||||
)
|
|
||||||
resolver := func() (float64, float64, error) {
|
resolver := func() (float64, float64, error) {
|
||||||
paperWidth, err := r.Float64Arg(
|
paperWidth, err := r.Float64Arg(
|
||||||
PaperWidthArgKey,
|
PaperWidthArgKey,
|
||||||
defaultPaperWidth,
|
opts.PaperWidth,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultPaperWidth,
|
return opts.PaperWidth,
|
||||||
defaultPaperHeight,
|
opts.PaperHeight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
paperHeight, err := r.Float64Arg(
|
paperHeight, err := r.Float64Arg(
|
||||||
PaperHeightArgKey,
|
PaperHeightArgKey,
|
||||||
defaultPaperHeight,
|
opts.PaperHeight,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultPaperWidth,
|
return opts.PaperWidth,
|
||||||
defaultPaperHeight,
|
opts.PaperHeight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
return paperWidth,
|
return paperWidth,
|
||||||
@@ -173,61 +171,56 @@ MarginArgs is a helper for retrieving
|
|||||||
the "marginTop", "marginBottom", "marginLeft"
|
the "marginTop", "marginBottom", "marginLeft"
|
||||||
and "marginRight" arguments as float64.
|
and "marginRight" arguments as float64.
|
||||||
*/
|
*/
|
||||||
func MarginArgs(r Resource) (float64, float64, float64, float64, error) {
|
func MarginArgs(r Resource, config conf.Config) (float64, float64, float64, float64, error) {
|
||||||
const (
|
const op string = "resource.MarginArgs"
|
||||||
op string = "resource.MarginArgs"
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
defaultMarginTop float64 = 1.0
|
|
||||||
defaultMarginBottom float64 = 1.0
|
|
||||||
defaultMarginLeft float64 = 1.0
|
|
||||||
defaultMarginRight float64 = 1.0
|
|
||||||
)
|
|
||||||
resolver := func() (float64, float64, float64, float64, error) {
|
resolver := func() (float64, float64, float64, float64, error) {
|
||||||
marginTop, err := r.Float64Arg(
|
marginTop, err := r.Float64Arg(
|
||||||
MarginTopArgKey,
|
MarginTopArgKey,
|
||||||
defaultMarginTop,
|
opts.MarginTop,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultMarginTop,
|
return opts.MarginTop,
|
||||||
defaultMarginBottom,
|
opts.MarginBottom,
|
||||||
defaultMarginLeft,
|
opts.MarginLeft,
|
||||||
defaultMarginRight,
|
opts.MarginRight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
marginBottom, err := r.Float64Arg(
|
marginBottom, err := r.Float64Arg(
|
||||||
MarginBottomArgKey,
|
MarginBottomArgKey,
|
||||||
defaultMarginBottom,
|
opts.MarginBottom,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultMarginTop,
|
return opts.MarginTop,
|
||||||
defaultMarginBottom,
|
opts.MarginBottom,
|
||||||
defaultMarginLeft,
|
opts.MarginLeft,
|
||||||
defaultMarginRight,
|
opts.MarginRight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
marginLeft, err := r.Float64Arg(
|
marginLeft, err := r.Float64Arg(
|
||||||
MarginLeftArgKey,
|
MarginLeftArgKey,
|
||||||
defaultMarginLeft,
|
opts.MarginLeft,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultMarginTop,
|
return opts.MarginTop,
|
||||||
defaultMarginBottom,
|
opts.MarginBottom,
|
||||||
defaultMarginLeft,
|
opts.MarginLeft,
|
||||||
defaultMarginRight,
|
opts.MarginRight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
marginRight, err := r.Float64Arg(
|
marginRight, err := r.Float64Arg(
|
||||||
MarginRightArgKey,
|
MarginRightArgKey,
|
||||||
defaultMarginRight,
|
opts.MarginRight,
|
||||||
xassert.Float64NotInferiorTo(0.0),
|
xassert.Float64NotInferiorTo(0.0),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultMarginTop,
|
return opts.MarginTop,
|
||||||
defaultMarginBottom,
|
opts.MarginBottom,
|
||||||
defaultMarginLeft,
|
opts.MarginLeft,
|
||||||
defaultMarginRight,
|
opts.MarginRight,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
return marginTop,
|
return marginTop,
|
||||||
|
|||||||
258
internal/app/xhttp/pkg/resource/arg_test.go
Normal file
258
internal/app/xhttp/pkg/resource/arg_test.go
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||||
|
"github.com/thecodingmachine/gotenberg/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestArgKeys(t *testing.T) {
|
||||||
|
expected := []ArgKey{
|
||||||
|
ResultFilenameArgKey,
|
||||||
|
WaitTimeoutArgKey,
|
||||||
|
WebhookURLArgKey,
|
||||||
|
WebhookURLTimeoutArgKey,
|
||||||
|
RemoteURLArgKey,
|
||||||
|
WaitDelayArgKey,
|
||||||
|
PaperWidthArgKey,
|
||||||
|
PaperHeightArgKey,
|
||||||
|
MarginTopArgKey,
|
||||||
|
MarginBottomArgKey,
|
||||||
|
MarginLeftArgKey,
|
||||||
|
MarginRightArgKey,
|
||||||
|
LandscapeArgKey,
|
||||||
|
}
|
||||||
|
assert.Equal(t, expected, ArgKeys())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWaitTimeoutArg(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
var expected float64
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
config := conf.DefaultConfig()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// argument does not exist.
|
||||||
|
expected = config.DefaultWaitTimeout()
|
||||||
|
v, err := WaitTimeoutArg(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// argument exist.
|
||||||
|
expected = 5.0
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "5.0")
|
||||||
|
v, err = WaitTimeoutArg(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// shoul not be OK as argument
|
||||||
|
// value is < 0.
|
||||||
|
expected = config.DefaultWaitTimeout()
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "-1.0")
|
||||||
|
v, err = WaitTimeoutArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// shoul not be OK as argument
|
||||||
|
// value is > config.MaximumWaitTimeout().
|
||||||
|
expected = config.DefaultWaitTimeout()
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "31.0")
|
||||||
|
v, err = WaitTimeoutArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// should not be OK as
|
||||||
|
// argument value is invalid.
|
||||||
|
expected = config.DefaultWaitTimeout()
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "foo")
|
||||||
|
v, err = WaitTimeoutArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWaitDelayArg(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue float64 = 0.0
|
||||||
|
)
|
||||||
|
var expected float64
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
config := conf.DefaultConfig()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// argument does not exist.
|
||||||
|
expected = defaultValue
|
||||||
|
v, err := WaitDelayArg(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// argument exist.
|
||||||
|
expected = 5.0
|
||||||
|
r.WithArg(WaitDelayArgKey, "5.0")
|
||||||
|
v, err = WaitDelayArg(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// shoul not be OK as argument
|
||||||
|
// value is < 0.
|
||||||
|
expected = defaultValue
|
||||||
|
r.WithArg(WaitDelayArgKey, "-1.0")
|
||||||
|
v, err = WaitDelayArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// shoul not be OK as argument
|
||||||
|
// value is > config.MaximumWaitTimeout().
|
||||||
|
expected = defaultValue
|
||||||
|
r.WithArg(WaitDelayArgKey, "31.0")
|
||||||
|
v, err = WaitDelayArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// should not be OK as
|
||||||
|
// argument value is invalid.
|
||||||
|
expected = defaultValue
|
||||||
|
r.WithArg(WaitDelayArgKey, "foo")
|
||||||
|
v, err = WaitDelayArg(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPaperSizeArgs(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
var expected float64
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
config := conf.DefaultConfig()
|
||||||
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// arguments do not exist.
|
||||||
|
width, height, err := PaperSizeArgs(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, opts.PaperWidth, width)
|
||||||
|
assert.Equal(t, opts.PaperHeight, height)
|
||||||
|
// arguments exist.
|
||||||
|
expected = 5.0
|
||||||
|
r.WithArg(PaperWidthArgKey, "5.0")
|
||||||
|
r.WithArg(PaperHeightArgKey, "5.0")
|
||||||
|
width, height, err = PaperSizeArgs(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, width)
|
||||||
|
assert.Equal(t, expected, height)
|
||||||
|
// shoul not be OK as arguments
|
||||||
|
// value are < 0.
|
||||||
|
expected = opts.PaperWidth
|
||||||
|
r.WithArg(PaperWidthArgKey, "-1.0")
|
||||||
|
width, _, err = PaperSizeArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, width)
|
||||||
|
r.WithArg(PaperWidthArgKey, "5.0")
|
||||||
|
expected = opts.PaperHeight
|
||||||
|
r.WithArg(PaperHeightArgKey, "-1.0")
|
||||||
|
_, height, err = PaperSizeArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, height)
|
||||||
|
r.WithArg(PaperHeightArgKey, "5.0")
|
||||||
|
// should not be OK as
|
||||||
|
// arguments value are invalids.
|
||||||
|
expected = opts.PaperWidth
|
||||||
|
r.WithArg(PaperWidthArgKey, "foo")
|
||||||
|
width, _, err = PaperSizeArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, width)
|
||||||
|
r.WithArg(PaperWidthArgKey, "5.0")
|
||||||
|
expected = opts.PaperHeight
|
||||||
|
r.WithArg(PaperHeightArgKey, "foo")
|
||||||
|
_, height, err = PaperSizeArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, height)
|
||||||
|
r.WithArg(PaperHeightArgKey, "5.0")
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarginArgs(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
var expected float64
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
config := conf.DefaultConfig()
|
||||||
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// arguments do not exist.
|
||||||
|
top, bottom, left, right, err := MarginArgs(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, opts.MarginTop, top)
|
||||||
|
assert.Equal(t, opts.MarginBottom, bottom)
|
||||||
|
assert.Equal(t, opts.MarginLeft, left)
|
||||||
|
assert.Equal(t, opts.MarginRight, right)
|
||||||
|
// arguments exist.
|
||||||
|
expected = 5.0
|
||||||
|
r.WithArg(MarginTopArgKey, "5.0")
|
||||||
|
r.WithArg(MarginBottomArgKey, "5.0")
|
||||||
|
r.WithArg(MarginLeftArgKey, "5.0")
|
||||||
|
r.WithArg(MarginRightArgKey, "5.0")
|
||||||
|
top, bottom, left, right, err = MarginArgs(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, top)
|
||||||
|
assert.Equal(t, expected, bottom)
|
||||||
|
assert.Equal(t, expected, left)
|
||||||
|
assert.Equal(t, expected, right)
|
||||||
|
// shoul not be OK as arguments
|
||||||
|
// value are < 0.
|
||||||
|
expected = opts.MarginTop
|
||||||
|
r.WithArg(MarginTopArgKey, "-1.0")
|
||||||
|
top, _, _, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, top)
|
||||||
|
r.WithArg(MarginTopArgKey, "5.0")
|
||||||
|
expected = opts.MarginBottom
|
||||||
|
r.WithArg(MarginBottomArgKey, "-1.0")
|
||||||
|
_, bottom, _, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, bottom)
|
||||||
|
r.WithArg(MarginBottomArgKey, "5.0")
|
||||||
|
expected = opts.MarginLeft
|
||||||
|
r.WithArg(MarginLeftArgKey, "-1.0")
|
||||||
|
_, _, left, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, left)
|
||||||
|
r.WithArg(MarginLeftArgKey, "5.0")
|
||||||
|
expected = opts.MarginRight
|
||||||
|
r.WithArg(MarginRightArgKey, "-1.0")
|
||||||
|
_, _, _, right, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, right)
|
||||||
|
r.WithArg(MarginRightArgKey, "5.0")
|
||||||
|
// should not be OK as
|
||||||
|
// arguments value are invalids.
|
||||||
|
expected = opts.MarginTop
|
||||||
|
r.WithArg(MarginTopArgKey, "foo")
|
||||||
|
top, _, _, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, top)
|
||||||
|
r.WithArg(MarginTopArgKey, "5.0")
|
||||||
|
expected = opts.MarginBottom
|
||||||
|
r.WithArg(MarginBottomArgKey, "foo")
|
||||||
|
_, bottom, _, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, bottom)
|
||||||
|
r.WithArg(MarginBottomArgKey, "5.0")
|
||||||
|
expected = opts.MarginLeft
|
||||||
|
r.WithArg(MarginLeftArgKey, "foo")
|
||||||
|
_, _, left, _, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, left)
|
||||||
|
r.WithArg(MarginLeftArgKey, "5.0")
|
||||||
|
expected = opts.MarginRight
|
||||||
|
r.WithArg(MarginRightArgKey, "foo")
|
||||||
|
_, _, _, right, err = MarginArgs(r, config)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, right)
|
||||||
|
r.WithArg(MarginRightArgKey, "5.0")
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,22 +58,20 @@ HeaderFooterContents is a helper for retrieving
|
|||||||
the content of the files "header.html"
|
the content of the files "header.html"
|
||||||
and "footer.html".
|
and "footer.html".
|
||||||
*/
|
*/
|
||||||
func HeaderFooterContents(r Resource) (string, string, error) {
|
func HeaderFooterContents(r Resource, config conf.Config) (string, string, error) {
|
||||||
const (
|
const op string = "resource.HeaderFooterContents"
|
||||||
op string = "resource.HeaderFooterContents"
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
defaultHeaderFooterHTML string = "<html><head></head><body></body></html>"
|
|
||||||
)
|
|
||||||
resolver := func() (string, string, error) {
|
resolver := func() (string, string, error) {
|
||||||
headerHTML, err := r.Fcontent("header.html", defaultHeaderFooterHTML)
|
headerHTML, err := r.Fcontent("header.html", opts.HeaderHTML)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultHeaderFooterHTML,
|
return opts.HeaderHTML,
|
||||||
defaultHeaderFooterHTML,
|
opts.FooterHTML,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
footerHTML, err := r.Fcontent("footer.html", defaultHeaderFooterHTML)
|
footerHTML, err := r.Fcontent("footer.html", opts.FooterHTML)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultHeaderFooterHTML,
|
return opts.HeaderHTML,
|
||||||
defaultHeaderFooterHTML,
|
opts.FooterHTML,
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
return headerHTML,
|
return headerHTML,
|
||||||
|
|||||||
45
internal/app/xhttp/pkg/resource/file_test.go
Normal file
45
internal/app/xhttp/pkg/resource/file_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||||
|
"github.com/thecodingmachine/gotenberg/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHeaderFooterContents(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
var expected string
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
config := conf.DefaultConfig()
|
||||||
|
opts := printer.DefaultChromePrinterOptions(config)
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// arguments do not exist.
|
||||||
|
header, footer, err := HeaderFooterContents(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, opts.HeaderHTML, header)
|
||||||
|
assert.Equal(t, opts.FooterHTML, footer)
|
||||||
|
// arguments exist.
|
||||||
|
expected = "Gutenberg"
|
||||||
|
fpath := test.OfficeFpaths(t)[2]
|
||||||
|
f1, err := os.Open(fpath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer f1.Close() // nolint: errcheck
|
||||||
|
err = r.WithFile("header.html", f1)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
f2, err := os.Open(fpath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer f2.Close() // nolint: errcheck
|
||||||
|
err = r.WithFile("footer.html", f2)
|
||||||
|
header, footer, err = HeaderFooterContents(r, config)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Contains(t, header, expected)
|
||||||
|
assert.Contains(t, footer, expected)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
286
internal/app/xhttp/pkg/resource/resource_test.go
Normal file
286
internal/app/xhttp/pkg/resource/resource_test.go
Normal file
@@ -0,0 +1,286 @@
|
|||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/thecodingmachine/gotenberg/internal/pkg/xassert"
|
||||||
|
"github.com/thecodingmachine/gotenberg/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStringArg(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue string = "FOO"
|
||||||
|
)
|
||||||
|
var expected string
|
||||||
|
rule := xassert.StringOneOf([]string{"FOO", "BAR"})
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// empty value, result should be equal
|
||||||
|
// to the default value.
|
||||||
|
r.WithArg(ResultFilenameArgKey, "")
|
||||||
|
v, err := r.StringArg(ResultFilenameArgKey, defaultValue)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, defaultValue, v)
|
||||||
|
// result should be equal to given value
|
||||||
|
// as it is one of "FOO" and "BAR".
|
||||||
|
expected = "BAR"
|
||||||
|
r.WithArg(ResultFilenameArgKey, expected)
|
||||||
|
v, err = r.StringArg(ResultFilenameArgKey, defaultValue, rule)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// should not be OK as given value is not
|
||||||
|
// one of "FOO" and "BAR".
|
||||||
|
expected = defaultValue
|
||||||
|
r.WithArg(ResultFilenameArgKey, "BAZ")
|
||||||
|
v, err = r.StringArg(ResultFilenameArgKey, defaultValue, rule)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInt64Arg(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue int64 = 10
|
||||||
|
)
|
||||||
|
var expected int64
|
||||||
|
rule := xassert.Int64NotInferiorTo(6)
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// empty value, result should be equal
|
||||||
|
// to the default value.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "")
|
||||||
|
v, err := r.Int64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// result should be equal to given value
|
||||||
|
// but as integer.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "5")
|
||||||
|
v, err = r.Int64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = 5
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// should not be OK as given value is not
|
||||||
|
// a string representation of an integer.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "foo")
|
||||||
|
v, err = r.Int64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// should not be OK as given value does not
|
||||||
|
// validate the rule x >= 6.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "5")
|
||||||
|
v, err = r.Int64Arg(WaitTimeoutArgKey, defaultValue, rule)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloat64Arg(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue float64 = 10.0
|
||||||
|
)
|
||||||
|
var expected float64
|
||||||
|
rule := xassert.Float64NotInferiorTo(6.0)
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// empty value, result should be equal
|
||||||
|
// to the default value.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "")
|
||||||
|
v, err := r.Float64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// result should be equal to given value
|
||||||
|
// but as float.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "5.5")
|
||||||
|
v, err = r.Float64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = 5.5
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// should not be OK as given value is not
|
||||||
|
// a string representation of a float.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "foo")
|
||||||
|
v, err = r.Float64Arg(WaitTimeoutArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// should not be OK as given value does not
|
||||||
|
// validate the rule x >= 6.
|
||||||
|
r.WithArg(WaitTimeoutArgKey, "5.0")
|
||||||
|
v, err = r.Float64Arg(WaitTimeoutArgKey, defaultValue, rule)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBoolArg(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue bool = true
|
||||||
|
)
|
||||||
|
var expected bool
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// empty value, result should be equal
|
||||||
|
// to the default value.
|
||||||
|
r.WithArg(LandscapeArgKey, "")
|
||||||
|
v, err := r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// result should be equal to given value
|
||||||
|
// but as boolean.
|
||||||
|
r.WithArg(LandscapeArgKey, "1")
|
||||||
|
v, err = r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = true
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
r.WithArg(LandscapeArgKey, "true")
|
||||||
|
v, err = r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = true
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
r.WithArg(LandscapeArgKey, "0")
|
||||||
|
v, err = r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = false
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
r.WithArg(LandscapeArgKey, "false")
|
||||||
|
v, err = r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = false
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// should not be OK as given value is not
|
||||||
|
// a string representation of a boolean.
|
||||||
|
r.WithArg(LandscapeArgKey, "foo")
|
||||||
|
v, err = r.BoolArg(LandscapeArgKey, defaultValue)
|
||||||
|
expected = defaultValue
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFpath(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// file exists.
|
||||||
|
fpath := test.MergeFpaths(t)[0]
|
||||||
|
f, err := os.Open(fpath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
filename := "foo.pdf"
|
||||||
|
err = r.WithFile(filename, f)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
absDirPath, err := filepath.Abs(fmt.Sprintf("%s/%s", TemporaryDirectory, resourceDirectoryName))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
expected := fmt.Sprintf("%s/%s", absDirPath, filename)
|
||||||
|
v, err := r.Fpath(filename)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
// should not be OK as file does
|
||||||
|
// not exist.
|
||||||
|
_, err = r.Fpath("bar.pdf")
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFpaths(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
fpath := test.MergeFpaths(t)[0]
|
||||||
|
f, err := os.Open(fpath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer f.Close() // nolint: errcheck
|
||||||
|
filename := "foo.pdf"
|
||||||
|
err = r.WithFile(filename, f)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// file extension exists.
|
||||||
|
absDirPath, err := filepath.Abs(fmt.Sprintf("%s/%s", TemporaryDirectory, resourceDirectoryName))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
expected := []string{
|
||||||
|
fmt.Sprintf("%s/%s", absDirPath, filename),
|
||||||
|
}
|
||||||
|
fpaths, err := r.Fpaths(".pdf")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, fpaths)
|
||||||
|
// should not be OK as file extension
|
||||||
|
// does not exist.
|
||||||
|
_, err = r.Fpaths(".html")
|
||||||
|
test.AssertError(t, err)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFcontent(t *testing.T) {
|
||||||
|
const (
|
||||||
|
resourceDirectoryName string = "foo"
|
||||||
|
defaultValue string = "Gutenberg"
|
||||||
|
)
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
filename := "foo.txt"
|
||||||
|
// file does not exist, expecting
|
||||||
|
// value.
|
||||||
|
v, err := r.Fcontent(filename, defaultValue)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, defaultValue, v)
|
||||||
|
// file exists.
|
||||||
|
fpath := test.OfficeFpaths(t)[2]
|
||||||
|
f, err := os.Open(fpath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer f.Close() // nolint: errcheck
|
||||||
|
err = r.WithFile(filename, f)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
v, err = r.Fcontent(filename, defaultValue)
|
||||||
|
assert.Contains(t, v, defaultValue)
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetters(t *testing.T) {
|
||||||
|
const resourceDirectoryName string = "foo"
|
||||||
|
logger := test.DebugLogger()
|
||||||
|
r, err := New(logger, resourceDirectoryName)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// has arg.
|
||||||
|
assert.Equal(t, false, r.HasArg(LandscapeArgKey))
|
||||||
|
r.WithArg(LandscapeArgKey, "foo")
|
||||||
|
assert.Equal(t, true, r.HasArg(LandscapeArgKey))
|
||||||
|
// directory path.
|
||||||
|
absDirPath, err := filepath.Abs(fmt.Sprintf("%s/%s", TemporaryDirectory, resourceDirectoryName))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, absDirPath, r.DirPath())
|
||||||
|
// finally...
|
||||||
|
err = r.Close()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user