mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
WIP: tests of api pkg
This commit is contained in:
@@ -36,7 +36,7 @@ func New(c echo.Context, logger *logger.Logger, config *config.Config) *Context
|
|||||||
// MustCastFromEchoContext cast an echo.Context to our custom
|
// MustCastFromEchoContext cast an echo.Context to our custom
|
||||||
// context. If something goes wrong, panic.
|
// context. If something goes wrong, panic.
|
||||||
func MustCastFromEchoContext(c echo.Context) *Context {
|
func MustCastFromEchoContext(c echo.Context) *Context {
|
||||||
const op = "MustCastFromEchoContext"
|
const op = "context.MustCastFromEchoContext"
|
||||||
ctx, ok := c.(*Context)
|
ctx, ok := c.(*Context)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf("%s: unable to cast an echo.Context to a custom context", op))
|
panic(fmt.Sprintf("%s: unable to cast an echo.Context to a custom context", op))
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/handler"
|
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/handler"
|
||||||
@@ -9,13 +12,30 @@ import (
|
|||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/random"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/random"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TestingTraceEnvVar is an environment
|
||||||
|
// variable used in some tests.
|
||||||
|
TestingTraceEnvVar = "TESTING_TRACE"
|
||||||
|
// TestsTracePrefix helps
|
||||||
|
// creating all resources inside a prefix.
|
||||||
|
// Only used in some tests
|
||||||
|
// to check if the resources
|
||||||
|
// have been removed.
|
||||||
|
TestsTracePrefix = "tmp"
|
||||||
|
)
|
||||||
|
|
||||||
// Context helps extending the default echo.Context with
|
// Context helps extending the default echo.Context with
|
||||||
// our custom context.
|
// our custom context.
|
||||||
func Context(config *config.Config) echo.MiddlewareFunc {
|
func Context(config *config.Config) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
// generate a unique identifier for the request.
|
var trace string
|
||||||
trace := random.Get()
|
if os.Getenv(TestingTraceEnvVar) == "1" {
|
||||||
|
trace = fmt.Sprintf("%s/%s", TestsTracePrefix, random.Get())
|
||||||
|
} else {
|
||||||
|
// generate a unique identifier for the request.
|
||||||
|
trace = random.Get()
|
||||||
|
}
|
||||||
// create the logger for this request using
|
// create the logger for this request using
|
||||||
// the previous identifier as trace.
|
// the previous identifier as trace.
|
||||||
logger := logger.New(config.LogLevel(), trace)
|
logger := logger.New(config.LogLevel(), trace)
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ func Error() echo.MiddlewareFunc {
|
|||||||
// so far so good!
|
// so far so good!
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// if it's an error from echo
|
||||||
|
// like 404 not found and so on.
|
||||||
|
if echoHTTPErr, ok := err.(*echo.HTTPError); ok {
|
||||||
|
return echoHTTPErr
|
||||||
|
}
|
||||||
// we log the initial error before returning
|
// we log the initial error before returning
|
||||||
// the HTTP error.
|
// the HTTP error.
|
||||||
errOp := standarderror.Op(err)
|
errOp := standarderror.Op(err)
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ func FromEnv() (*Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return c, &standarderror.Error{Op: op, Err: err}
|
return c, &standarderror.Error{Op: op, Err: err}
|
||||||
}
|
}
|
||||||
disableChromeEndpoints, err := boolFromEnv(disableGoogleChromeEnvVar, c.EnableChromeEndpoints())
|
disableChromeEndpoints, err := boolFromEnv(disableGoogleChromeEnvVar, !c.EnableChromeEndpoints())
|
||||||
c.enableChromeEndpoints = !disableChromeEndpoints
|
c.enableChromeEndpoints = !disableChromeEndpoints
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, &standarderror.Error{Op: op, Err: err}
|
return c, &standarderror.Error{Op: op, Err: err}
|
||||||
}
|
}
|
||||||
disableUnoconvEndpoints, err := boolFromEnv(disableUnoconvEnvVar, c.EnableUnoconvEndpoints())
|
disableUnoconvEndpoints, err := boolFromEnv(disableUnoconvEnvVar, !c.EnableUnoconvEndpoints())
|
||||||
c.enableUnoconvEndpoints = !disableUnoconvEndpoints
|
c.enableUnoconvEndpoints = !disableUnoconvEndpoints
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, &standarderror.Error{Op: op, Err: err}
|
return c, &standarderror.Error{Op: op, Err: err}
|
||||||
|
|||||||
@@ -12,9 +12,12 @@ import (
|
|||||||
|
|
||||||
func TestDefaultWaitTimeout(t *testing.T) {
|
func TestDefaultWaitTimeout(t *testing.T) {
|
||||||
// should be OK.
|
// should be OK.
|
||||||
os.Setenv(defaultWaitTimeoutEnvVar, "1.5")
|
|
||||||
config, err := FromEnv()
|
config, err := FromEnv()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 10.0, config.DefaultWaitTimeout())
|
||||||
|
os.Setenv(defaultWaitTimeoutEnvVar, "1.5")
|
||||||
|
config, err = FromEnv()
|
||||||
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 1.5, config.DefaultWaitTimeout())
|
assert.Equal(t, 1.5, config.DefaultWaitTimeout())
|
||||||
// should failed.
|
// should failed.
|
||||||
os.Setenv(defaultWaitTimeoutEnvVar, "foo")
|
os.Setenv(defaultWaitTimeoutEnvVar, "foo")
|
||||||
@@ -27,9 +30,12 @@ func TestDefaultWaitTimeout(t *testing.T) {
|
|||||||
|
|
||||||
func TestDefaultListenPort(t *testing.T) {
|
func TestDefaultListenPort(t *testing.T) {
|
||||||
// should be OK.
|
// should be OK.
|
||||||
os.Setenv(defaultListenPortEnvVar, "4000")
|
|
||||||
config, err := FromEnv()
|
config, err := FromEnv()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "3000", config.DefaultListenPort())
|
||||||
|
os.Setenv(defaultListenPortEnvVar, "4000")
|
||||||
|
config, err = FromEnv()
|
||||||
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "4000", config.DefaultListenPort())
|
assert.Equal(t, "4000", config.DefaultListenPort())
|
||||||
// should failed.
|
// should failed.
|
||||||
os.Setenv(defaultListenPortEnvVar, "foo")
|
os.Setenv(defaultListenPortEnvVar, "foo")
|
||||||
@@ -47,9 +53,12 @@ func TestDefaultListenPort(t *testing.T) {
|
|||||||
|
|
||||||
func TestEnableChromeEndpoints(t *testing.T) {
|
func TestEnableChromeEndpoints(t *testing.T) {
|
||||||
// should be OK.
|
// should be OK.
|
||||||
os.Setenv(disableGoogleChromeEnvVar, "1")
|
|
||||||
config, err := FromEnv()
|
config, err := FromEnv()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, true, config.EnableChromeEndpoints())
|
||||||
|
os.Setenv(disableGoogleChromeEnvVar, "1")
|
||||||
|
config, err = FromEnv()
|
||||||
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, false, config.EnableChromeEndpoints())
|
assert.Equal(t, false, config.EnableChromeEndpoints())
|
||||||
os.Setenv(disableGoogleChromeEnvVar, "0")
|
os.Setenv(disableGoogleChromeEnvVar, "0")
|
||||||
config, err = FromEnv()
|
config, err = FromEnv()
|
||||||
@@ -66,9 +75,12 @@ func TestEnableChromeEndpoints(t *testing.T) {
|
|||||||
|
|
||||||
func TestEnableUnoconvEndpoints(t *testing.T) {
|
func TestEnableUnoconvEndpoints(t *testing.T) {
|
||||||
// should be OK.
|
// should be OK.
|
||||||
os.Setenv(disableUnoconvEnvVar, "1")
|
|
||||||
config, err := FromEnv()
|
config, err := FromEnv()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, true, config.EnableUnoconvEndpoints())
|
||||||
|
os.Setenv(disableUnoconvEnvVar, "1")
|
||||||
|
config, err = FromEnv()
|
||||||
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, false, config.EnableUnoconvEndpoints())
|
assert.Equal(t, false, config.EnableUnoconvEndpoints())
|
||||||
os.Setenv(disableUnoconvEnvVar, "0")
|
os.Setenv(disableUnoconvEnvVar, "0")
|
||||||
config, err = FromEnv()
|
config, err = FromEnv()
|
||||||
@@ -85,9 +97,12 @@ func TestEnableUnoconvEndpoints(t *testing.T) {
|
|||||||
|
|
||||||
func TestLogLevel(t *testing.T) {
|
func TestLogLevel(t *testing.T) {
|
||||||
// should be OK.
|
// should be OK.
|
||||||
os.Setenv(logLevelEnvVar, "DEBUG")
|
|
||||||
config, err := FromEnv()
|
config, err := FromEnv()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, logrus.InfoLevel, config.LogLevel())
|
||||||
|
os.Setenv(logLevelEnvVar, "DEBUG")
|
||||||
|
config, err = FromEnv()
|
||||||
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, logrus.DebugLevel, config.LogLevel())
|
assert.Equal(t, logrus.DebugLevel, config.LogLevel())
|
||||||
os.Setenv(logLevelEnvVar, "INFO")
|
os.Setenv(logLevelEnvVar, "INFO")
|
||||||
config, err = FromEnv()
|
config, err = FromEnv()
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
// Package timeout helps managing
|
// Package timeout helps creating
|
||||||
// context with timeout.
|
// context with timeout.
|
||||||
package timeout
|
package timeout
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestContext(t *testing.T) {
|
||||||
|
ctx, cancel := Context(1.5)
|
||||||
|
assert.NotNil(t, ctx)
|
||||||
|
assert.NotNil(t, cancel)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDuration(t *testing.T) {
|
func TestDuration(t *testing.T) {
|
||||||
expected := time.Duration(1500) * time.Millisecond
|
expected := time.Duration(1500) * time.Millisecond
|
||||||
result := Duration(1.5)
|
result := Duration(1.5)
|
||||||
|
|||||||
@@ -30,6 +30,16 @@ func AssertStatusCode(t *testing.T, expectedStatusCode int, srv http.Handler, re
|
|||||||
assert.Equal(t, expectedStatusCode, rec.Code)
|
assert.Equal(t, expectedStatusCode, rec.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AssertDirectoryEmpty checks if given directory
|
||||||
|
// is empty.
|
||||||
|
func AssertDirectoryEmpty(t *testing.T, directory string) {
|
||||||
|
f, err := os.Open(directory)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer f.Close() // nolint: errcheck
|
||||||
|
_, err = f.Readdir(1)
|
||||||
|
assert.Equal(t, io.EOF, err)
|
||||||
|
}
|
||||||
|
|
||||||
// AssertConcurrent runs all functions simultaneously
|
// AssertConcurrent runs all functions simultaneously
|
||||||
// and wait until execution has completed
|
// and wait until execution has completed
|
||||||
// or an error is encountered.
|
// or an error is encountered.
|
||||||
|
|||||||
Reference in New Issue
Block a user